★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ucawzcgx-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given an array with n
integers, your task is to check if it could become non-decreasing by modifying at most 1
element.git
We define an array is non-decreasing if array[i] <= array[i + 1]
holds for every i
(1 <= i < n).github
Example 1:數組
Input: [4,2,3] Output: True Explanation: You could modify the first to to get a non-decreasing array. 41
Example 2:微信
Input: [4,2,1] Output: False Explanation: You can't get a non-decreasing array by modify at most one element.
Note: The n
belongs to [1, 10,000].spa
給定一個長度爲 n
的整數數組,你的任務是判斷在最多改變 1
個元素的狀況下,該數組可否變成一個非遞減數列。code
咱們是這樣定義一個非遞減數列的: 對於數組中全部的 i
(1 <= i < n),知足 array[i] <= array[i + 1]
。htm
示例 1:blog
輸入: [4,2,3] 輸出: True 解釋: 你能夠經過把第一個4變成1來使得它成爲一個非遞減數列。
示例 2:element
輸入: [4,2,1] 輸出: False 解釋: 你不能在只改變一個元素的狀況下將其變爲非遞減數列。
說明: n
的範圍爲 [1, 10,000]。
44ms
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 var i = 0 4 var j = nums.count - 1 5 while i < j && nums[i] <= nums[i+1] { 6 i += 1 7 } 8 while i < j && nums[j] >= nums[j-1] { 9 j -= 1 10 } 11 let head = (i == 0) ? Int.min : nums[i-1] 12 let next = (j == nums.count - 1) ? Int.max : nums[j+1] 13 14 if j - i <= 1 && (head < nums[j] || nums[i] < next) { 15 return true 16 } else { 17 return false 18 } 19 } 20 }
188ms
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 4 if nums.count < 3 { 5 return true 6 } 7 8 var i = 0 9 10 while i <= nums.count - 2, nums[i] <= nums[i + 1] { 11 i += 1 12 } 13 14 var j = nums.count - 1 15 16 if i >= j - 1 { 17 return true 18 } 19 20 while 1 <= j, nums[j - 1] <= nums[j] { 21 j -= 1 22 } 23 24 if j <= 1 { 25 return true 26 } 27 28 return ((j - i == 2) && (nums[i] <= nums[j])) 29 || 30 ((j - i == 1) 31 && 32 ((0 < i) && (nums[i - 1] <= nums[j]) 33 || ((j < nums.count - 1) && (nums[i] <= nums[j + 1])))) 34 } 35 }
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 var nums = nums 4 var cnt:Int = 1 5 var n:Int = nums.count 6 for i in 1..<n 7 { 8 if nums[i] < nums[i - 1] 9 { 10 if cnt == 0 {return false} 11 if i == 1 || nums[i] >= nums[i - 2] 12 { 13 nums[i - 1] = nums[i] 14 } 15 else 16 { 17 nums[i] = nums[i - 1] 18 } 19 cnt -= 1 20 } 21 } 22 return true 23 } 24 }
196ms
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 var c = 0 4 var nums = [-Int.max] + nums 5 var low = nums[0] 6 7 for (i, n) in nums.enumerated().dropFirst() { 8 if n < nums[i-1] { 9 if n >= low { 10 nums[i-1] = low 11 } else { 12 nums[i] = nums[i-1] 13 } 14 c += 1 15 if c > 1 { 16 return false 17 } 18 } 19 if i > 1 { 20 low = nums[i-1] 21 } 22 } 23 24 return true 25 } 26 }
200ms
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 var newNums = nums 4 if newNums.count <= 2 { 5 return true 6 } 7 8 var i = 1 9 var j = 2 10 var noOfChange = 0 11 12 if (newNums[i] < newNums[i-1]) { 13 newNums[i-1] = newNums[i] 14 noOfChange = noOfChange + 1 15 } 16 17 while j <= newNums.count - 1 { 18 if (newNums[i] > newNums[j]) { 19 //if [j] > [i-1], change value of [i] -> [i-1] 20 if (newNums[j] > newNums[i-1]) { 21 newNums[i] = newNums[i-1] 22 } 23 //else, change [j] -> [i] 24 else { 25 newNums[j] = newNums[i] 26 } 27 28 noOfChange = noOfChange + 1 29 } 30 j = j + 1 31 i = i + 1 32 } 33 34 return noOfChange <= 1 35 } 36 }
208ms
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 4 if nums.count < 3 { 5 return true 6 } 7 8 var index = -1 9 10 for i in 0 ..< (nums.count - 1) { 11 if nums[i] > nums[i+1] { 12 if index != -1 { return false } 13 index = i 14 } 15 } 16 17 return ((index == -1) 18 || (index == 0) 19 || (index == nums.count - 2) 20 || (nums[index+1] - nums[index-1] > 0) 21 || (nums[index+2] - nums[index] > 0)) 22 } 23 }
284ms
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 guard nums.count > 1 && nums.count <= 10000 else { 4 return nums.count == 1 5 } 6 var array = nums 7 var modified = false 8 for i in 0 ..< array.count - 1 { 9 if array[i] > array[i + 1] { 10 if modified { 11 return false 12 } 13 if i > 0 && array[i - 1] > array[i + 1] { 14 array[i + 1] = array[i] 15 } 16 modified = true 17 } 18 } 19 return true 20 } 21 }