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
| /** | |
| Solution One: Recursion Solution | |
| **/ | |
| class Solution { | |
| public int calculate(String s) { | |
| return cal(s, new int[]{0}); | |
| } | |
| private int cal(String s, int[] p) { | |
| int res = 0; |
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 Solution { | |
| String[] ones = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; | |
| String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", | |
| "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; | |
| String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", | |
| "Seventy", "Eighty", "Ninety"}; | |
| String[] ks = {"", "Thousand", "Million", "Billion"}; | |
| public String numberToWords(int num) { | |
| if (num == 0) { |
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
| /** | |
| Recursion Solution | |
| **/ | |
| class Solution { | |
| public String countOfAtoms(String formula) { | |
| if (formula == null || formula.length() == 0) { | |
| return ""; | |
| } | |
| int len = formula.length(); | |
| Map<String, Integer> res = parse(formula, new int[]{len - 1}); |
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 Solution { | |
| public String solveEquation(String equation) { | |
| String[] tokens = equation.split("="); | |
| if (tokens.length != 2) { | |
| return "No solution"; | |
| } | |
| int[] left = parse(tokens[0]); | |
| int[] right = parse(tokens[1]); | |
| // move x to left, num to right |
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 Solution { | |
| public boolean isNumber(String s) { | |
| int idx = 0; | |
| boolean seenE = false; | |
| boolean seenDot = false; | |
| boolean seenSign = false; | |
| boolean seenNum = false; | |
| s = s.trim(); | |
| while(idx < 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
| class Solution { | |
| public double findMedianSortedArrays(int[] nums1, int[] nums2) { | |
| // search range solution | |
| int len = nums1.length + nums2.length; | |
| double mid = -1; | |
| if (len % 2 == 1) { | |
| mid = findKth(nums1, nums2, (len + 1) /2 ); | |
| } else { | |
| int mid1 = findKth(nums1, nums2, len / 2); | |
| int mid2 = findKth(nums1, nums2, len / 2 + 1); |
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 Solution { | |
| public int divide(int dividend, int divisor) { | |
| if (dividend == Integer.MIN_VALUE && divisor == -1) { | |
| return Integer.MAX_VALUE; | |
| } | |
| if (divisor == 1) { |
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 Solution { | |
| public int[] searchRange(int[] nums, int target) { | |
| if (nums == null || nums.length == 0) { | |
| return new int[]{-1, -1}; | |
| } | |
| int l = findFirst(nums, target); | |
| int r = findLast(nums, target); | |
| return new int[]{l, r}; | |
| } |
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 Solution { | |
| public int findMin(int[] nums) { | |
| int l = 0; | |
| int r = nums.length - 1; | |
| while(l < r) { | |
| int mid = l + (r - l) / 2; | |
| if (nums[mid] > nums[r]) { | |
| l = mid + 1; | |
| } else { |
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 Solution { | |
| public int findPeakElement(int[] nums) { | |
| int l = 0; | |
| int r = nums.length - 1; | |
| while(l < r) { | |
| int mid = l + (r - l) / 2; | |
| int next = mid + 1; // safe to use mid + 1 | |
| if (nums[mid] < nums[next]) { | |
| l = mid + 1; |