Swift 3 Solution


  • 0
    Y
    class Solution {
        func checkPossibility(_ nums: [Int]) -> Bool {
            // 3  4   2    3        (4>2)
            //    i   i+1  i-1      (4>3)
            //        4     3       arr[i + 1] = arr[i]
            var arr = nums
            var moved = false
            for i in 0..<arr.count-1 {
                if arr[i] > arr[i + 1] {
                    if moved {
                        return false
                    }
                    if i > 0 && arr[i - 1] > arr[i + 1] {
                        arr[i + 1] = arr[i]
                    }
                    moved = true
                }
            }
            return true
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.