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 isPerm(String str1, String str2) { | |
// edge cases: null, len not equal, len = 0 | |
if(str1 == null || str2 == null) | |
return false; | |
int m = str1.length(); | |
int n = str2.length(); | |
if(m != n) | |
return false; | |
if(m == 0) | |
return true; |
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 void replace(char[] line) { | |
int len; // original length; | |
int cnt = 0; // space count | |
for(len = 0; line[len] != '\0'; len++) | |
{ | |
if(line[len] == ' ') | |
cnt++; | |
} | |
int new_len = len + cnt*2; // length of new string | |
int k = new_len - 1; // starts from the end of new string |
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 String compress(String line) { | |
// corner cases: null, 0-length | |
if(line == null || line.length() == 0) | |
return ""; | |
int len = line.length(); | |
char prev = line.charAt(0); // save the previous character | |
int cnt = 1; | |
StringBuffer sb = new StringBuffer(); | |
for(int i = 1; i < len; i++) { | |
if(line.charAt(i) == prev) // if current == previous |
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 void rotate(int[][] matrix) { | |
if(matrix == null) | |
return; | |
int N = matrix.length; | |
for(int i = 0; i < N/2; i++) // This is the layer | |
for(int j = i; j < N-i-1; j++) { // This is the offset to start | |
// swap | |
int t = matrix[i][j]; | |
matrix[i][j] = matrix[N-j-1][i]; | |
matrix[N-j-1][i] = matrix[N-i-1][N-j-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 void zero(int[][] matrix) { | |
if(matrix == null) | |
return; | |
int m = matrix.length; | |
if(m == 0) | |
return; | |
int n = matrix[0].length; | |
boolean[] row_zero = new boolean[m]; | |
boolean[] col_zero = new boolean[n]; | |
for(int i = 0; i < m; 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 boolean isRotate(String s1, String s2) { | |
if(s1 == null || s2 == null || s1.isEmpty() || s2.isEmpty() || s1.length() != s2.length()) | |
return false; | |
else | |
return isSubstring(s1+s1, s2); | |
} | |
private boolean isSubstring(String s1, String s2) { | |
return s1.contains(s2); | |
} |
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
// O(N) | |
public void removeDuplicate(ListNode head) { | |
if(head == null || head.next == null) { | |
return; | |
} | |
HashMap<Integer, Integer> dict = new HashMap<Integer, Integer>(); | |
ListNode cur = head; | |
ListNode prev = head; | |
while(cur != null) { | |
if(!dict.containsKey(cur.val)) { |
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 int kthToLast(ListNode head, int k) { | |
if(head == null) | |
return -1; | |
ListNode p1, p2; | |
p1 = p2 = head; | |
for(int i = 0; p1 != null && i < k; i++) { | |
p1 = p1.next; | |
} | |
while(p1 != null) { | |
p1 = p1.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 void deleteNode(ListNode node) { | |
// node is null or node is the last node | |
if(node == null || node.next == null) | |
return; | |
ListNode next = node.next; | |
node.val = next.val; | |
node.next = next.next; | |
next = null; | |
} |
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 ListNode partition(ListNode head, int x) { | |
if(head == null || head.next == null) | |
return head; | |
ListNode lessHead, nolessHead, lessCur, nolessCur; | |
lessHead = nolessHead = lessCur = nolessCur = null; | |
ListNode cur, next; | |
cur = head; | |
while(cur != null) { | |
next = cur.next; | |
cur.next = null; // cut off cur node from the list |
OlderNewer