This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestUnique { | |
/* use a array to record the occurrence of different characters | |
* first method uses extra space | |
* NOTE: the spaces will cause duplicates too | |
* the time complexity is O(n), the space complexity is also O(n) | |
*/ | |
public static boolean test(String input){ | |
boolean char_set[] = new boolean[255]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringReverse { | |
public static void main(String args[]){ | |
String s = "this is a demo test!"; | |
System.out.println(reverse(s)); | |
} | |
public static String reverse(String input){ | |
char[] data = input.toCharArray(); | |
int i = 0; | |
int j = data.length - 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RemoveDuplicates { | |
public static void main(String args[]){ | |
String s = "1234512345aabbbccccddddddeeefffffggg"; | |
char[] input = s.toCharArray(); | |
removeDup(input); | |
System.out.println(input); | |
} | |
public static void removeDup(char[] data){ | |
int len = data.length; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestAnagram { | |
public static void main(String args[]){ | |
String a = "tomcat as"; | |
String b = " tamcots a"; | |
System.out.println(test(a, b)); | |
} | |
public static boolean test(String s1, String s2){ | |
if(s1.length() != s2.length()) | |
return false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReplaceSpace { | |
public static void main(String args[]){ | |
System.out.println(replaceSpace("this is a demo test! ")); | |
} | |
public static String replaceSpace(String input){ | |
char s[] = input.toCharArray(); | |
int spaceCount = 0; | |
int index = 0; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RotateMatrix { | |
public static void main(String args[]){ | |
int[][] matrix = { | |
{1, 2, 3 ,4 ,5 ,6}, | |
{12, 13, 14, 15, 16, 17}, | |
{23, 24, 25, 26, 27, 28}, | |
{34, 35, 36, 37, 38, 39}, | |
{45, 46, 47, 48, 49, 50}, | |
{66, 67, 68, 69, 70, 71} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SetZero { | |
public static void main(String args[]){ | |
int[][] matrix = { | |
{1, 2, 3 ,4 ,5 ,6}, | |
{12, 13, 0, 15, 16, 17}, | |
{23, 24, 25, 26, 27, 28}, | |
{34, 35, 36, 0, 38, 39}, | |
{45, 46, 47, 48, 49, 50}, | |
{66, 0, 68, 69, 1, 71}, | |
{33, 46, 47, 48, 49, 50} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Assume you have a method isSubstring which checks if one word is a substring of | |
* another. | |
* Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using | |
* only one call to isSubstring (i e , “waterbottle” is a rotation of “erbottlewat”) | |
*/ | |
public class SubString { | |
public static void main(String args[]){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
import java.util.HashSet; | |
import java.util.NoSuchElementException; | |
/* | |
* A LinkedList class with a private static inner node class | |
* based on: | |
* http://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public AnyType findLastNth(int k){ | |
if(head == null || k <= 0) | |
return null; | |
if(size() < k) | |
return null; | |
Node<AnyType> tmp = head; | |
for(int i = 0; i < k; i++) | |
tmp = tmp.next; |
OlderNewer