Skip to content

Commit 026c054

Browse files
authored
Merge branch 'main' into patch-5
2 parents b707317 + b0f757b commit 026c054

File tree

78 files changed

+2513
-347
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+2513
-347
lines changed

lcof2/剑指 Offer II 069. 山峰数组的顶部/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,26 @@ var peakIndexInMountainArray = function (arr) {
182182
};
183183
```
184184

185+
#### Swift
186+
187+
```swift
188+
class Solution {
189+
func peakIndexInMountainArray(_ arr: [Int]) -> Int {
190+
var left = 1
191+
var right = arr.count - 2
192+
while left < right {
193+
let mid = (left + right) / 2
194+
if arr[mid] > arr[mid + 1] {
195+
right = mid
196+
} else {
197+
left = mid + 1
198+
}
199+
}
200+
return left
201+
}
202+
}
203+
```
204+
185205
<!-- tabs:end -->
186206

187207
<!-- solution:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
func peakIndexInMountainArray(_ arr: [Int]) -> Int {
3+
var left = 1
4+
var right = arr.count - 2
5+
while left < right {
6+
let mid = (left + right) / 2
7+
if arr[mid] > arr[mid + 1] {
8+
right = mid
9+
} else {
10+
left = mid + 1
11+
}
12+
}
13+
return left
14+
}
15+
}
16+

lcof2/剑指 Offer II 070. 排序数组中只出现一次的数字/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,28 @@ function singleNonDuplicate(nums: number[]): number {
151151
}
152152
```
153153

154+
#### Swift
155+
156+
```swift
157+
class Solution {
158+
func singleNonDuplicate(_ nums: [Int]) -> Int {
159+
var left = 0
160+
var right = nums.count - 1
161+
162+
while left < right {
163+
let mid = (left + right) / 2
164+
if nums[mid] != nums[mid ^ 1] {
165+
right = mid
166+
} else {
167+
left = mid + 1
168+
}
169+
}
170+
171+
return nums[left]
172+
}
173+
}
174+
```
175+
154176
<!-- tabs:end -->
155177

156178
<!-- solution:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
func singleNonDuplicate(_ nums: [Int]) -> Int {
3+
var left = 0
4+
var right = nums.count - 1
5+
6+
while left < right {
7+
let mid = (left + right) / 2
8+
if nums[mid] != nums[mid ^ 1] {
9+
right = mid
10+
} else {
11+
left = mid + 1
12+
}
13+
}
14+
15+
return nums[left]
16+
}
17+
}

lcof2/剑指 Offer II 071. 按权重生成随机数/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,44 @@ func (this *Solution) PickIndex() int {
222222
*/
223223
```
224224

225+
#### Swift
226+
227+
```swift
228+
class Solution {
229+
private var presum: [Int]
230+
231+
init(_ w: [Int]) {
232+
let n = w.count
233+
presum = [Int](repeating: 0, count: n + 1)
234+
for i in 0..<n {
235+
presum[i + 1] = presum[i] + w[i]
236+
}
237+
}
238+
239+
func pickIndex() -> Int {
240+
let n = presum.count
241+
let x = Int.random(in: 1...presum[n - 1])
242+
var left = 0
243+
var right = n - 2
244+
while left < right {
245+
let mid = (left + right) >> 1
246+
if presum[mid + 1] >= x {
247+
right = mid
248+
} else {
249+
left = mid + 1
250+
}
251+
}
252+
return left
253+
}
254+
}
255+
/**
256+
* Your Solution object will be instantiated and called as such:
257+
* let w = [1]
258+
* let solution = Solution(w)
259+
* solution.pickIndex()
260+
*/
261+
```
262+
225263
<!-- tabs:end -->
226264

227265
<!-- solution:end -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
private var presum: [Int]
3+
4+
init(_ w: [Int]) {
5+
let n = w.count
6+
presum = [Int](repeating: 0, count: n + 1)
7+
for i in 0..<n {
8+
presum[i + 1] = presum[i] + w[i]
9+
}
10+
}
11+
12+
func pickIndex() -> Int {
13+
let n = presum.count
14+
let x = Int.random(in: 1...presum[n - 1])
15+
var left = 0
16+
var right = n - 2
17+
while left < right {
18+
let mid = (left + right) >> 1
19+
if presum[mid + 1] >= x {
20+
right = mid
21+
} else {
22+
left = mid + 1
23+
}
24+
}
25+
return left
26+
}
27+
}
28+
/**
29+
* Your Solution object will be instantiated and called as such:
30+
* let w = [1]
31+
* let solution = Solution(w)
32+
* solution.pickIndex()
33+
*/

solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ func letterCombinations(digits string) []string {
165165

166166
```ts
167167
function letterCombinations(digits: string): string[] {
168-
if (digits.length == 0) {
168+
if (digits.length === 0) {
169169
return [];
170170
}
171171
const ans: string[] = [''];
172172
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
173173
for (const i of digits) {
174-
const s = d[parseInt(i) - 2];
174+
const s = d[+i - 2];
175175
const t: string[] = [];
176176
for (const a of ans) {
177177
for (const b of s) {
@@ -218,13 +218,13 @@ impl Solution {
218218
* @return {string[]}
219219
*/
220220
var letterCombinations = function (digits) {
221-
if (digits.length == 0) {
221+
if (digits.length === 0) {
222222
return [];
223223
}
224224
const ans = [''];
225225
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
226226
for (const i of digits) {
227-
const s = d[parseInt(i) - 2];
227+
const s = d[+i - 2];
228228
const t = [];
229229
for (const a of ans) {
230230
for (const b of s) {
@@ -392,7 +392,7 @@ func letterCombinations(digits string) (ans []string) {
392392

393393
```ts
394394
function letterCombinations(digits: string): string[] {
395-
if (digits.length == 0) {
395+
if (digits.length === 0) {
396396
return [];
397397
}
398398
const ans: string[] = [];
@@ -403,7 +403,7 @@ function letterCombinations(digits: string): string[] {
403403
ans.push(t.join(''));
404404
return;
405405
}
406-
const s = d[parseInt(digits[i]) - 2];
406+
const s = d[+digits[i] - 2];
407407
for (const c of s) {
408408
t.push(c);
409409
dfs(i + 1);
@@ -453,7 +453,7 @@ impl Solution {
453453
* @return {string[]}
454454
*/
455455
var letterCombinations = function (digits) {
456-
if (digits.length == 0) {
456+
if (digits.length === 0) {
457457
return [];
458458
}
459459
const ans = [];
@@ -464,7 +464,7 @@ var letterCombinations = function (digits) {
464464
ans.push(t.join(''));
465465
return;
466466
}
467-
const s = d[parseInt(digits[i]) - 2];
467+
const s = d[+digits[i] - 2];
468468
for (const c of s) {
469469
t.push(c);
470470
dfs(i + 1);

solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ func letterCombinations(digits string) []string {
161161

162162
```ts
163163
function letterCombinations(digits: string): string[] {
164-
if (digits.length == 0) {
164+
if (digits.length === 0) {
165165
return [];
166166
}
167167
const ans: string[] = [''];
168168
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
169169
for (const i of digits) {
170-
const s = d[parseInt(i) - 2];
170+
const s = d[+i - 2];
171171
const t: string[] = [];
172172
for (const a of ans) {
173173
for (const b of s) {
@@ -214,13 +214,13 @@ impl Solution {
214214
* @return {string[]}
215215
*/
216216
var letterCombinations = function (digits) {
217-
if (digits.length == 0) {
217+
if (digits.length === 0) {
218218
return [];
219219
}
220220
const ans = [''];
221221
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
222222
for (const i of digits) {
223-
const s = d[parseInt(i) - 2];
223+
const s = d[+i - 2];
224224
const t = [];
225225
for (const a of ans) {
226226
for (const b of s) {
@@ -388,7 +388,7 @@ func letterCombinations(digits string) (ans []string) {
388388

389389
```ts
390390
function letterCombinations(digits: string): string[] {
391-
if (digits.length == 0) {
391+
if (digits.length === 0) {
392392
return [];
393393
}
394394
const ans: string[] = [];
@@ -399,7 +399,7 @@ function letterCombinations(digits: string): string[] {
399399
ans.push(t.join(''));
400400
return;
401401
}
402-
const s = d[parseInt(digits[i]) - 2];
402+
const s = d[+digits[i] - 2];
403403
for (const c of s) {
404404
t.push(c);
405405
dfs(i + 1);
@@ -449,7 +449,7 @@ impl Solution {
449449
* @return {string[]}
450450
*/
451451
var letterCombinations = function (digits) {
452-
if (digits.length == 0) {
452+
if (digits.length === 0) {
453453
return [];
454454
}
455455
const ans = [];
@@ -460,7 +460,7 @@ var letterCombinations = function (digits) {
460460
ans.push(t.join(''));
461461
return;
462462
}
463-
const s = d[parseInt(digits[i]) - 2];
463+
const s = d[+digits[i] - 2];
464464
for (const c of s) {
465465
t.push(c);
466466
dfs(i + 1);

solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* @return {string[]}
44
*/
55
var letterCombinations = function (digits) {
6-
if (digits.length == 0) {
6+
if (digits.length === 0) {
77
return [];
88
}
99
const ans = [''];
1010
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
1111
for (const i of digits) {
12-
const s = d[parseInt(i) - 2];
12+
const s = d[+i - 2];
1313
const t = [];
1414
for (const a of ans) {
1515
for (const b of s) {

solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
function letterCombinations(digits: string): string[] {
2-
if (digits.length == 0) {
2+
if (digits.length === 0) {
33
return [];
44
}
55
const ans: string[] = [''];
66
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
77
for (const i of digits) {
8-
const s = d[parseInt(i) - 2];
8+
const s = d[+i - 2];
99
const t: string[] = [];
1010
for (const a of ans) {
1111
for (const b of s) {

0 commit comments

Comments
 (0)