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 { | |
| private int getVal(char c) { | |
| switch (c) { | |
| case 'I': | |
| return 1; | |
| case 'V': | |
| return 5; | |
| case 'X': | |
| return 10; |
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 longestCommonPrefix(String[] strs) { | |
| StringBuilder prefix = new StringBuilder(); | |
| for (int i = 0; i < strs[0].length(); i++) { | |
| prefix.append(strs[0].charAt(i)); | |
| for (int j = 1; j < strs.length; j++) { | |
| if (i >= strs[j].length() || prefix.charAt(i) != strs[j].charAt(i)) { | |
| prefix.deleteCharAt(i); | |
| return prefix.toString(); | |
| } |
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 lengthOfLastWord(String s) { | |
| s = s.trim(); // 1. trim | |
| char[] arr = s.toCharArray(); | |
| int slow = 0; | |
| int fast = 0; | |
| while (fast < arr.length) { | |
| slow = fast; | |
| // skip the words | |
| while (fast < arr.length && arr[fast] != ' ') { |
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 reverseWords(String s) { | |
| s = s.trim(); | |
| String[] spl = s.split(" "); | |
| StringBuilder sb = new StringBuilder(); | |
| for (int i = spl.length - 1; i >= 0; i--) { | |
| if (!spl[i].trim().isEmpty()) { | |
| sb.append(spl[i]); | |
| if (i != 0) { | |
| sb.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
| class Solution { | |
| public List<List<Integer>> combine(int n, int k) { | |
| // 1 - n, contains | |
| List<List<Integer>> result = new LinkedList<>(); | |
| List<Integer> current = new LinkedList<>(); | |
| dfs(result, current, 1, n, k); | |
| return result; | |
| } | |
| private void dfs(List<List<Integer>> result, List<Integer> current, int start, int n, int k) { |
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
| private char transform(char c) { | |
| if (c >= 'A' && c <= 'Z') { | |
| return (char)(c + 32); | |
| } else if (c >= 'a' && c <= 'z' || c >= '0' && c <= '9') { | |
| return c; | |
| } else { | |
| return '!'; | |
| } | |
| } |
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 canCompleteCircuit(int[] gas, int[] cost) { | |
| // note: cost is from ith station to i+1 th station | |
| // find the point that gas is at the lowest, start from there | |
| int minGas = 0; | |
| int startPoint = 0; | |
| int currentGas = 0; | |
| for (int i = 0; i < gas.length; i++) { | |
| currentGas += (gas[i] - cost[i]); | |
| if (currentGas < minGas) { |
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 RandomizedSet { | |
| private int pos = 0; | |
| private Map<Integer, Integer> set = new HashMap<>(); | |
| private int[] vals = new int[200001]; | |
| public RandomizedSet() { | |
| } | |
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 { | |
| private void reverse(int[] arr) { | |
| int start = 0; | |
| int end = arr.length - 1; | |
| while (start < end) { | |
| int temp = arr[start]; | |
| arr[start] = arr[end]; | |
| arr[end] = temp; | |
| start++; |
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 jump(int[] nums) { | |
| // min steps of position n | |
| int[] dp = new int[nums.length]; | |
| dp[0] = 0; | |
| for (int i = 0; i < nums.length; i++) { | |
| int reachableDistance = nums[i]; | |
| for (int j = i + 1; j <= Math.min(nums.length - 1, i + reachableDistance); j++) { | |
| if (dp[j] == 0) { | |
| dp[j] = dp[i] + 1; |
NewerOlder