File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // 잘못된 풀이 -> 음수를 생각안함
2+ // let n = Int(readLine()!)!
3+ // let sortedNums = readLine()!.split(separator: " ").compactMap { Int($0) }.sorted()
4+
5+ // func good(_ nums: [Int], target: Int) -> Bool {
6+ // var left = 0
7+ // var right = nums.count - 1
8+
9+ // while left < right {
10+ // let sum = nums[left] + nums[right]
11+
12+ // if sum < target {
13+ // left += 1
14+ // } else if sum > target {
15+ // right -= 1
16+ // } else {
17+ // return true
18+ // }
19+ // }
20+
21+ // return false
22+ // }
23+
24+ // var result = 0
25+ // for i in 2..<n {
26+ // let nums = Array(sortedNums[0..<i])
27+
28+ // if good(nums, target: sortedNums[i]) {
29+ // result += 1
30+ // }
31+ // }
32+ // print(result)
33+
34+ let n = Int ( readLine ( ) !) !
35+ let nums = readLine ( ) !. split ( separator: " " ) . compactMap { Int ( $0) } . sorted ( )
36+
37+ var result = 0
38+
39+ for targetIndex in 0 ..< n {
40+ var left = 0
41+ var right = n- 1
42+
43+ while left < right {
44+ if left == targetIndex {
45+ left += 1
46+ continue
47+ }
48+
49+ if right == targetIndex {
50+ right -= 1
51+ continue
52+ }
53+
54+ let sum = nums [ left] + nums[ right]
55+ let target = nums [ targetIndex]
56+
57+ if sum < target {
58+ left += 1
59+ } else if sum > target {
60+ right -= 1
61+ } else {
62+ result += 1
63+ break
64+ }
65+ }
66+ }
67+
68+ print ( result)
You can’t perform that action at this time.
0 commit comments