Skip to content

Commit 0120b48

Browse files
committed
0066-plus-one
1 parent 1aacdb0 commit 0120b48

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ You can visit the pages below to search problems by company tags. Then come back
5252
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
5353
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
5454
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
5556
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
5657
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
5758
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

0 commit comments

Comments
 (0)