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 boolean uniqueChar(String s) { | |
if (s == null) | |
throw new IllegalArgumentException(); | |
if (s.length() <= 1) | |
return true; | |
if (s.length() > 256) | |
return false; | |
boolean[] iFlag = new boolean[256]; | |
for (int i = 0; i < s.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
public class Main { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Main so = new Main(); | |
so.reverse("abcdefghijk"); | |
} |
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
// Ask the interviewer about details: are the characters case sensitive, | |
// are the white space and special characters significant? | |
/** | |
* Solution 1 | |
* Assume the string falls under a certain encoding. | |
* Check if the two strings have the same character count. | |
* Use an extra constant | |
* size array of integers to store the frequency of each character in str1, | |
* for each character in str2, minus the frequency array's value by 1. In |
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 Main { | |
/** | |
* @* Runtime & space | |
* runtime: O(n) | |
* space: O(1) | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Main so = new Main(); |
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 Main { | |
/** | |
* @Runtime & spaces ||runtime: O(n) ||space: O(n) | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Main so = new Main(); | |
String s = "a"; | |
System.out.println(so.compress(s)); |
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
/** | |
* 1.6 Given an image represented by an NxN matrix, | |
* where each pixel in the image is 4 bytes, write a | |
* method to rotate the image by 90 degrees. Can you | |
* do this in place? | |
* @Runtime & spaces | |
* runtime: O(n^2) | |
* space: O(1) | |
* Assumption: the rotation is clockwise. | |
*/ |
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
/** | |
* 1.7 Write an algorithm such that if | |
* an element in an MxN matrix is 0, its | |
* entire row and column are set to 0. | |
* | |
* @Runtime & spaces | |
* runtime: O(mn) | |
* space: O(1) | |
*/ | |
//Solution: idea got from zhenzhenanan |
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 Main { | |
/** | |
* 1.8 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 (e.g.,"waterbottle"is | |
* a rotation of"erbottlewat"). | |
* | |
* Solution: just the same as CTCI |
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
/** | |
* 2.1 Write code to remove duplicates from an unsorted linked list. | |
* FOLLOW UP | |
* How would you solve this problem if a temporary buffer is not allowed? | |
* | |
* Solution1: | |
* @Runtime & spaces | |
* runtime: O(n) | |
* space: 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
public class Main { | |
/** | |
* 2.2 Implement an algorithm to find the k th | |
* to last element of a singly linked list. | |
* | |
* we have defined k such that passing in k = 1 would return | |
* the last element,k = 2 would return to the second to last | |
* element,and soon.It is equally acceptable to define k such | |
* that k = 0 would return the last element. |
OlderNewer