Skip to content

Latest commit

 

History

History
40 lines (30 loc) · 758 Bytes

167. Two Sum II - Input Array Is Sorted.md

File metadata and controls

40 lines (30 loc) · 758 Bytes

Leetcode

167. Two Sum II - Input Array Is Sorted

문제링크

코드

class Solution {
    func twoSum(_ numbers: [Int], _ target: Int) -> [Int] {
        var cnt: Int = numbers.count - 1
        var num1: Int = numbers[0]
        var num2: Int = numbers[cnt]
        
        if cnt == 1 {
            return [1,2]
        }
        for i in 0...cnt {
            num2 = numbers[cnt - i]
            for j in 0..<cnt - i {
                num1 = numbers[j]
                if num1 + num2 == target {
                    return [j + 1, cnt - i + 1]
                }
            }
        }
        return [0]
    }
}

풀이 이유

참고한 내용