-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathTwoSumII_InputArrayIsSorted_167.swift
More file actions
46 lines (40 loc) · 1.05 KB
/
TwoSumII_InputArrayIsSorted_167.swift
File metadata and controls
46 lines (40 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// TwoSumII_InputArrayIsSorted_167.swift
// LeetCode_Swift
//
// Created by Huni on 12/09/2017.
// Copyright © 2017 KnighhtJoker. All rights reserved.
//
import Foundation
class TwoSumII_InputArrayIsSorted_167 {
func twoSum(_ numbers: [Int], _ target: Int) -> [Int] {
// Time Limit Exceeded
// var i = 0,j = 1
// while i < numbers.count {
//
// if j == numbers.count {
// i += 1
// j = 0
// }
//
// if numbers[i] + numbers[j] == target && i != j {
// return [i + 1,j + 1]
// } else {
// j += 1
// }
// }
//
// 双向指针
var i = 0,j = numbers.count - 1
while i < j {
if numbers[i] + numbers[j] == target {
return [i + 1,j + 1]
} else if numbers[i] + numbers[j] < target {
i += 1
} else {
j -= 1
}
}
return [0,0]
}
}