[Swift]LeetCode66. 加一 | Plus One

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-byacmtnv-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.git

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.github

You may assume the integer does not contain any leading zero, except the number 0 itself.數組

Example 1:微信

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:app

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

給定一個由整數組成的非空數組所表示的非負整數,在該數的基礎上加一。ide

最高位數字存放在數組的首位, 數組中每一個元素只存儲一個數字。spa

你能夠假設除了整數 0 以外,這個整數不會以零開頭。code

示例 1:htm

輸入: [1,2,3]
輸出: [1,2,4]
解釋: 輸入數組表示數字 123。

示例 2:

輸入: [4,3,2,1]
輸出: [4,3,2,2]
解釋: 輸入數組表示數字 4321。

8ms
 1 class Solution {
 2     func plusOne(_ digits: [Int]) -> [Int] {
 3         var addOne = digits     
 4         var current = digits.count - 1
 5         var carryOver = false        
 6         var num = digits[current]
 7 
 8         if (current < 0)  {
 9             return addOne
10         }
11         
12         num = addOne[current] + 1
13         if (num<10) {
14             addOne[current] = num
15             return addOne
16         } else {
17             carryOver = true
18             addOne[current] = 0
19             current = current - 1
20             while (current>=0) && (carryOver==true) {
21                 num = addOne[current] + 1
22                 if (num<10) {
23                     addOne[current] = num
24                     return addOne
25                 } else {
26                     carryOver = true
27                     addOne[current] = 0
28                     current = current - 1
29                 }
30             }
31         }
32     
33         //[9] - >[0] ==>[1,0]
34         print("addOne: \(addOne)")
35         if (carryOver==true)  {
36             addOne[0] = 0
37             addOne.insert(1, at: 0) 
38             /*
39             if (addOne[0]<9) {      
40                addOne[0] = addOne[0] + 1 
41             } else {
42                 addOne[0] = 0
43                 addOne.insert(1, at: 0)  
44             }*/
45         } 
46         print("--**addOne: \(addOne)")
47         return addOne
48     }
49 }

12ms

 1 class Solution {
 2     func plusOne(_ digits: [Int]) -> [Int] {
 3         var copy = [Int](), carry = 0, add = 1
 4         for i in stride(from: digits.count-1, through: 0, by: -1){
 5             if(((digits[i] + add + carry)) == 10){
 6                 carry = 1
 7                 copy.append(0)
 8             }
 9             else{
10                 copy.append(digits[i] + carry + add)
11                 carry = 0
12             }
13             add = 0
14         }
15         if(carry != 0){copy.append(carry)}
16         return copy.reversed()
17     }
18 }

12ms

 1 class Solution {
 2     func plusOne(_ digits: [Int]) -> [Int] {
 3         //獲取數組長度
 4         var len:Int = digits.count
 5         //參數digits是let定義,轉成可變參數
 6         var arr:[Int] = digits
 7         //倒序遍歷數組
 8         for i in (0..<digits.count).reversed()
 9         {
10             //若是數組最後一位不是整數9,加一返回數組
11             if arr[i]<9
12             {
13                 arr[i]+=1
14                 return arr  
15             }
16             else
17             {
18                 //若是數組最後一位是整數9,則變成整數0。
19                 arr[i] = 0
20             }
21         }
22         //程序運行至此是全9的狀況,須要在首位插入數字1
23         arr.insert(1, at: 0)
24         return arr
25     }
26 }

16ms

 1 class Solution {
 2     func plusOne(_ digits: [Int]) -> [Int] {
 3         var resultArray = [Int]()
 4         var pointer = digits.count - 1
 5         var checkIfAdded = false
 6         
 7         
 8         if digits.count == 1 {
 9             if digits[0] + 1 > 9 {
10                 return [1,0]
11             } else {
12                 return [digits[0] + 1]
13             }  
14         }
15         
16         
17         while pointer >= 0 {
18             if checkIfAdded == false {
19                if digits[pointer] + 1 > 9 {
20                     resultArray.insert(0, at: 0)
21                     pointer -= 1
22                 } else {
23                     resultArray.insert(digits[pointer] + 1, at: 0)
24                     checkIfAdded = true
25                     pointer -= 1
26                 }  
27             } else {
28                 resultArray.insert(digits[pointer], at: 0)
29                 pointer -= 1
30             } 
31         }
32         
33         if resultArray[0] == 0 {
34             resultArray.insert(1, at: 0)
35         }
36         
37         return resultArray
38 
39     }
40 }

20ms

 1 class Solution {
 2     func plusOne(_ digits: [Int]) -> [Int] {
 3         var carry : Int = 1
 4         var returnArray = digits
 5         
 6         for index in stride(from:digits.count - 1 , to: -1, by: -1){
 7             let value = (digits[index] + carry) % 10
 8             carry = (digits[index] + carry) / 10
 9             returnArray[index] = value
10         }
11         if carry == 1 {
12             returnArray.insert(1 ,at:0)
13         }
14         return returnArray
15     }
16 }
相關文章
相關標籤/搜索