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 lengthOfLongestSubstringKDistinct(String s, int k) { | |
| Map<Character, Integer> map = new HashMap<>(); | |
| if (s == null || s.length() == 0 || k == 0) return 0; | |
| int start = 0; | |
| int longest = 0; | |
| for (int i = 0; i < s.length(); i++) { | |
| if (map.containsKey(s.charAt(i)) || map.size() < k) { | |
| map.put(s.charAt(i), i); | |
| } else { | |
| int minPos = getMinPos(map); |
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 boolean hasGroupsSizeX(int[] deck) { | |
| if (deck == null || deck.length <= 1) return false; | |
| Map<Integer, Integer> map = new HashMap<>(); | |
| for (int i = 0; i < deck.length; i++) { | |
| int freq = map.getOrDefault(deck[i], 0); | |
| map.put(deck[i], freq + 1); | |
| } | |
| int n = 0; List<Integer> lcf = new ArrayList<>(); | |
| for (Map.Entry<Integer, Integer> entry : map.entrySet()) { | |
| if (n == 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 { | |
| public int superpalindromesInRange(String L, String R) { | |
| Long l = (long)Math.sqrt(Long.parseLong(L)); | |
| Long r = (long)Math.sqrt(Long.parseLong(R)); | |
| int re = 0; | |
| for(int odd = 0;odd <= 1;odd++){ | |
| long core = 1; | |
| long num = getP(core,odd == 0); | |
| while(num <= r){ | |
| if(num >= l){ |
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
| You will be supplied with two data files in CSV format . | |
| The first file contains statistics about various dinosaurs. The second file contains additional data. | |
| Given the following formula, `speed = ((STRIDE_LENGTH / LEG_LENGTH) - 1) * SQRT(LEG_LENGTH * g)` | |
| Where g = 9.8 m/s^2 (gravitational constant) | |
| Write a program to read in the data files from disk, it must then print the names of only the bipedal dinosaurs from fastest to slowest. | |
| Do not print any other information. | |
| $ cat dataset1.csv | |
| NAME,LEG_LENGTH,DIET |
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
| // store all the flower position to a TreeSet, each time a flower bloom, we check the distance of this flower to | |
| // two closest flower is k or not, return the first data that satisfy the condition. | |
| // TreeSet: Time = O(NlogN) Space = O(N) | |
| public int kEmptySlots(int[] flowers, int k) { | |
| if (flowers == null || k > flowers.length - 2) return -1; | |
| int N = flowers.length; | |
| TreeSet<Integer> set = new TreeSet<>(); | |
| for (int i = 0; i < N; i++) { | |
| set.add(flowers[i]); // include right and exclude left | |
| Integer lower = set.lower(flowers[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 int numDecodings(String s) { | |
| if (s == null || s.length() == 0) return 0; | |
| int[] result = new int[]{0}; | |
| dfs(s, 0, 1, result); | |
| return result[0]; | |
| } | |
| private void dfs(String s, int i, int count, int[] result) { | |
| if (i == s.length()) { | |
| count = count % 1000000007; | |
| result[0] = (result[0] + count) % 1000000007; |
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
| bhh |
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 orderOfLargestPlusSign(int N, int[][] mines) { | |
| int[][] matrix = new int[N][N]; | |
| for (int i = 0; i < N; i++) { | |
| Arrays.fill(matrix[i], 1); | |
| } | |
| for (int[] pair : mines) { | |
| matrix[pair[0]][pair[1]] = 0; | |
| } | |
| // calculate number of consecutive 1's in four directions: up, down, left, right | |
| int[][] left = new int[N][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
| public int minFactory(int[] array, int range) { | |
| int i = 0; | |
| int count = 0; | |
| int start = 0; | |
| while (i < array.length && i <= range) { | |
| if (array[i] == 1) { | |
| start = i; | |
| } | |
| 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
| /** | |
| 有一个数组,里面是26个字母,顺序是乱的,有个API叫做 swich_with_a(int pos), 这个方法的作用是调换pos位置上的字母和字母”a“的位置 | |
| (无论”a”在哪里,这个方法会把“a“换到pos的位置)。让你实现把这26个字母排序,但是只能用到这个swith_with_a来调换位置。 | |
| 遇到一个char[i]有两个思路:1. 把char[i]换到出去,到它应该在的位置上去,但这样不能让char[i]本身是对的,走完一遍后结果还是不对 | |
| 2. 不管char[i]是什么,我只要把字母i + 'a'换进来,换到位置i上就行,这样就保证了那个位置的字母一定是对的 | |
| */ | |
| public char[] sortChar(char[] array) { | |
| // record each character's index | |
| int[] index = new int[26]; |
NewerOlder