[Swift]LeetCode306. 累加數 | Additive Number

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-djmpbcaj-kr.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Additive number is a string whose digits can form additive sequence.git

A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.github

Given a string containing only digits '0'-'9', write a function to determine if it's an additive number.算法

Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.微信

Example 1:spa

Input: 
Output: true 
Explanation: The digits can form an additive sequence: . 
             1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
"112358"1, 1, 2, 3, 5, 8

Example 2:code

Input: 
Output: true 
Explanation: The additive sequence is: . 
             1 + 99 = 100, 99 + 100 = 199"199100199"1, 99, 100, 199

Follow up:
How would you handle overflow for very large input integers?orm


累加數是一個字符串,組成它的數字能夠造成累加序列。htm

一個有效的累加序列必須至少包含 3 個數。除了最開始的兩個數之外,字符串中的其餘數都等於它以前兩個數相加的和。blog

給定一個只包含數字 '0'-'9' 的字符串,編寫一個算法來判斷給定輸入是不是累加數。

說明: 累加序列裏的數不會以 0 開頭,因此不會出現 1, 2, 03 或者 1, 02, 3 的狀況。

示例 1:

輸入: 
輸出: true 
解釋: 累加序列爲: 。1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
"112358"1, 1, 2, 3, 5, 8

示例 2:

輸入: 
輸出: true 
解釋: 累加序列爲: 1 + 99 = 100, 99 + 100 = 199"199100199"1, 99, 100, 199。

進階:
你如何處理一個溢出的過大的整數輸入?


16ms

 1 class Solution {
 2     func isAdditiveNumber(_ num: String) -> Bool {
 3         let array = [Character](num)
 4         let n = array.count
 5         if n < 3 { return false }
 6         
 7         func helper(s1: String, s2: String, remain: String) -> Bool {
 8             let one = Int(s1)!
 9             let two = Int(s2)!
10             let next = "\(one + two)"
11             if next == remain { return true }
12             var newRemain = remain
13             if remain.hasPrefix(next) {
14                 let range = remain.range(of: next)!
15                 newRemain.replaceSubrange(range, with: "")
16                 return helper(s1: s2, s2: next, remain: newRemain)
17             } else {
18                 return false
19             }
20         }
21         
22         //設第一個數是以i結尾的,第二個數是以j結尾的
23         for i in 0 ... (n - 1)/2 - 1 { //i只能取到一半如下的值
24             let one = String(array[0...i])
25             if one != "0" && one.hasPrefix("0") { continue }
26             for j in i + 1 ... n - 2 - i { //j至少取一位,且 n - j >= i
27                 let two = String(array[i+1 ... j])
28                 if two != "0" && two.hasPrefix("0") { continue }
29                 let remain = String(array[j+1 ... n-1])
30                 if remain != "0" && remain.hasPrefix("0") { continue }
31                 if helper(s1: one, s2: two, remain: remain) {
32                     return true
33                 }
34             }
35         }
36         return false
37     }
38 }

24ms

 1 class Solution {
 2     func isAdditiveNumber(_ num: String) -> Bool {
 3         
 4         if num.count < 3 {
 5             return false
 6         }
 7         
 8         let numArr = Array(num)
 9         
10         for i in 1..<numArr.count-1 {
11             for j in i+1..<numArr.count {
12                 let f = String(numArr[0..<i])
13                 let s = String(numArr[i..<j])
14                 var fn = Int(f)!
15                 var sn = Int(s)!
16                 var ln = fn + sn
17                 
18                 var l = "\(ln)"
19                 var total = f + s + l
20                 
21                 if (f.count > 1 && f.first == "0" )||(s.count > 1 && s.first == "0") {
22                     continue
23                 }
24                 while total.count < num.count {
25                     let totalArr = Array(total)
26                     if totalArr != Array(numArr[0..<totalArr.count]) {
27                         break
28                     }
29                     fn = sn
30                     sn = ln
31                     ln = fn + sn
32                     l = "\(ln)"
33                     total += l
34                 }
35                 if total == num {
36                     return true
37                 }
38             }
39         }
40         return false
41     }
42 }
相關文章
相關標籤/搜索