Skip to content

Commit 548cf66

Browse files
committed
feat: update solutions to lcof problem
1 parent 2f6a9fb commit 548cf66

File tree

10 files changed

+104
-143
lines changed

10 files changed

+104
-143
lines changed

lcof/面试题16. 数值的整数次方/README.md

+8-14
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,15 @@
3939

4040
```python
4141
class Solution:
42-
cache = {}
4342
def myPow(self, x: float, n: int) -> float:
44-
self.cache = {}
45-
return self.pow(x, n)
46-
47-
def pow(self, x, n):
48-
if self.cache.get('{}-{}'.format(x, n)):
49-
return self.cache['{}-{}'.format(x, n)]
50-
if n == 0: return 1
51-
if n == 1: return x
52-
if n == -1: return 1 / x
53-
54-
half = self.pow(x, n // 2)
55-
self.cache['{}-{}'.format(x, n // 2)] = half
56-
return half * half * self.pow(x, n % 2)
43+
if n == 0:
44+
return 1
45+
if n == 1:
46+
return x
47+
if n == -1:
48+
return 1 / x
49+
half = self.myPow(x, n // 2)
50+
return half * half * self.myPow(x, n % 2)
5751
```
5852

5953
### **Java**

lcof/面试题16. 数值的整数次方/Solution.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
class Solution {
22
public double myPow(double x, int n) {
3-
if (n == 0)
4-
return 1;
5-
if (n == 1)
6-
return x;
7-
if (n == -1)
8-
return 1 / x;
3+
if (n == 0) return 1;
4+
if (n == 1) return x;
5+
if (n == -1) return 1 / x;
96
double half = myPow(x, n / 2);
107
return half * half * myPow(x, n % 2);
118
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
class Solution:
2-
cache = {}
32
def myPow(self, x: float, n: int) -> float:
4-
self.cache = {}
5-
return self.pow(x, n)
6-
7-
def pow(self, x, n):
8-
if self.cache.get('{}-{}'.format(x, n)):
9-
return self.cache['{}-{}'.format(x, n)]
10-
if n == 0: return 1
11-
if n == 1: return x
12-
if n == -1: return 1 / x
13-
14-
half = self.pow(x, n // 2)
15-
self.cache['{}-{}'.format(x, n // 2)] = half
16-
return half * half * self.pow(x, n % 2)
3+
if n == 0:
4+
return 1
5+
if n == 1:
6+
return x
7+
if n == -1:
8+
return 1 / x
9+
half = self.myPow(x, n // 2)
10+
return half * half * self.myPow(x, n % 2)

lcof/面试题30. 包含min函数的栈/README.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ class MinStack:
3434
"""
3535
initialize your data structure here.
3636
"""
37-
self._s1 = []
38-
self._s2 = []
37+
self.s1 = []
38+
self.s2 = []
3939

4040
def push(self, x: int) -> None:
41-
self._s1.append(x)
42-
self._s2.append(x if len(self._s2) == 0 or self._s2[-1] > x else self._s2[-1])
41+
self.s1.append(x)
42+
self.s2.append(x if not self.s2 or self.s2[-1] >= x else self.s2[-1])
4343

4444
def pop(self) -> None:
45-
self._s1.pop()
46-
self._s2.pop()
45+
self.s1.pop()
46+
self.s2.pop()
4747

4848
def top(self) -> int:
49-
return self._s1[-1]
49+
return self.s1[-1]
5050

5151
def min(self) -> int:
52-
return self._s2[-1]
52+
return self.s2[-1]
5353

5454

5555
# Your MinStack object will be instantiated and called as such:
@@ -64,29 +64,29 @@ class MinStack:
6464

6565
```java
6666
class MinStack {
67-
private Stack<Integer> s1;
68-
private Stack<Integer> s2;
67+
private Deque<Integer> s1;
68+
private Deque<Integer> s2;
6969

7070
/** initialize your data structure here. */
7171
public MinStack() {
72-
s1 = new Stack<>();
73-
s2 = new Stack<>();
72+
s1 = new ArrayDeque<>();
73+
s2 = new ArrayDeque<>();
7474
}
75-
75+
7676
public void push(int x) {
7777
s1.push(x);
78-
s2.push((s2.empty() || s2.peek() > x) ? x : s2.peek());
78+
s2.push(s2.isEmpty() || s2.peek() >= x ? x : s2.peek());
7979
}
80-
80+
8181
public void pop() {
8282
s1.pop();
8383
s2.pop();
8484
}
85-
85+
8686
public int top() {
8787
return s1.peek();
8888
}
89-
89+
9090
public int min() {
9191
return s2.peek();
9292
}

lcof/面试题30. 包含min函数的栈/Solution.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class MinStack {
2-
private Stack<Integer> s1;
3-
private Stack<Integer> s2;
4-
2+
private Deque<Integer> s1;
3+
private Deque<Integer> s2;
4+
55
/** initialize your data structure here. */
66
public MinStack() {
7-
s1 = new Stack<>();
8-
s2 = new Stack<>();
7+
s1 = new ArrayDeque<>();
8+
s2 = new ArrayDeque<>();
99
}
1010

1111
public void push(int x) {
1212
s1.push(x);
13-
s2.push((s2.empty() || s2.peek() > x) ? x : s2.peek());
13+
s2.push(s2.isEmpty() || s2.peek() >= x ? x : s2.peek());
1414
}
1515

1616
public void pop() {

lcof/面试题30. 包含min函数的栈/Solution.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ def __init__(self):
44
"""
55
initialize your data structure here.
66
"""
7-
self._s1 = []
8-
self._s2 = []
7+
self.s1 = []
8+
self.s2 = []
99

1010
def push(self, x: int) -> None:
11-
self._s1.append(x)
12-
self._s2.append(x if len(self._s2) == 0 or self._s2[-1] > x else self._s2[-1])
11+
self.s1.append(x)
12+
self.s2.append(x if not self.s2 or self.s2[-1] >= x else self.s2[-1])
1313

1414
def pop(self) -> None:
15-
self._s1.pop()
16-
self._s2.pop()
15+
self.s1.pop()
16+
self.s2.pop()
1717

1818
def top(self) -> int:
19-
return self._s1[-1]
19+
return self.s1[-1]
2020

2121
def min(self) -> int:
22-
return self._s2[-1]
22+
return self.s2[-1]
2323

2424

2525
# Your MinStack object will be instantiated and called as such:

lcof/面试题31. 栈的压入、弹出序列/README.md

+26-38
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,14 @@ push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
4545
```python
4646
class Solution:
4747
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
48-
t = []
49-
for num in popped:
50-
while len(t) == 0 or t[-1] != num:
51-
if len(pushed) == 0:
52-
return False
53-
t.append(pushed[0])
54-
pushed = pushed[1:]
55-
t.pop()
56-
return True
57-
48+
s = []
49+
q = 0
50+
for num in pushed:
51+
s.append(num)
52+
while s and s[-1] == popped[q]:
53+
s.pop()
54+
q += 1
55+
return not s
5856
```
5957

6058
### **Java**
@@ -64,18 +62,16 @@ class Solution:
6462
```java
6563
class Solution {
6664
public boolean validateStackSequences(int[] pushed, int[] popped) {
67-
Stack<Integer> t = new Stack<>();
68-
int i = 0, n = pushed.length;
69-
for (int num : popped) {
70-
while (t.empty() || t.peek() != num) {
71-
if (i == n) {
72-
return false;
73-
}
74-
t.push(pushed[i++]);
65+
Deque<Integer> s = new ArrayDeque<>();
66+
int q = 0;
67+
for (int num : pushed) {
68+
s.push(num);
69+
while (!s.isEmpty() && s.peek() == popped[q]) {
70+
s.pop();
71+
++q;
7572
}
76-
t.pop();
7773
}
78-
return true;
74+
return s.isEmpty();
7975
}
8076
}
8177
```
@@ -88,25 +84,17 @@ class Solution {
8884
* @param {number[]} popped
8985
* @return {boolean}
9086
*/
91-
var validateStackSequences = function (pushed, popped) {
92-
let stack = [];
93-
while (pushed.length && popped.length) {
94-
if (pushed[0] === popped[0]) {
95-
pushed.shift();
96-
popped.shift();
97-
} else if (popped[0] === stack[0]) {
98-
stack.shift();
99-
popped.shift();
100-
} else {
101-
stack.unshift(pushed.shift());
87+
var validateStackSequences = function(pushed, popped) {
88+
let s = [];
89+
let q = 0;
90+
for (let num of pushed) {
91+
s.push(num);
92+
while (s.length > 0 && s[s.length - 1] == popped[q]) {
93+
++q;
94+
s.pop();
95+
}
10296
}
103-
}
104-
while (stack.length) {
105-
if (stack[0] !== popped[0]) return false;
106-
stack.shift();
107-
popped.shift();
108-
}
109-
return true;
97+
return s.length == 0;
11098
};
11199
```
112100

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
class Solution {
22
public boolean validateStackSequences(int[] pushed, int[] popped) {
3-
Stack<Integer> t = new Stack<>();
4-
int i = 0, n = pushed.length;
5-
for (int num : popped) {
6-
while (t.empty() || t.peek() != num) {
7-
if (i == n) {
8-
return false;
9-
}
10-
t.push(pushed[i++]);
3+
Deque<Integer> s = new ArrayDeque<>();
4+
int q = 0;
5+
for (int num : pushed) {
6+
s.push(num);
7+
while (!s.isEmpty() && s.peek() == popped[q]) {
8+
s.pop();
9+
++q;
1110
}
12-
t.pop();
1311
}
14-
return true;
12+
return s.isEmpty();
1513
}
1614
}

lcof/面试题31. 栈的压入、弹出序列/Solution.js

+11-19
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@
33
* @param {number[]} popped
44
* @return {boolean}
55
*/
6-
var validateStackSequences = function (pushed, popped) {
7-
let stack = [];
8-
while (pushed.length && popped.length) {
9-
if (pushed[0] === popped[0]) {
10-
pushed.shift();
11-
popped.shift();
12-
} else if (popped[0] === stack[0]) {
13-
stack.shift();
14-
popped.shift();
15-
} else {
16-
stack.unshift(pushed.shift());
17-
}
6+
var validateStackSequences = function(pushed, popped) {
7+
let s = [];
8+
let q = 0;
9+
for (let num of pushed) {
10+
s.push(num);
11+
while (s.length > 0 && s[s.length - 1] == popped[q]) {
12+
++q;
13+
s.pop();
14+
}
1815
}
19-
while (stack.length) {
20-
if (stack[0] !== popped[0]) return false;
21-
stack.shift();
22-
popped.shift();
23-
}
24-
return true;
25-
};
16+
return s.length == 0;
17+
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
class Solution:
2-
def validateStackSequences(self, pushed: List[int],
3-
popped: List[int]) -> bool:
4-
t = []
5-
for num in popped:
6-
while len(t) == 0 or t[-1] != num:
7-
if len(pushed) == 0:
8-
return False
9-
t.append(pushed[0])
10-
pushed = pushed[1:]
11-
t.pop()
12-
return True
2+
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
3+
s = []
4+
q = 0
5+
for num in pushed:
6+
s.append(num)
7+
while s and s[-1] == popped[q]:
8+
s.pop()
9+
q += 1
10+
return not s

0 commit comments

Comments
 (0)