Skip to content

Commit 59d54a5

Browse files
committed
feat: add solutions to lc problem: No.1064. Fixed Point
1 parent 3778dbc commit 59d54a5

File tree

9 files changed

+193
-7
lines changed

9 files changed

+193
-7
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
.idea/
22
.DS_Store
33
.vscode
4-
/node_modules
4+
/node_modules
5+
/solution/result.json
6+
/lcof/lcof.json
7+
/lcof/lcof_list.json
8+
/lcci/lcci.json

solution/1000-1099/1064.Fixed Point/README.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,86 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
二分查找。
56+
5557
<!-- tabs:start -->
5658

5759
### **Python3**
5860

5961
<!-- 这里可写当前语言的特殊实现逻辑 -->
6062

6163
```python
62-
64+
class Solution:
65+
def fixedPoint(self, arr: List[int]) -> int:
66+
left, right = 0, len(arr) - 1
67+
while left < right:
68+
mid = (left + right) >> 1
69+
if arr[mid] >= mid:
70+
right = mid
71+
else:
72+
left = mid + 1
73+
return left if arr[left] == left else -1
6374
```
6475

6576
### **Java**
6677

6778
<!-- 这里可写当前语言的特殊实现逻辑 -->
6879

6980
```java
81+
class Solution {
82+
public int fixedPoint(int[] arr) {
83+
int left = 0, right = arr.length - 1;
84+
while (left < right) {
85+
int mid = (left + right) >> 1;
86+
if (arr[mid] >= mid) {
87+
right = mid;
88+
} else {
89+
left = mid + 1;
90+
}
91+
}
92+
return arr[left] == left ? left : -1;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int fixedPoint(vector<int>& arr) {
103+
int left = 0, right = arr.size() - 1;
104+
while (left < right) {
105+
int mid = left + right >> 1;
106+
if (arr[mid] >= mid) {
107+
right = mid;
108+
} else {
109+
left = mid + 1;
110+
}
111+
}
112+
return arr[left] == left ? left : -1;
113+
}
114+
};
115+
```
70116
117+
### **Go**
118+
119+
```go
120+
func fixedPoint(arr []int) int {
121+
left, right := 0, len(arr)-1
122+
for left < right {
123+
mid := (left + right) >> 1
124+
if arr[mid] >= mid {
125+
right = mid
126+
} else {
127+
left = mid + 1
128+
}
129+
}
130+
if arr[left] == left {
131+
return left
132+
}
133+
return -1
134+
}
71135
```
72136

73137
### **...**

solution/1000-1099/1064.Fixed Point/README_EN.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,82 @@
4141

4242
## Solutions
4343

44+
Binary search.
45+
4446
<!-- tabs:start -->
4547

4648
### **Python3**
4749

4850
```python
49-
51+
class Solution:
52+
def fixedPoint(self, arr: List[int]) -> int:
53+
left, right = 0, len(arr) - 1
54+
while left < right:
55+
mid = (left + right) >> 1
56+
if arr[mid] >= mid:
57+
right = mid
58+
else:
59+
left = mid + 1
60+
return left if arr[left] == left else -1
5061
```
5162

5263
### **Java**
5364

5465
```java
66+
class Solution {
67+
public int fixedPoint(int[] arr) {
68+
int left = 0, right = arr.length - 1;
69+
while (left < right) {
70+
int mid = (left + right) >> 1;
71+
if (arr[mid] >= mid) {
72+
right = mid;
73+
} else {
74+
left = mid + 1;
75+
}
76+
}
77+
return arr[left] == left ? left : -1;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
int fixedPoint(vector<int>& arr) {
88+
int left = 0, right = arr.size() - 1;
89+
while (left < right) {
90+
int mid = left + right >> 1;
91+
if (arr[mid] >= mid) {
92+
right = mid;
93+
} else {
94+
left = mid + 1;
95+
}
96+
}
97+
return arr[left] == left ? left : -1;
98+
}
99+
};
100+
```
55101
102+
### **Go**
103+
104+
```go
105+
func fixedPoint(arr []int) int {
106+
left, right := 0, len(arr)-1
107+
for left < right {
108+
mid := (left + right) >> 1
109+
if arr[mid] >= mid {
110+
right = mid
111+
} else {
112+
left = mid + 1
113+
}
114+
}
115+
if arr[left] == left {
116+
return left
117+
}
118+
return -1
119+
}
56120
```
57121

58122
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int fixedPoint(vector<int>& arr) {
4+
int left = 0, right = arr.size() - 1;
5+
while (left < right) {
6+
int mid = left + right >> 1;
7+
if (arr[mid] >= mid) {
8+
right = mid;
9+
} else {
10+
left = mid + 1;
11+
}
12+
}
13+
return arr[left] == left ? left : -1;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func fixedPoint(arr []int) int {
2+
left, right := 0, len(arr)-1
3+
for left < right {
4+
mid := (left + right) >> 1
5+
if arr[mid] >= mid {
6+
right = mid
7+
} else {
8+
left = mid + 1
9+
}
10+
}
11+
if arr[left] == left {
12+
return left
13+
}
14+
return -1
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int fixedPoint(int[] arr) {
3+
int left = 0, right = arr.length - 1;
4+
while (left < right) {
5+
int mid = (left + right) >> 1;
6+
if (arr[mid] >= mid) {
7+
right = mid;
8+
} else {
9+
left = mid + 1;
10+
}
11+
}
12+
return arr[left] == left ? left : -1;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def fixedPoint(self, arr: List[int]) -> int:
3+
left, right = 0, len(arr) - 1
4+
while left < right:
5+
mid = (left + right) >> 1
6+
if arr[mid] >= mid:
7+
right = mid
8+
else:
9+
left = mid + 1
10+
return left if arr[left] == left else -1

solution/1900-1999/1905.Count Sub Islands/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
<p> </p>
1616

1717
<p><strong>示例 1:</strong></p>
18-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1905.Count%20Sub%20Islands/test1.png" style="width: 493px; height: 205px;">
18+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1905.Count%20Sub%20Islands/images/test1.png" style="width: 493px; height: 205px;">
1919
<pre><b>输入:</b>grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
2020
<b>输出:</b>3
2121
<strong>解释:</strong>如上图所示,左边为 grid1 ,右边为 grid2 。
2222
grid2 中标红的 1 区域是子岛屿,总共有 3 个子岛屿。
2323
</pre>
2424

2525
<p><strong>示例 2:</strong></p>
26-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1905.Count%20Sub%20Islands/testcasex2.png" style="width: 491px; height: 201px;">
26+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1905.Count%20Sub%20Islands/images/testcasex2.png" style="width: 491px; height: 201px;">
2727
<pre><b>输入:</b>grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
2828
<b>输出:</b>2
2929
<strong>解释:</strong>如上图所示,左边为 grid1 ,右边为 grid2 。

solution/1900-1999/1905.Count Sub Islands/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<p>&nbsp;</p>
1414
<p><strong>Example 1:</strong></p>
1515

16-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1905.Count%20Sub%20Islands/test1.png" style="width: 493px; height: 205px;" />
16+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1905.Count%20Sub%20Islands/images/test1.png" style="width: 493px; height: 205px;" />
1717
<pre>
1818
<strong>Input:</strong> grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
1919
<strong>Output:</strong> 3
@@ -22,7 +22,7 @@ The 1s colored red in grid2 are those considered to be part of a sub-island. The
2222
</pre>
2323

2424
<p><strong>Example 2:</strong></p>
25-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1905.Count%20Sub%20Islands/testcasex2.png" style="width: 491px; height: 201px;" />
25+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1905.Count%20Sub%20Islands/images/testcasex2.png" style="width: 491px; height: 201px;" />
2626
<pre>
2727
<strong>Input:</strong> grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
2828
<strong>Output:</strong> 2

0 commit comments

Comments
 (0)