★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-eduvcfcm-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Solve a given equation and return the value of x
in the form of string "x=#value". The equation contains only '+', '-' operation, the variable x
and its coefficient.git
If there is no solution for the equation, return "No solution".github
If there are infinite solutions for the equation, return "Infinite solutions".微信
If there is exactly one solution for the equation, we ensure that the value of x
is an integer.app
Example 1:spa
Input: "x+5-3+x=6+x-2" Output: "x=2"
Example 2:code
Input: "x=x" Output: "Infinite solutions"
Example 3:component
Input: "2x=x" Output: "x=0"
Example 4:orm
Input: "2x+3x-6x=x+2" Output: "x=-1"
Example 5:htm
Input: "x=x+2" Output: "No solution"
求解一個給定的方程,將x
以字符串"x=#value"的形式返回。該方程僅包含'+',' - '操做,變量 x
和其對應係數。
若是方程沒有解,請返回「No solution」。
若是方程有無限解,則返回「Infinite solutions」。
若是方程中只有一個解,要保證返回值 x
是一個整數。
示例 1:
輸入: "x+5-3+x=6+x-2" 輸出: "x=2"
示例 2:
輸入: "x=x" 輸出: "Infinite solutions"
示例 3:
輸入: "2x=x" 輸出: "x=0"
示例 4:
輸入: "2x+3x-6x=x+2" 輸出: "x=-1"
示例 5:
輸入: "x=x+2" 輸出: "No solution"
1 class Solution { 2 func solveEquation(_ equation: String) -> String { 3 var arr:[Character] = Array(equation) 4 var n:Int = equation.count 5 var a:Int = 0 6 var b:Int = 0 7 var sign:Int = 1 8 var j:Int = 0 9 for i in 0..<n 10 { 11 if arr[i] == "+" || arr[i] == "-" 12 { 13 if i > j 14 { 15 b += Int(equation.subString(j, i - j))! * sign 16 } 17 j = i 18 } 19 else if arr[i] == "x" 20 { 21 if i == j || arr[i - 1] == "+" 22 { 23 a += sign 24 } 25 else if arr[i - 1] == "-" 26 { 27 a -= sign 28 } 29 else 30 { 31 a += Int(equation.subString(j, i - j))! * sign 32 } 33 j = i + 1 34 } 35 else if arr[i] == "=" 36 { 37 if i > j 38 { 39 b += Int(equation.subString(j, i - j))! * sign 40 } 41 sign = -1 42 j = i + 1 43 } 44 } 45 if j < n {b += Int(equation.subString(j))! * sign} 46 if a == 0 && a == b {return "Infinite solutions"} 47 if a == 0 && a != b {return "No solution"} 48 return "x=" + String(-b / a) 49 } 50 } 51 52 extension String { 53 // 截取字符串:指定索引和字符數 54 // - begin: 開始截取處索引 55 // - count: 截取的字符數量 56 func subString(_ begin:Int,_ count:Int) -> String { 57 let start = self.index(self.startIndex, offsetBy: max(0, begin)) 58 let end = self.index(self.startIndex, offsetBy: min(self.count, begin + count)) 59 return String(self[start..<end]) 60 } 61 62 // 截取字符串:從index到結束處 63 // - Parameter index: 開始索引 64 // - Returns: 子字符串 65 func subString(_ index: Int) -> String { 66 let theIndex = self.index(self.endIndex, offsetBy: index - self.count) 67 return String(self[theIndex..<endIndex]) 68 } 69 }
8ms
1 class Solution { 2 func solveEquation(_ equation: String) -> String { 3 let parts = equation.components(separatedBy: "=") 4 let res1 = resolve(parts[0]), res2 = resolve(parts[1]) 5 let n = (res2.1 - res1.1) 6 let x = (res1.0 - res2.0) 7 if n == 0 && x == 0 { return "Infinite solutions" } 8 if x == 0 { return "No solution" } 9 return "x=\(n/x)" 10 } 11 12 private func resolve(_ e: String) -> (Int, Int) { 13 var x = 0, n = 0 14 var num = 0 15 var isX = false 16 var sign = 1 17 var ca = Array(e) 18 ca.append("+") 19 for i in 0..<ca.count { 20 let c = ca[i] 21 if let digit = Int(String(c)) { 22 num *= 10 23 num += digit 24 } else if c == "+" || c == "-" { 25 if isX { 26 if i <= 1 || ca[i-2] != "0" { 27 num = num == 0 ? 1 : num 28 } 29 x += num * sign 30 } else { 31 n += num * sign 32 } 33 sign = c == "+" ? 1 : -1 34 num = 0 35 isX = false 36 } else if c == "x" { 37 isX = true 38 } 39 } 40 return (x, n) 41 } 42 }