GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — update table order, add some easy questions · awangdev/leet-code@fd5d498

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .gitignore
  • GenerateCodeTable.java
  • Java
    • 2 Sum.java
    • Plus One.java
    • Power of Three.java
    • Power of Two.java
    • Reverse Vowels of a String.java
    • Valid Palindrome.java
  • KnowledgeHash2.md
  • README.md
  • ReviewPage.md
  • WordPress.txt
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.gitignore‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
GenerateCodeTable.class
2+
GenerateCodeTable$TableRow.class
Collapse file

‎GenerateCodeTable.java‎

Copy file name to clipboardExpand all lines: GenerateCodeTable.java
+43-5Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import java.io.*;
2+
import java.util.*;
23
/*
34
Used to generate table of contents.
45
- No args: generate GitHub table
@@ -7,6 +8,26 @@
78
- args == 'all', genereate both table
89
*/
910
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+
1031
public final static String TUTORIAL_KEY_WORD = "tutorial:";
1132
public static void main(String[] args) {
1233
//Read Java Solution Folder
@@ -119,30 +140,46 @@ public static String generateREADME(File[] listOfFiles) {
119140
"| Squence | Problem | Level | Language | Video Tutorial|\n" +
120141
"|:-------:|:--------------|:------:|:---------:|:-------------:|\n";
121142
int count = 0;
143+
final ArrayList<TableRow> tableRows = new ArrayList<>();
122144
for (File file : listOfFiles) {
123145
String tutorialLink = "";
124146
String calculatedLevel = "";
147+
long timestamp = 0;
125148
if (file.getName().contains(".java")) {
126149
try {
127150
final BufferedReader reader = new BufferedReader(new InputStreamReader(
128151
new FileInputStream("Java/" + file.getName()), "UTF-8"));
152+
// Get level
129153
final String levelLine = reader.readLine().trim();
130-
if (levelLine.length() == 1) {
154+
if (levelLine.length() == 1 && calculateLevel(levelLine.toUpperCase()).length() == 1) {
131155
calculatedLevel = calculateLevel(levelLine.toUpperCase());
132156
}
157+
// Get timestamp
158+
final String timestampLine = reader.readLine();
159+
if (timestampLine.length() != 0) {
160+
timestamp = Long.parseLong(timestampLine);
161+
}
162+
163+
// Get tutorial
133164
final String tutorialLine = reader.readLine();
134165
if (tutorialLine.indexOf(TUTORIAL_KEY_WORD) == 0) {
135166
tutorialLink = "[Link](" + tutorialLine.substring(TUTORIAL_KEY_WORD.length()) + ")";
136167
}
137-
} catch (Exception e) {
168+
} catch (final Exception e) {
138169
System.err.format("IOException: %s%n", e);
139170
}
140171
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/"
142173
+ convertedFileName + ")|" + calculatedLevel + "|" + "Java|" + tutorialLink + "|\n";
174+
final TableRow tableRow = new TableRow(timestamp, composedLine);
175+
tableRows.add(tableRow);
143176
count++;
144177
}
145-
}
178+
}
179+
Collections.sort(tableRows, Comparator.comparing(TableRow::getDate));
180+
for (TableRow row : tableRows) {
181+
outputContent += row.getRowContext();
182+
}
146183
return outputContent;
147184
}
148185

@@ -170,6 +207,7 @@ public static String generateReviewPage(File[] listOfFiles) {
170207
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("Java/" + file.getName()), "UTF-8"));
171208
String line = null;
172209
int countLine = 0;
210+
// Reading ends upon first '```'
173211
while ((line = reader.readLine()) != null && !line.equals("```")) {
174212
if (countLine == 0) {
175213
final String trimedLine = line.trim().toUpperCase();
@@ -189,7 +227,7 @@ public static String generateReviewPage(File[] listOfFiles) {
189227
System.err.format("IOException: %s%n", e);
190228
}//end of one file
191229
outputContent += "\n---\n";
192-
count++;
230+
count++;
193231
}
194232
}
195233
return outputContent;
Collapse file

‎Java/2 Sum.java‎

Copy file name to clipboardExpand all lines: Java/2 Sum.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
E
2+
23
tutorial:https://www.youtube.com/watch?v=P8zBxoVY1oI&feature=youtu.be
34

45
解法1:相对暴力简洁, HashMap<value, index>,找到一个value, 存一个; 若在HashMap里面 match 到结果, 就return HashMap里存的index. O(n) space && time.
Collapse file

‎Java/Plus One.java‎

Copy file name to clipboardExpand all lines: Java/Plus One.java
+36-1Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
E
2+
1516342836
3+
4+
简单的实现, 加1, 进位. 唯一取巧的地方, 最后如果要多一位, 一定是10000...这个模式, 可以走捷径, 直接来个+1size的array, 然后第一位=1.
5+
注意,转换成long也不合理,用太多memory.
6+
```
17
/*
28
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.
410
The digits are stored such that the most significant digit is at the head of the list.
511
612
@@ -13,6 +19,33 @@
1319
Array
1420
1521
*/
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+
}
1649

1750
public class Solution {
1851
public int[] plusOne(int[] digits) {
@@ -93,3 +126,5 @@ public int[] plusOne(int[] digits) {
93126
}
94127

95128

129+
130+
```
Collapse file

‎Java/Power of Three.java‎

Copy file name to clipboardExpand all lines: Java/Power of Three.java
+56-1Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
E
2+
1516341495
23

4+
方法1:
35
Power of 3: 3 ^ x == n ?
6+
意思是 n / 3 一直除, 最后是可以等于1的, 那么就有了 n/=3, check n%3, 最后看结果是不是整除到1的做法. 用while loop.
47

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整除. 一步到位.
613

714
```
815
/*
@@ -22,6 +29,33 @@ Hide Similar Problems (E) Power of Two
2229
2330
*/
2431

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+
2559
//Check if n = 3 ^ x;
2660
public class Solution {
2761
public boolean isPowerOfThree(int n) {
@@ -38,5 +72,26 @@ public boolean isPowerOfThree(int n) {
3872
}
3973
}
4074

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+
}
4196

4297
```
Collapse file

‎Java/Power of Two.java‎

Copy file name to clipboard
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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

‎Java/Reverse Vowels of a String.java‎

Copy file name to clipboard
+55Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

‎Java/Valid Palindrome.java‎

Copy file name to clipboardExpand all lines: Java/Valid Palindrome.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
E
2+
23
tutorial:https://www.youtube.com/watch?v=2hNK0Yz53LQ&list=PLZn-UvluQZuNedn1hDzTmNLE8MQWXjKVb
34

45
过滤alphanumeric,其他字母掠过
Collapse file

‎KnowledgeHash2.md‎

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 numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ Binary Search
5757

5858
Sort
5959

60+
### Collections
61+
62+
#### methods
63+
Collections.sort()
64+
65+
#### ArrayList
66+
new ArrayList(Arrays.asList([1,2,3]))
6067

6168

6269
### Hash

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.