zoukankan      html  css  js  c++  java
  • [Swift]LeetCode640. 求解方程 | Solve the Equation

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址: https://www.cnblogs.com/strengthen/p/10479684.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.

    If there is no solution for the equation, return "No solution".

    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.

    Example 1:

    Input: "x+5-3+x=6+x-2"
    Output: "x=2" 

    Example 2:

    Input: "x=x"
    Output: "Infinite solutions" 

    Example 3:

    Input: "2x=x"
    Output: "x=0" 

    Example 4:

    Input: "2x+3x-6x=x+2"
    Output: "x=-1" 

    Example 5:

    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"

    Runtime: 4 ms
    Memory Usage: 20 MB
     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 }
  • 相关阅读:
    理解SynchronizationContext,如何在Winform里面跨线程访问UI控件
    ThreadPool.QueueUserWorkItem引发的血案,线程池异步非正确姿势导致程序闪退的问题
    实战经验分享之C#对象XML序列化
    C#wxpay和alipay
    C#调用windows api 实现打印机控制
    C#winform程序关闭计算机的正确姿势
    自动化控制之线程池的使用
    自动化控制之重码校验
    (转)C#中的那些全局异常捕获
    android studio 2.32躺坑记
  • 原文地址:https://www.cnblogs.com/strengthen/p/10479684.html
Copyright © 2011-2022 走看看