为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode66. 加一 | Plus One

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9697954.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

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.

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:

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

给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

示例 1:

输入: [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 }

 

posted @ 2018-09-25 09:29  为敢技术  阅读(324)  评论(0编辑  收藏  举报