Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2121604

Browse files
[LEET-1111] add more problem descriptions
1 parente8cb699 commit2121604

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
lines changed

‎leetcode-algorithms/src/main/java/com/stevesun/solutions/Flatten2DVector.java

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,58 @@
55
importjava.util.List;
66
importjava.util.Queue;
77

8+
/**Implement an iterator to flatten a 2d vector.
9+
10+
For example,
11+
Given 2d vector =
12+
13+
[
14+
[1,2],
15+
[3],
16+
[4,5,6]
17+
]
18+
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,2,3,4,5,6].
19+
20+
Hint:
21+
22+
How many variables do you need to keep track?
23+
Two variables is all you need. Try with x and y.
24+
Beware of empty rows. It could be the first few rows.
25+
To write correct code, think about the invariant to maintain. What is it?
26+
The invariant is x and y must always point to a valid point in the 2d vector. Should you maintain your invariant ahead of time or right when you need it?
27+
Not sure? Think about how you would implement hasNext(). Which is more complex?
28+
Common logic in two different places should be refactored into a common method.
29+
30+
Follow up:
31+
As an added challenge, try to code it using only iterators in C++ or iterators in Java.*/
32+
833
publicclassFlatten2DVector {
934

10-
}
11-
12-
classVector2DimplementsIterator<Integer> {
13-
privateQueue<Integer>cache;
14-
privateList<List<Integer>>vec2d;
15-
16-
publicVector2D(List<List<Integer>>vec2d) {
17-
this.vec2d =vec2d;
18-
this.cache =newLinkedList<Integer>();
19-
if(vec2d !=null &&vec2d.size() >0){
20-
for(List<Integer>list :vec2d){
21-
for(inti :list){
22-
cache.offer(i);
35+
classVector2DimplementsIterator<Integer> {
36+
privateQueue<Integer>cache;
37+
privateList<List<Integer>>vec2d;
38+
39+
publicVector2D(List<List<Integer>>vec2d) {
40+
this.vec2d =vec2d;
41+
this.cache =newLinkedList<Integer>();
42+
if (vec2d !=null &&vec2d.size() >0) {
43+
for (List<Integer>list :vec2d) {
44+
for (inti :list) {
45+
cache.offer(i);
46+
}
2347
}
2448
}
2549
}
26-
}
27-
28-
@Override
29-
publicIntegernext() {
30-
returncache.poll();
31-
}
3250

33-
@Override
34-
publicbooleanhasNext() {
35-
return !cache.isEmpty();
51+
@Override
52+
publicIntegernext() {
53+
returncache.poll();
54+
}
55+
56+
@Override
57+
publicbooleanhasNext() {
58+
return !cache.isEmpty();
59+
}
3660
}
37-
}
3861

62+
}

‎leetcode-algorithms/src/main/java/com/stevesun/solutions/InorderSuccessorInBST.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ public class InorderSuccessorInBST {
1212
//Amazed at this solution: https://discuss.leetcode.com/topic/25698/java-python-solution-o-h-time-and-o-1-space-iterative
1313
//I used brute force: traverse the tree and store and nodes in a list, then traverse the list to compare with p to return the successor if there is.
1414
publicTreeNodeinorderSuccessor(TreeNoderoot,TreeNodep) {
15-
TreeNodesucc =null;
15+
TreeNodesuccessor =null;
1616
while(root !=null){
1717
if(p.val <root.val){
18-
succ =root;
18+
successor =root;
1919
root =root.left;
2020
}else {
2121
root =root.right;
2222
}
2323
}
24-
returnsucc;
24+
returnsuccessor;
2525
}
2626

2727
}

‎leetcode-algorithms/src/main/java/com/stevesun/solutions/OneEditDistance.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
packagecom.stevesun.solutions;
22

3+
/**Given two strings S and T, determine if they are both one edit distance apart.*/
34
publicclassOneEditDistance {
45

56
publicstaticbooleanisOneEditDistance(Strings,Stringt) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp