Goutham
BAN USERpublic class NonZeroElements {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = new int[]{ 1, 0, 2, 0, 0, 3, 4};
System.out.println(Arrays.toString(arr));
//Using selection sort technique
int lastIndex = arr.length - 1;
for(int index = 0; index < arr.length;){
if(index == lastIndex)break;
if(arr[index] == 0){
//swap to last index
int temp = arr[lastIndex];
arr[lastIndex] = arr[index];
arr[index] = temp;
lastIndex--;
}else{
index++;
}
}
System.out.println(Arrays.toString(arr));
}
}
- Goutham March 16, 2017public class SinglyLinkedListBackward {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedNode root = new LinkedNode("A");
root.next = new LinkedNode("B");
root.next.next = new LinkedNode("C");
//recursiveReverse(root);
//iterativeReverse(root);
iterativeReverseWithOutMemory(root);
}
/**
* reverse linked list using iterative approach with O(n2) runtime
* reverse the LinkedList and iterate
* @param root
*/
private static void iterativeReverseWithOutMemory(LinkedNode root) {
if(root == null) return;
LinkedNode previousNode = null;
LinkedNode currentNode = root;
LinkedNode nextNode = null;
while(currentNode.next != null){
nextNode = currentNode.next;
currentNode.next = previousNode;
previousNode = currentNode;
currentNode = nextNode;
}
currentNode.next = previousNode;
while(currentNode != null){
System.out.print(currentNode.value+" -> ");
currentNode = currentNode.next;
}
}
/**
* reverse linked list using iterative approach
* push node to stack, and pop it
* consume O(n) memeory
* @param root
*/
private static void iterativeReverse(LinkedNode root) {
if(root == null) return;
Stack<LinkedNode> stack = new Stack<>();
while(root!=null){
stack.push(root);
root = root.next;
}
while(!stack.isEmpty()){
System.out.print(stack.pop().value+ "->");
}
}
/**
* reverse linked list using recursion
* @param root
*/
private static void recursiveReverse(LinkedNode root) {
if(root == null) return;
recursiveReverse(root.next);
System.out.print(root.value+ "->");
}
}
class LinkedNode{
public LinkedNode next;
public String value;
public LinkedNode(String value){
this.value = value;
}
}
- Goutham March 16, 2017public static int maxConsecutiveLetters(String str){
if(str== null || str.isEmpty())return 0;
int maxCount = 0;
int currentCount = 1;
int previousChar = str.charAt(0);
run : for(int index = 1; index < str.length(); index++){
if(str.charAt(index) == previousChar){
currentCount++;
maxCount = maxCount>currentCount ? maxCount : currentCount;
continue run;
}else{
currentCount = 1;
previousChar = str.charAt(index);
}
}
return maxCount;
}
- Goutham March 15, 2017import java.util.Stack;
public class SortingStack {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Stack<Integer> stack = new Stack<>();
stack.push(5);
stack.push(1);
stack.push(5);
stack.push(2);
stack.push(9);
stack.push(3);
stack.push(0);
System.out.println(stack);
sort(stack);
System.out.println(stack);
}
private static void sort(Stack<Integer> stack) {
if(stack.isEmpty())return;
int pop = stack.pop();
sort(stack);
insertInOrder(stack,pop);
}
private static void insertInOrder(Stack<Integer> stack, int element) {
if(stack.isEmpty() || stack.peek() < element){
stack.push(element);
}else{
int pop = stack.pop();
insertInOrder(stack,element);
stack.push(pop);
}
}
}
static public List<String> subString(String str){
List<String> lst = new LinkedList<>();
outer : for(int outer_index = 0; outer_index < str.length(); outer_index++){
int start_index = str.charAt(outer_index);
for(int inner_index = outer_index+1; inner_index < str.length(); inner_index++){
if(start_index < str.charAt(inner_index)){
lst.add(str.substring(outer_index, inner_index + 1));
start_index = str.charAt(inner_index);
}else{
continue outer;
}
}
}
return lst;
}
}
- Goutham April 18, 2017