saddham
BAN USERBelow code solve the problem in O(n)
package com.timepass.sortingx;
public class StringSort {
public static void threeWaySort(StringBuilder str){
int pos=0;
for (int i = pos; i < str.length(); i++) {
if(isLower(str.charAt(i))){
swap(str, pos++, i);
}
}
for (int i = pos; i < str.length(); i++) {
if(isSpace(str.charAt(i))){
swap(str, pos++, i);
}
}
for (int i = pos; i < str.length(); i++) {
if(isUpper(str.charAt(i))){
swap(str, pos++, i);
}
}
}
private static boolean isSpace(char ch){
return 32==ch;
}
private static boolean isUpper(char ch){
return ch>=65 && ch<=90;
}
private static boolean isLower(char ch){
return ch>=97 && ch<=122;
}
private static void swap(StringBuilder sb, int x, int y){
char y_char = sb.charAt(y);
sb.setCharAt(y, sb.charAt(x));
sb.setCharAt(x, y_char);
}
public static void main(String[] args) {
StringBuilder str = new StringBuilder("a cBd LkmY");
threeWaySort(str);
System.out.println(str);
}
}
Check this out:
public class Count {
public static void main(String[] args) {
countOnes(1000);
}
public static int countOnes(int power){
int count = 0;
if(power < 2){
count = power + 1;
} else{
StringBuilder number = new StringBuilder("11");
while((--power) > 0){
char [] charArray = number.toString().toCharArray();
int carry = 0;
number = new StringBuilder(""+charArray[charArray.length-1]);
for (int i = charArray.length-1; i > 0 ; i--) {
int temp = Integer.parseInt(""+charArray[i]) + Integer.parseInt(""+charArray[i-1]) + carry;
carry = temp/10;
temp = temp%10;
number.append(temp);
}
number.append(Integer.parseInt(""+charArray[0])+carry);
number.reverse();
}
Pattern pattern = Pattern.compile("1");
Matcher matcher = pattern.matcher(number);
while(matcher.find()){
count++;
}
}
return count;
}
}
A more simple solution is given below:
public class Task {
private Runnable runnable;
private int seconds;
public Runnable getRunnable() {
return runnable;
}
public void setRunnable(Runnable runnable) {
this.runnable = runnable;
}
public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
this.seconds = seconds;
}
}
public class MyTimer extends System{
Queue<Task> queue = new LinkedList<Task>();
@Override
public void schedule(Runnable runnable, int seconds) {
Task newTask = new Task();
newTask.setRunnable(runnable);
newTask.setSeconds(seconds);
queue.add(newTask);
Task task = queue.remove();
super.schedule(task.getRunnable(), task.getSeconds());
}
}
public class Task {
private Runnable runnable;
private int seconds;
public Runnable getRunnable() {
return runnable;
}
public void setRunnable(Runnable runnable) {
this.runnable = runnable;
}
public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
this.seconds = seconds;
}
}
public class MyTimer extends System{
Queue<Task> queue = new LinkedList<Task>();
Boolean flag;
@Override
public void schedule(Runnable runnable, int seconds) {
if(!queue.isEmpty()){
Task task = queue.remove();
if(flag){
super.schedule(task.getRunnable(), task.getSeconds());
}
flag = true;
} else{
super.schedule(runnable, seconds);
}
Task newTask = new Task();
newTask.setRunnable(runnable);
newTask.setSeconds(seconds);
queue.add(newTask);
}
}
- saddham April 18, 2015
}
- saddham August 22, 2015