GeezerVison is showing github.com — update table order, add some easy questions · awangdev/leet-code@fd5d498
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 clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | GenerateCodeTable.class | | 2 | +GenerateCodeTable$TableRow.class |
Collapse file Copy file name to clipboardExpand all lines: GenerateCodeTable.java +43-5Lines changed: 43 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import java.io.*; | | 2 | +import java.util.*; | 2 | 3 | /* | 3 | 4 | Used to generate table of contents. | 4 | 5 | - No args: generate GitHub table | | 7 | 8 | - args == 'all', genereate both table | 8 | 9 | */ | 9 | 10 | public class GenerateCodeTable { | | 11 | + /* | | 12 | + TableRow, used to hold table object and sort by date. | | 13 | + */ | | 14 | + private static class TableRow { | | 15 | + private long date; | | 16 | + private String rowContext; | | 17 | + public TableRow(long date, String rowContext) { | | 18 | + this.date = date; | | 19 | + this.rowContext = rowContext; | | 20 | + } | | 21 | + | | 22 | + public long getDate() { | | 23 | + return this.date; | | 24 | + } | | 25 | + | | 26 | + public String getRowContext() { | | 27 | + return this.rowContext; | | 28 | + } | | 29 | + } | | 30 | + | 10 | 31 | public final static String TUTORIAL_KEY_WORD = "tutorial:"; | 11 | 32 | public static void main(String[] args) { | 12 | 33 | //Read Java Solution Folder | @@ -119,30 +140,46 @@ public static String generateREADME(File[] listOfFiles) { | 119 | 140 | "| Squence | Problem | Level | Language | Video Tutorial|\n" + | 120 | 141 | "|:-------:|:--------------|:------:|:---------:|:-------------:|\n"; | 121 | 142 | int count = 0; | | 143 | + final ArrayList<TableRow> tableRows = new ArrayList<>(); | 122 | 144 | for (File file : listOfFiles) { | 123 | 145 | String tutorialLink = ""; | 124 | 146 | String calculatedLevel = ""; | | 147 | + long timestamp = 0; | 125 | 148 | if (file.getName().contains(".java")) { | 126 | 149 | try { | 127 | 150 | final BufferedReader reader = new BufferedReader(new InputStreamReader( | 128 | 151 | new FileInputStream("Java/" + file.getName()), "UTF-8")); | | 152 | + // Get level | 129 | 153 | final String levelLine = reader.readLine().trim(); | 130 | | - if (levelLine.length() == 1) { | | 154 | + if (levelLine.length() == 1 && calculateLevel(levelLine.toUpperCase()).length() == 1) { | 131 | 155 | calculatedLevel = calculateLevel(levelLine.toUpperCase()); | 132 | 156 | } | | 157 | + // Get timestamp | | 158 | + final String timestampLine = reader.readLine(); | | 159 | + if (timestampLine.length() != 0) { | | 160 | + timestamp = Long.parseLong(timestampLine); | | 161 | + } | | 162 | + | | 163 | + // Get tutorial | 133 | 164 | final String tutorialLine = reader.readLine(); | 134 | 165 | if (tutorialLine.indexOf(TUTORIAL_KEY_WORD) == 0) { | 135 | 166 | tutorialLink = "[Link](" + tutorialLine.substring(TUTORIAL_KEY_WORD.length()) + ")"; | 136 | 167 | } | 137 | | - } catch (Exception e) { | | 168 | + } catch (final Exception e) { | 138 | 169 | System.err.format("IOException: %s%n", e); | 139 | 170 | } | 140 | 171 | String convertedFileName = file.getName().replace(" ", "%20"); | 141 | | - outputContent += "|" + count + "|[" + file.getName() + "](https://github.com/awangdev/LintCode/blob/master/Java/" | | 172 | + String composedLine = "|" + count + "|[" + file.getName() + "](https://github.com/awangdev/LintCode/blob/master/Java/" | 142 | 173 | + convertedFileName + ")|" + calculatedLevel + "|" + "Java|" + tutorialLink + "|\n"; | | 174 | + final TableRow tableRow = new TableRow(timestamp, composedLine); | | 175 | + tableRows.add(tableRow); | 143 | 176 | count++; | 144 | 177 | } | 145 | | - } | | 178 | + } | | 179 | + Collections.sort(tableRows, Comparator.comparing(TableRow::getDate)); | | 180 | + for (TableRow row : tableRows) { | | 181 | + outputContent += row.getRowContext(); | | 182 | + } | 146 | 183 | return outputContent; | 147 | 184 | } | 148 | 185 |
| @@ -170,6 +207,7 @@ public static String generateReviewPage(File[] listOfFiles) { | 170 | 207 | BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("Java/" + file.getName()), "UTF-8")); | 171 | 208 | String line = null; | 172 | 209 | int countLine = 0; | | 210 | + // Reading ends upon first '```' | 173 | 211 | while ((line = reader.readLine()) != null && !line.equals("```")) { | 174 | 212 | if (countLine == 0) { | 175 | 213 | final String trimedLine = line.trim().toUpperCase(); | @@ -189,7 +227,7 @@ public static String generateReviewPage(File[] listOfFiles) { | 189 | 227 | System.err.format("IOException: %s%n", e); | 190 | 228 | }//end of one file | 191 | 229 | outputContent += "\n---\n"; | 192 | | - count++; | | 230 | + count++; | 193 | 231 | } | 194 | 232 | } | 195 | 233 | return outputContent; | |
Collapse file Copy file name to clipboardExpand all lines: Java/2 Sum.java +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | E | | 2 | + | 2 | 3 | tutorial:https://www.youtube.com/watch?v=P8zBxoVY1oI&feature=youtu.be | 3 | 4 |
| 4 | 5 | 解法1:相对暴力简洁, HashMap<value, index>,找到一个value, 存一个; 若在HashMap里面 match 到结果, 就return HashMap里存的index. O(n) space && time. | |
Collapse file Copy file name to clipboardExpand all lines: Java/Plus One.java +36-1Lines changed: 36 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +E | | 2 | +1516342836 | | 3 | + | | 4 | +简单的实现, 加1, 进位. 唯一取巧的地方, 最后如果要多一位, 一定是10000...这个模式, 可以走捷径, 直接来个+1size的array, 然后第一位=1. | | 5 | +注意,转换成long也不合理,用太多memory. | | 6 | +``` | 1 | 7 | /* | 2 | 8 | Given a non-negative number represented as an array of digits, plus one to the number. | 3 | | -
| | 9 | +You may assume the integer do not contain any leading zero, except the number 0 itself. | 4 | 10 | The digits are stored such that the most significant digit is at the head of the list. | 5 | 11 |
| 6 | 12 |
| | 13 | 19 | Array | 14 | 20 |
| 15 | 21 | */ | | 22 | +/** | | 23 | +Thoughts: | | 24 | +Loop over all digit and see if it advanced into next position. If not, return digits. | | 25 | +Check if last position if over 10, if so, create a new array and set last pos = 1. | | 26 | + */ | | 27 | +public class Solution { | | 28 | + public int[] plusOne(int[] digits) { | | 29 | + if(digits.length==0) return digits; | | 30 | + digits[digits.length-1] += 1; | | 31 | + //Check index digit.length-1 to 1 | | 32 | + for(int i = digits.length-1; i>0; i--){ | | 33 | + if(digits[i] == 10){ | | 34 | + digits[i]=0; | | 35 | + digits[i-1]+=1; | | 36 | + } | | 37 | + else return digits; | | 38 | + } | | 39 | + | | 40 | + //Check index 0. If ==0, set it to 0 and carry over 1 | | 41 | + if(digits[0]==10){ | | 42 | + int[] output = new int[digits.length+1]; | | 43 | + output[0] = 1; | | 44 | + return output; | | 45 | + } | | 46 | + else return digits; | | 47 | + } | | 48 | +} | 16 | 49 |
| 17 | 50 | public class Solution { | 18 | 51 | public int[] plusOne(int[] digits) { | @@ -93,3 +126,5 @@ public int[] plusOne(int[] digits) { | 93 | 126 | } | 94 | 127 |
| 95 | 128 |
| | 129 | + | | 130 | +``` |
Collapse file Copy file name to clipboardExpand all lines: Java/Power of Three.java +56-1Lines changed: 56 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | E | | 2 | +1516341495 | 2 | 3 |
| | 4 | +方法1: | 3 | 5 | Power of 3: 3 ^ x == n ? | | 6 | +意思是 n / 3 一直除, 最后是可以等于1的, 那么就有了 n/=3, check n%3, 最后看结果是不是整除到1的做法. 用while loop. | 4 | 7 |
| 5 | | -做出发. 查%. | | 8 | +方法2: | | 9 | +如果n是power of 3, 那么 3 ^ x的这个 x一定是个比n小的数字. 那么可以在 0 ~ n 之间做binary serach, 但是就比较慢. | | 10 | + | | 11 | +方法3: | | 12 | +巧妙的想法.最大的3^x integer是 3^19. 那么找到这个数, 一定可以被n整除. 一步到位. | 6 | 13 |
| 7 | 14 | ``` | 8 | 15 | /* | @@ -22,6 +29,33 @@ Hide Similar Problems (E) Power of Two | 22 | 29 |
| 23 | 30 | */ | 24 | 31 |
| | 32 | +/* | | 33 | +Thoughts: | | 34 | +Use binary serach: pick an item, do 3^x and see if equals to n, < n or >n, then move the number. | | 35 | +*/ | | 36 | +class Solution { | | 37 | + public boolean isPowerOfThree(int n) { | | 38 | + if (n <= 0) { | | 39 | + return false; | | 40 | + } | | 41 | + int start = 0; | | 42 | + int end = n; | | 43 | + while (start + 1 < end) { | | 44 | + int mid = start + (end - start)/2; | | 45 | + long powerOfThree = (long) Math.pow(3, mid); | | 46 | + if (powerOfThree == n) { | | 47 | + return true; | | 48 | + } | | 49 | + if (powerOfThree < n) { | | 50 | + start = mid; | | 51 | + } else { | | 52 | + end = mid; | | 53 | + } | | 54 | + } | | 55 | + return Math.pow(3, start) == n; | | 56 | + } | | 57 | +} | | 58 | + | 25 | 59 | //Check if n = 3 ^ x; | 26 | 60 | public class Solution { | 27 | 61 | public boolean isPowerOfThree(int n) { | @@ -38,5 +72,26 @@ public boolean isPowerOfThree(int n) { | 38 | 72 | } | 39 | 73 | } | 40 | 74 |
| | 75 | +// Shorter version | | 76 | +class Solution { | | 77 | + public boolean isPowerOfThree(int n) { | | 78 | + while (n/3>=1){ | | 79 | + if (n%3!=0) return false; | | 80 | + n/=3; | | 81 | + } | | 82 | + return n==1; | | 83 | + } | | 84 | +} | | 85 | + | | 86 | +/* | | 87 | +Thoughts: | | 88 | +The largest int is 2^31, and the largest 3^x = 3^19. | | 89 | +If n is power of 3, it should 3^19 % n should == 0 | | 90 | +*/ | | 91 | +class Solution { | | 92 | + public boolean isPowerOfThree(int n) { | | 93 | + return (n > 0 && Math.pow(3, 19) % n == 0); | | 94 | + } | | 95 | +} | 41 | 96 |
| 42 | 97 | ``` |
Collapse file Copy file name to clipboard +28Lines changed: 28 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +E | | 2 | +1516343302 | | 3 | + | | 4 | +跟powerOfThree一样: 可以loop, check mod; 也可以用binary search找合适的数字. | | 5 | + | | 6 | +``` | | 7 | +/* | | 8 | +Given an integer, write a function to determine if it is a power of two. | | 9 | +*/ | | 10 | + | | 11 | +/* | | 12 | +Similar to powerOfThree. Two ways: 1 divide and check mode; 2. binary search | | 13 | +*/ | | 14 | +class Solution { | | 15 | + public boolean isPowerOfTwo(int n) { | | 16 | + if (n <= 0) { | | 17 | + return false; | | 18 | + } | | 19 | + while (n != 1) { | | 20 | + if (n % 2 != 0 || n / 2 == 0) { | | 21 | + return false; | | 22 | + } | | 23 | + n/=2; | | 24 | + } | | 25 | + return true; | | 26 | + } | | 27 | +} | | 28 | +``` |
Collapse file Copy file name to clipboard +55Lines changed: 55 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +E | | 2 | +1516344636 | | 3 | + | | 4 | +方法1: two pointer. 还没有做 | | 5 | + | | 6 | +方法2: 拿出所有vowels, 反过来放进去. O(n) | | 7 | + | | 8 | +``` | | 9 | +/** | | 10 | +Write a function that takes a string as input and reverse only the vowels of a string. | | 11 | +
| | 12 | +Example 1: | | 13 | +Given s = "hello", return "holle". | | 14 | +
| | 15 | +Example 2: | | 16 | +Given s = "leetcode", return "leotcede". | | 17 | +
| | 18 | +Note: | | 19 | +The vowels does not include the letter "y". | | 20 | +
| | 21 | + */ | | 22 | + | | 23 | + /* | | 24 | +Thoughts: | | 25 | +vowels: a e i o u A E I O U | | 26 | +HashMap, store the <pos, letter> and put them back in reverse order. | | 27 | +*/ | | 28 | +class Solution { | | 29 | + | | 30 | + public String reverseVowels(String s) { | | 31 | + if (s == null || s.length() == 0) { | | 32 | + return s; | | 33 | + } | | 34 | + final List<Character> vowels = Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'); | | 35 | + final List<Character> matches = new ArrayList<>(); | | 36 | + for (int i = 0; i < s.length(); i++) { | | 37 | + if (vowels.contains(s.charAt(i))) { | | 38 | + matches.add(s.charAt(i)); | | 39 | + } | | 40 | + } | | 41 | + final StringBuffer sb = new StringBuffer(); | | 42 | + for (int i = 0; i < s.length(); i++) { | | 43 | + char letter = s.charAt(i); | | 44 | + if (vowels.contains(letter)) { | | 45 | + int lastMatchIndex = matches.size() - 1; | | 46 | + sb.append(matches.get(lastMatchIndex)); | | 47 | + matches.remove(lastMatchIndex); | | 48 | + } else { | | 49 | + sb.append(letter); | | 50 | + } | | 51 | + } | | 52 | + return sb.toString(); | | 53 | + } | | 54 | +} | | 55 | +``` |
Collapse file Copy file name to clipboardExpand all lines: Java/Valid Palindrome.java +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | E | | 2 | + | 2 | 3 | tutorial:https://www.youtube.com/watch?v=2hNK0Yz53LQ&list=PLZn-UvluQZuNedn1hDzTmNLE8MQWXjKVb | 3 | 4 |
| 4 | 5 | 过滤alphanumeric,其他字母掠过 | |
Collapse file Copy file name to clipboardExpand all lines: KnowledgeHash2.md +7Lines changed: 7 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -57,6 +57,13 @@ Binary Search | 57 | 57 |
| 58 | 58 | Sort | 59 | 59 |
| | 60 | +### Collections | | 61 | + | | 62 | +#### methods | | 63 | +Collections.sort() | | 64 | + | | 65 | +#### ArrayList | | 66 | +new ArrayList(Arrays.asList([1,2,3])) | 60 | 67 |
| 61 | 68 |
| 62 | 69 | ### Hash | |
|