GeezerVison is showing github.com — Initial check in · lelea2/Algorithm@542cc01
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: java/coding/3Sum.java +5-7Lines changed: 5 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,17 +5,15 @@ public ArrayList<ArrayList<Integer>> threeSum(int[] num) { | 5 | 5 | if (num.length < 3) { //If given array is less than 3 number, just returned right away | 6 | 6 | return result; | 7 | 7 | } | 8 | | - // sort array | | 8 | + // sort array (assume that array being sorted) | 9 | 9 | Arrays.sort(num); | 10 | 10 | for (int i = 0; i < num.length - 2; i++) { | 11 | 11 | // //avoid duplicate solutions | 12 | 12 | if (i == 0 || num[i] > num[i - 1]) { | 13 | 13 |
| 14 | 14 | int negate = -num[i]; | 15 | | - | 16 | 15 | int start = i + 1; | 17 | 16 | int end = num.length - 1; | 18 | | - | 19 | 17 | while (start < end) { | 20 | 18 | //case 1 | 21 | 19 | if (num[start] + num[end] == negate) { | @@ -28,11 +26,12 @@ public ArrayList<ArrayList<Integer>> threeSum(int[] num) { | 28 | 26 | start++; | 29 | 27 | end--; | 30 | 28 | //avoid duplicate solutions | 31 | | - while (start < end && num[end] == num[end + 1]) | | 29 | + while (start < end && num[end] == num[end + 1]) { | 32 | 30 | end--; | 33 | | - | 34 | | - while (start < end && num[start] == num[start - 1]) | | 31 | + } | | 32 | + while (start < end && num[start] == num[start - 1]) { | 35 | 33 | start++; | | 34 | + } | 36 | 35 | //case 2 | 37 | 36 | } else if (num[start] + num[end] < negate) { | 38 | 37 | start++; | @@ -41,7 +40,6 @@ public ArrayList<ArrayList<Integer>> threeSum(int[] num) { | 41 | 40 | end--; | 42 | 41 | } | 43 | 42 | } | 44 | | - | 45 | 43 | } | 46 | 44 | } | 47 | 45 |
| |
Collapse file Copy file name to clipboardExpand all lines: java/coding/kthElementOf2SortedArray.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,11 +46,11 @@ public static int findKth(int A[], int B[], int k, int aStart, int aEnd, int bSt | 46 | 46 | aMid = aMid + aStart; | 47 | 47 | bMid = bMid + bStart; | 48 | 48 |
| 49 | | - if (A[aMid] > B[bMid]) { | | 49 | + if (A[aMid] > B[bMid]) { //aEnd, bStart | 50 | 50 | k = k - (bMid - bStart + 1); | 51 | 51 | aEnd = aMid; | 52 | 52 | bStart = bMid + 1; | 53 | | - } else { | | 53 | + } else { //(A[mid] <= B[mid]) => bEnd, aStart | 54 | 54 | k = k - (aMid - aStart + 1); | 55 | 55 | bEnd = bMid; | 56 | 56 | aStart = aMid + 1; | |
Collapse file Copy file name to clipboard +36Lines changed: 36 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/** | | 2 | + * Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. | | 3 | + *The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. | | 4 | + */ | | 5 | + | | 6 | +//Idea: this typical problem could be solve with stacking logic | | 7 | +//Create a hash map contains corresponding character | | 8 | +//if value contains key, do array push | | 9 | +//if value contains hashmap value, do array pop | | 10 | +//at the end, if array/stack is empty, meant your parenthensis is balanced. | | 11 | +// | | 12 | +// | | 13 | +public static boolean isValid(String s) { | | 14 | + HashMap<Character, Character> map = new HashMap<Character, Character>(); | | 15 | + map.put('(', ')'); | | 16 | + map.put('[', ']'); | | 17 | + map.put('{', '}'); | | 18 | + | | 19 | + Stack<Character> stack = new Stack<Character>(); | | 20 | + | | 21 | + for (int i = 0; i < s.length(); i++) { | | 22 | + char curr = s.charAt(i); | | 23 | + | | 24 | + if (map.keySet().contains(curr)) { | | 25 | + stack.push(curr); | | 26 | + } else if (map.values().contains(curr)) { | | 27 | + if (!stack.empty() && map.get(stack.peek()) == curr) { | | 28 | + stack.pop(); | | 29 | + } else { | | 30 | + return false; | | 31 | + } | | 32 | + } | | 33 | + } | | 34 | + | | 35 | + return stack.empty(); | | 36 | +} |
Collapse file Copy file name to clipboard +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | + | | 2 | +//Pow function in javascript | | 3 | +// | | 4 | +function pow(a, b) { | | 5 | + if (b === 0) { | | 6 | + return 0; | | 7 | + } | | 8 | + if (b === 1) { | | 9 | + return 1; | | 10 | + } | | 11 | + var c = pow(a/2); | | 12 | + if (b % 2 === 0) { | | 13 | + return c * c; | | 14 | + } else { | | 15 | + return c * c * a; | | 16 | + } | | 17 | +} |
|