Skip to content

Commit 6cedebf

Browse files
committed
feat: update solutions to lc problems
* No.0136.Single Number * No.0137.Single Number II * No.1237.Find Positive Integer Solution for a Given Equation
1 parent dbd5355 commit 6cedebf

File tree

13 files changed

+139
-139
lines changed

13 files changed

+139
-139
lines changed

solution/0100-0199/0136.Single Number/README.md

+9-12
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,16 @@ impl Solution {
156156

157157
```swift
158158
class Solution {
159-
func singleNumber(_ nums: [Int]) -> Int {
160-
var a = nums.sorted()
161-
var n = a.count
162-
for i in stride(from: 0, through: n - 1, by: 2) {
163-
if i == n - 1 {
164-
return a[i]
165-
}
166-
else if a[i] != a[i + 1] {
167-
return a[i]
168-
}
159+
func singleNumber(_ nums: [Int]) -> Int {
160+
var a = nums.sorted()
161+
var n = a.count
162+
for i in stride(from: 0, through: n - 2, by: 2) {
163+
if a[i] != a[i + 1] {
164+
return a[i]
165+
}
166+
}
167+
return a[n - 1]
169168
}
170-
return 0
171-
}
172169
}
173170
```
174171

solution/0100-0199/0136.Single Number/README_EN.md

+9-12
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,16 @@ impl Solution {
120120

121121
```swift
122122
class Solution {
123-
func singleNumber(_ nums: [Int]) -> Int {
124-
var a = nums.sorted()
125-
var n = a.count
126-
for i in stride(from: 0, through: n - 1, by: 2) {
127-
if i == n - 1 {
128-
return a[i]
129-
}
130-
else if a[i] != a[i + 1] {
131-
return a[i]
132-
}
123+
func singleNumber(_ nums: [Int]) -> Int {
124+
var a = nums.sorted()
125+
var n = a.count
126+
for i in stride(from: 0, through: n - 2, by: 2) {
127+
if a[i] != a[i + 1] {
128+
return a[i]
129+
}
130+
}
131+
return a[n - 1]
133132
}
134-
return 0
135-
}
136133
}
137134
```
138135

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
class Solution {
2-
func singleNumber(_ nums: [Int]) -> Int {
3-
var a = nums.sorted()
4-
var n = a.count
5-
for i in stride(from: 0, through: n - 1, by: 2) {
6-
if i == n - 1 {
7-
return a[i]
8-
}
9-
else if a[i] != a[i + 1] {
10-
return a[i]
11-
}
2+
func singleNumber(_ nums: [Int]) -> Int {
3+
var a = nums.sorted()
4+
var n = a.count
5+
for i in stride(from: 0, through: n - 2, by: 2) {
6+
if a[i] != a[i + 1] {
7+
return a[i]
8+
}
9+
}
10+
return a[n - 1]
1211
}
13-
return 0
14-
}
1512
}

solution/0100-0199/0137.Single Number II/README.md

+9-12
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,16 @@ public:
126126
127127
```swift
128128
class Solution {
129-
func singleNumber(_ nums: [Int]) -> Int {
130-
var a = nums.sorted()
131-
var n = a.count
132-
for i in stride(from: 0, through: n - 1, by: 3) {
133-
if i == n - 1 {
134-
return a[i]
135-
}
136-
else if a[i] != a[i + 1] {
137-
return a[i]
138-
}
129+
func singleNumber(_ nums: [Int]) -> Int {
130+
var a = nums.sorted()
131+
var n = a.count
132+
for i in stride(from: 0, through: n - 2, by: 3) {
133+
if a[i] != a[i + 1] {
134+
return a[i]
135+
}
136+
}
137+
return a[n - 1]
139138
}
140-
return 0
141-
}
142139
}
143140
```
144141

solution/0100-0199/0137.Single Number II/README_EN.md

+9-12
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,16 @@ public:
105105
106106
```swift
107107
class Solution {
108-
func singleNumber(_ nums: [Int]) -> Int {
109-
var a = nums.sorted()
110-
var n = a.count
111-
for i in stride(from: 0, through: n - 1, by: 3) {
112-
if i == n - 1 {
113-
return a[i]
114-
}
115-
else if a[i] != a[i + 1] {
116-
return a[i]
117-
}
108+
func singleNumber(_ nums: [Int]) -> Int {
109+
var a = nums.sorted()
110+
var n = a.count
111+
for i in stride(from: 0, through: n - 2, by: 3) {
112+
if a[i] != a[i + 1] {
113+
return a[i]
114+
}
115+
}
116+
return a[n - 1]
118117
}
119-
return 0
120-
}
121118
}
122119
```
123120

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
class Solution {
2-
func singleNumber(_ nums: [Int]) -> Int {
3-
var a = nums.sorted()
4-
var n = a.count
5-
for i in stride(from: 0, through: n - 1, by: 3) {
6-
if i == n - 1 {
7-
return a[i]
8-
}
9-
else if a[i] != a[i + 1] {
10-
return a[i]
11-
}
2+
func singleNumber(_ nums: [Int]) -> Int {
3+
var a = nums.sorted()
4+
var n = a.count
5+
for i in stride(from: 0, through: n - 2, by: 3) {
6+
if a[i] != a[i + 1] {
7+
return a[i]
8+
}
9+
}
10+
return a[n - 1]
1211
}
13-
return 0
14-
}
1512
}

solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/README.md

+44-27
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,23 @@ x=5, y=1 -> f(5, 1) = 5 * 1 = 5</pre>
7373

7474
<!-- 这里可写通用的实现逻辑 -->
7575

76-
二分查找。
76+
**方法一:枚举 + 二分查找**
77+
78+
根据题目我们可以知道,函数 $f(x, y)$ 是单调递增函数,因此,我们可以枚举 $x$,然后在 $[1,...z]$ 中二分查找 $y$,使得 $f(x, y) = z$。如果找到了,就将 $(x, y)$ 加入答案中。
79+
80+
时间复杂度 $(z \log z)$,空间复杂度 $O(1)$。本题中 $z \le 100$。
81+
82+
**方法二:双指针**
83+
84+
我们可以定义两个指针 $x$ 和 $y$,初始时 $x = 1$, $y = z$。
85+
86+
- 如果 $f(x, y) = z$,我们将 $(x, y)$ 加入答案中,然后 $x \leftarrow x + 1$, $y \leftarrow y - 1$;
87+
- 如果 $f(x, y) \lt z$,此时对任意的 $y' \lt y$,都有 $f(x, y') \lt f(x, y) \lt z$,因此我们不能将 $y$ 减小,只能将 $x$ 增大,所以 $x \leftarrow x + 1$;
88+
- 如果 $f(x, y) \gt z$,此时对任意的 $x' \gt x$,都有 $f(x', y) \gt f(x, y) \gt z$,因此我们不能将 $x$ 增大,只能将 $y$ 减小,所以 $y \leftarrow y - 1$。
89+
90+
循环结束后,返回答案。
91+
92+
时间复杂度 $O(z)$,空间复杂度 $O(1)$。本题中 $z \le 100$。
7793

7894
<!-- tabs:start -->
7995

@@ -97,8 +113,8 @@ x=5, y=1 -> f(5, 1) = 5 * 1 = 5</pre>
97113
class Solution:
98114
def findSolution(self, customfunction: "CustomFunction", z: int) -> List[List[int]]:
99115
ans = []
100-
for x in range(1, 1001):
101-
y = 1 + bisect_left(range(1, 1001), z, key=lambda y: customfunction.f(x, y))
116+
for x in range(1, z + 1):
117+
y = 1 + bisect_left(range(1, z + 1), z, key=lambda y: customfunction.f(x, y))
102118
if customfunction.f(x, y) == z:
103119
ans.append([x, y])
104120
return ans
@@ -113,14 +129,15 @@ class Solution:
113129
# Note that f(x, y) is increasing with respect to both x and y.
114130
# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
115131
def f(self, x, y):
116-
132+
117133
"""
118134

135+
119136
class Solution:
120-
def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
137+
def findSolution(self, customfunction: "CustomFunction", z: int) -> List[List[int]]:
121138
ans = []
122-
x, y = 1, 1000
123-
while x <= 1000 and y:
139+
x, y = 1, z
140+
while x <= z and y:
124141
t = customfunction.f(x, y)
125142
if t < z:
126143
x += 1
@@ -151,8 +168,8 @@ class Solution:
151168
class Solution {
152169
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
153170
List<List<Integer>> ans = new ArrayList<>();
154-
for (int x = 1; x <= 1000; ++x) {
155-
int l = 1, r = 1000;
171+
for (int x = 1; x <= z; ++x) {
172+
int l = 1, r = z;
156173
while (l < r) {
157174
int mid = (l + r) >> 1;
158175
if (customfunction.f(x, mid) >= z) {
@@ -185,13 +202,13 @@ class Solution:
185202
class Solution {
186203
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
187204
List<List<Integer>> ans = new ArrayList<>();
188-
int x = 1, y = 1000;
189-
while (x <= 1000 && y > 0) {
205+
int x = 1, y = z;
206+
while (x <= z && y > 0) {
190207
int t = customfunction.f(x, y);
191208
if (t < z) {
192-
++x;
209+
x++;
193210
} else if (t > z) {
194-
--y;
211+
y--;
195212
} else {
196213
ans.add(Arrays.asList(x++, y--));
197214
}
@@ -220,8 +237,8 @@ class Solution {
220237
public:
221238
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
222239
vector<vector<int>> ans;
223-
for (int x = 1; x <= 1000; ++x) {
224-
int l = 1, r = 1000;
240+
for (int x = 1; x <= z; ++x) {
241+
int l = 1, r = z;
225242
while (l < r) {
226243
int mid = (l + r) >> 1;
227244
if (customfunction.f(x, mid) >= z) {
@@ -256,13 +273,13 @@ class Solution {
256273
public:
257274
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
258275
vector<vector<int>> ans;
259-
int x = 1, y = 1000;
260-
while (x <= 1000 && y) {
276+
int x = 1, y = z;
277+
while (x <= z && y) {
261278
int t = customfunction.f(x, y);
262279
if (t < z) {
263-
++x;
280+
x++;
264281
} else if (t > z) {
265-
--y;
282+
y--;
266283
} else {
267284
ans.push_back({x++, y--});
268285
}
@@ -285,8 +302,8 @@ public:
285302
*/
286303

287304
func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
288-
for x := 1; x <= 1000; x++ {
289-
y := 1 + sort.Search(999, func(y int) bool { return customFunction(x, y+1) >= z })
305+
for x := 1; x <= z; x++ {
306+
y := 1 + sort.Search(z, func(y int) bool { return customFunction(x, y+1) >= z })
290307
if customFunction(x, y) == z {
291308
ans = append(ans, []int{x, y})
292309
}
@@ -306,8 +323,8 @@ func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
306323
*/
307324

308325
func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
309-
x, y := 1, 1000
310-
for x <= 1000 && y > 0 {
326+
x, y := 1, z
327+
for x <= z && y > 0 {
311328
t := customFunction(x, y)
312329
if t < z {
313330
x++
@@ -335,9 +352,9 @@ func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
335352

336353
function findSolution(customfunction: CustomFunction, z: number): number[][] {
337354
const ans: number[][] = [];
338-
for (let x = 1; x <= 1000; ++x) {
355+
for (let x = 1; x <= z; ++x) {
339356
let l = 1;
340-
let r = 1000;
357+
let r = z;
341358
while (l < r) {
342359
const mid = (l + r) >> 1;
343360
if (customfunction.f(x, mid) >= z) {
@@ -365,9 +382,9 @@ function findSolution(customfunction: CustomFunction, z: number): number[][] {
365382

366383
function findSolution(customfunction: CustomFunction, z: number): number[][] {
367384
let x = 1;
368-
let y = 1000;
385+
let y = z;
369386
const ans: number[][] = [];
370-
while (x <= 1000 && y) {
387+
while (x <= z && y) {
371388
const t = customfunction.f(x, y);
372389
if (t < z) {
373390
++x;

0 commit comments

Comments
 (0)