Skip to content

Commit

Permalink
0066-plus-one
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeizhu committed Aug 19, 2018
1 parent 1aacdb0 commit 0120b48
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 0066-plus-one.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// 66. Plus One
/// 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.

import XCTest

/// Approach: Math
func plusOne(_ digits: [Int]) -> [Int] {
var digits = digits
var carry = 1
for i in stride(from: digits.count - 1, through: 0, by: -1) {
digits[i] += carry
if digits[i] > 9 {
digits[i] = digits[i] % 10
carry = 1
} else {
carry = 0
}
}
if carry == 1 {
digits.insert(1, at: 0)
}
return digits
}

class Tests: XCTestCase {
func testExample() {
let digits = [1, 2, 3]
XCTAssertEqual(plusOne(digits), [1, 2, 4])
}
}

Tests.defaultTestSuite.run()
4 changes: 4 additions & 0 deletions 0066-plus-one.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ You can visit the pages below to search problems by company tags. Then come back
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
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
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
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
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
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
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
Expand Down

0 comments on commit 0120b48

Please sign in to comment.