SHARE
TWEET

Untitled

a guest Oct 14th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2.     func plusOne(_ digits: [Int]) -> [Int] {
  3.         guard !digits.isEmpty else {
  4.             fatalError("But you promised!")
  5.         }
  6.         var digits = digits
  7.         let lastIndex = digits.count - 1
  8.         for i in 0...lastIndex {
  9.             let index = lastIndex - i
  10.             var digit = digits[index]
  11.             if digit != 9 {
  12.                 digit += 1
  13.                 digits[index] = digit
  14.                 break
  15.             }
  16.             else {
  17.                 digits[index] = 0
  18.                 if index == 0 {
  19.                     digits.insert(1, at: 0)
  20.                 }
  21.             }
  22.         }
  23.         return digits
  24.     }
  25. }
  26.  
  27. /*
  28. Leetcode results:
  29. ------------------------------------------------------------------------------
  30. Runtime: 8 ms, faster than 78.25% of Swift online submissions for Plus One.
  31. Memory Usage: 21 MB, less than 20.00% of Swift online submissions for Plus One.
  32. -------------------------------------------------------------------------------
  33. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top