File tree Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ /// 66. Plus One
2
+ /// Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
3
+ /// The digits are stored such that the most significant digit is at the head of the list, and each
4
+ /// element in the array contain a single digit.
5
+ /// You may assume the integer does not contain any leading zero, except the number 0 itself.
6
+
7
+ import XCTest
8
+
9
+ /// Approach: Math
10
+ func plusOne( _ digits: [ Int ] ) -> [ Int ] {
11
+ var digits = digits
12
+ var carry = 1
13
+ for i in stride ( from: digits. count - 1 , through: 0 , by: - 1 ) {
14
+ digits [ i] += carry
15
+ if digits [ i] > 9 {
16
+ digits [ i] = digits [ i] % 10
17
+ carry = 1
18
+ } else {
19
+ carry = 0
20
+ }
21
+ }
22
+ if carry == 1 {
23
+ digits. insert ( 1 , at: 0 )
24
+ }
25
+ return digits
26
+ }
27
+
28
+ class Tests : XCTestCase {
29
+ func testExample( ) {
30
+ let digits = [ 1 , 2 , 3 ]
31
+ XCTAssertEqual ( plusOne ( digits) , [ 1 , 2 , 4 ] )
32
+ }
33
+ }
34
+
35
+ Tests . defaultTestSuite. run ( )
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" standalone =" yes" ?>
2
+ <playground version =' 5.0' target-platform =' macos' executeOnSourceChanges =' false' >
3
+ <timeline fileName =' timeline.xctimeline' />
4
+ </playground >
Original file line number Diff line number Diff line change @@ -52,6 +52,7 @@ You can visit the pages below to search problems by company tags. Then come back
52
52
31 | [ Next Permutation] ( https://leetcode.com/problems/next-permutation/description/ ) | [ Solution] ( https://github.com/zhubofei/LeetCode-Swift/blob/master/0031-next-permutation.playground/Contents.swift ) | Two Pointers
53
53
32 | [ Longest Valid Parentheses] ( https://leetcode.com/problems/longest-valid-parentheses/description/ ) | [ Solution] ( https://github.com/zhubofei/LeetCode-Swift/blob/master/0032-longest-valid-parentheses.playground/Contents.swift ) | Stack
54
54
33 | [ Search in Rotated Sorted Array] ( https://leetcode.com/problems/search-in-rotated-sorted-array/description/ ) | [ Solution] ( https://github.com/zhubofei/LeetCode-Swift/blob/master/0033-search-in-rotated-sorted-array.playground/Contents.swift ) | Binary Search
55
+ 66 | [ Plus One] ( https://leetcode.com/problems/plus-one/description/ ) | [ Solution] ( https://github.com/zhubofei/LeetCode-Swift/blob/master/0066-plus-one.playground/Contents.swift ) | Math
55
56
482 | [ License Key Formatting] ( https://leetcode.com/problems/license-key-formatting/description/ ) | [ Solution] ( https://github.com/zhubofei/LeetCode-Swift/blob/master/0482-license-key-formatting.playground/Contents.swift ) | String
56
57
681 | [ Next Closest Time] ( https://leetcode.com/problems/next-closest-time/description/ ) | [ Solution] ( https://github.com/zhubofei/LeetCode-Swift/blob/master/0681-next-closest-time.playground/Contents.swift ) | Simulation
57
58
683 | [ K Empty Slots] ( https://leetcode.com/problems/k-empty-slots/description/ ) | [ Solution] ( https://github.com/zhubofei/LeetCode-Swift/blob/master/0683-k-empty-slots.playground/Contents.swift ) | Sliding Window
You can’t perform that action at this time.
0 commit comments