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
//Implement an algorithm to determine if a string has all unique characters. | |
//What if you cannot use additional data structure | |
//first ask the string is Unicode string or ASCII string | |
// Unicode is a superset of ASCII, ASCII only defines 128 characters, which mappes 0-127 | |
// but Unicode defines 2^21(maybe less than), and mappes to 0-2^21, | |
// but the ASCII can be the same meaning in the Unicode | |
//the length of string is str.length() | |
//the length of array is array.length | |
//array[i], str.charAt(i). array=>[], string => () | |
//time complexity is O(n) |
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
//Implement a function to reverse a string | |
// time complexity O(n), space complexity O(n) | |
public Solution{ | |
public String reverseString(String str){ | |
String res = new String(); | |
if(str == null || str.length() == 0) | |
return res; | |
for(int i =0; i<str.length();i++) | |
{ | |
res.charAt[i] = str.charAt(str.length() -i); |
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
//Given two strings, write a method to decide if one is a permutation of the other | |
//Note that we will first make a assumption that the comparison is a case sensitive and whitespace is significant. | |
// "god " is different from "dog" | |
//Solution1, first sort the string, because the string 1 and string 2 are ust characters in different order | |
//So sort the string first will put the characters from two permutations in the same order | |
// we just need to compare the sorted verison of two strings | |
public class Solution1{ | |
public boolean isPermutation(String str1, String str2){ | |
if(str1==null||str2==null||str1.length() ==0||str2.length()==0||str1.length()!= str2.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
//Write a method to replace all spaces in a string with '%20' | |
//Bacause the String in Java is immutable, so we will use characters array | |
public class Solution{ | |
public void replaceSpace(char[] str, int length){ | |
if(str == null || str.length == 0) | |
return; | |
int count =0; | |
for(int i = 0;i <str.length;i++) | |
{ | |
if(str[i] == ' ') |
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
// Implement a method to perform basic string compression using the counts of repeated characters. | |
// For example, the string aabcccccaaa would become a2blc5a3. | |
// If the “compressed” string would not become smaller than the original string, | |
// your method should return the original string. | |
public class Solution{ | |
public String compressString(string str){ | |
StringBuffer res = new StringBuffer(); | |
char[] str = str.toCharArray(); | |
char check = str1[0]; | |
int count = 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 Solution{ | |
public void rotateMatrix(int[][] matrix){ | |
if(matrix == null || matrix.length == 0|| matrix[0].length ==0) | |
return; | |
int row = matrix.length; | |
for(int i =0; i < row/2; i++) | |
{ | |
for(int j =0;j< n-1-i;j++) | |
{ | |
matrix temp = matrix[i][j]; |
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 Solution{ | |
public void setZeroes(int[][] matrix){ | |
if(matrix == null || matrix.length ==0||matrix[0].length == 0) | |
return; | |
boolean rowHasZero = false; | |
boolean colHasZero = false; | |
//如果行列里面有0的话,记录一个为0就好,反正都是要变为0的,就先跳出,待程序最后再进行置0的操作,因为第一行和第一列是要被用来作为 | |
//判断行和判断列的 | |
for(int i = 0; i< matrix.length; i++) | |
{ |
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.Hashtable; | |
public class Solution1{ | |
public void deleteDuplicates(LinkedListNode n){ | |
if(n <= 0) | |
return; | |
Hashtable<Integer, boolean> res = new Hashtable<Integer, boolean>(); | |
LinkedListNode prev = null; | |
while(n != null) | |
{ | |
if (res.containsKey(n.data)) |
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 Solution{ | |
public int kthtoLast(LinkedListNode head, int k){ | |
if (k < 0|| head == null) | |
return -1; | |
LinkedListNode p1 = head; | |
LinkedListNode p2 = head; | |
//moving p2 to reach the position length - k. | |
for(int i = 0; i< k - 1; i++) | |
{ | |
p2 = p2.next; |
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 Solution{ | |
public int kthtoLast(LinkedListNode head, int k){ | |
if (k < 0|| head == null) | |
return -1; | |
LinkedListNode p1 = head; | |
LinkedListNode p2 = head; | |
//moving p2 to reach the position length - k. | |
for(int i = 0; i< k - 1; i++) | |
{ | |
p2 = p2.next; |
OlderNewer