This file contains hidden or 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 Contact { | |
| String email; | |
| ... | |
| public String toString() { | |
| // StringBuffer sb = new StringBuffer(); | |
| StringBuilder sb = new StringBuilder(); | |
| sb.append(first) | |
| .append(" ") |
This file contains hidden or 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
| // A = [1, 2, 3, 4, 5, 6] | |
| // L = [1, 2, 6, 24, 120, 720] | |
| // R = [720, 720, 360, 120, 30, 6] | |
| // first version | |
| int [] prod (int []A) { | |
| final int N = A.length; | |
| int []L = new int[N]; | |
| int []R = new int[N]; | |
This file contains hidden or 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
| // without lazy initialization | |
| class PeekingIterator implements Iterator<T> { | |
| private Iterator <T> it; | |
| private T nextV; | |
| private boolean hasPeekV | |
| private T peekV; | |
| public PeekingIterator (Iterator <T> iterator) { |
This file contains hidden or 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
| class NChooseMPermutation { | |
| TreeSet<String> ans = new TreeSet<String>(); | |
| public Set <String> perm (String str, int M) { | |
| char [] charAry = str.toCharArray(); | |
| final int N = str.length(); | |
| perm(Str, 0, charAry, N, M); | |
| } | |
| public void perm (String str, int i, char[] charAry, int N, int M) { |
This file contains hidden or 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 dim DP version | |
| /* | |
| ( ( ( ) ) ) ( ) ) | |
| 8 4 2 0 0 0 2 0 0 0 | |
| | | | | |
| +---------+-+ | |
| */ | |
| public int longestValidParentheses(String s) { | |
| final int N = s.length(); |
This file contains hidden or 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 maxProfit1(int[] prices) { | |
| if (prices.length == 0) { | |
| return 0; | |
| } else { | |
| int maxProfit = 0; | |
| int minPrice = prices[0]; | |
| for (int i = 1; i < prices.length; i++) { | |
| maxProfit = Math.max(maxProfit, prices[i] - minPrice); | |
| minPrice = Math.min(minPrice, prices[i]); | |
| } |
This file contains hidden or 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
| // constant space => quick sort | |
| public class Solution { | |
| // insert node in front of head linked list | |
| private ListNode insertFront(ListNode head, ListNode node) { | |
| // if head is null (empty list) | |
| // works fine | |
| node.next = head; | |
| return node; | |
| } |
This file contains hidden or 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 solve(char[][] board) { | |
| final int M = board.length; | |
| if (M == 0) { | |
| return; | |
| } | |
| final int N = board[0].length; | |
| boolean [][] visited = new boolean[M][N]; | |
| for (int i = 0; i < M; i++) { |
This file contains hidden or 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 String convert(String s, int nRows) { | |
| if (nRows == 1) { | |
| return s; | |
| } | |
| final int strLen = s.length(); | |
| final int blockSize = 2 * nRows - 2; | |
| char [] result = new char[strLen]; |
This file contains hidden or 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 minCut(String s) { | |
| final int N = s.length(); | |
| char []str = s.toCharArray(); | |
| // build palindrome | |
| boolean [][] palindrome = new boolean[N][N]; | |
| for (int i = N - 1; i >= 0; --i) { | |
| for (int j = i; j < N; j++) { | |
| if (i == j || | |
| (str[i] == str[j] && (j - i == 1 || palindrome[i + 1][j - 1]))) { |