640.求解方程 精简代码
This commit is contained in:
parent
bebc20c212
commit
4b5520d210
|
@ -26,13 +26,7 @@ const solveEquation = function (equation) {
|
|||
}
|
||||
}
|
||||
b += sign * left * c;
|
||||
if (a === 0 && b === 0) {
|
||||
return "Infinite solutions";
|
||||
} else if (a === 0) {
|
||||
return "No solution";
|
||||
} else {
|
||||
return "x=" + -b / a;
|
||||
}
|
||||
return a === 0 ? b === 0 ? "Infinite solutions" : "No solution" : "x=" + -b / a;
|
||||
};
|
||||
|
||||
console.assert(solveEquation("x+5-3+x=6+x-2") === "x=2");
|
|
@ -20,13 +20,7 @@ function solveEquation(equation: string): string {
|
|||
}
|
||||
}
|
||||
b += sign * left * c;
|
||||
if (a === 0 && b === 0) {
|
||||
return "Infinite solutions";
|
||||
} else if (a === 0) {
|
||||
return "No solution";
|
||||
} else {
|
||||
return "x=" + -b / a;
|
||||
}
|
||||
return a === 0 ? b === 0 ? "Infinite solutions" : "No solution" : "x=" + -b / a;
|
||||
}
|
||||
|
||||
console.assert(solveEquation("x+5-3+x=6+x-2") === "x=2");
|
|
@ -56,12 +56,7 @@ namespace my_leetcode
|
|||
}
|
||||
|
||||
b += sign * left * c;
|
||||
return a switch
|
||||
{
|
||||
0 when b == 0 => "Infinite solutions",
|
||||
0 => "No solution",
|
||||
_ => "x=" + -b / a
|
||||
};
|
||||
return a == 0 ? b == 0 ? "Infinite solutions" : "No solution" : "x=" + -b / a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
project(my_leetcode)
|
||||
include_directories(src)
|
||||
|
||||
|
|
|
@ -149,8 +149,8 @@ public:
|
|||
* @return 解
|
||||
* @see <a href="https://leetcode-cn.com/problems/solve-the-equation/">原题链接</a>
|
||||
*/
|
||||
string solveEquation(const string& equation) {
|
||||
int a = 0, b = 0, c = 0, sign = 1, left = 1;
|
||||
static string solveEquation(const string& equation) {
|
||||
/*int a = 0, b = 0, c = 0, sign = 1, left = 1;
|
||||
bool hasC = false;
|
||||
for(char z : equation) {
|
||||
switch (z) {
|
||||
|
@ -178,7 +178,26 @@ public:
|
|||
return "No solution";
|
||||
} else {
|
||||
return "x=" + std::to_string(-b / a);
|
||||
}
|
||||
}*/
|
||||
if (struct Status {
|
||||
int a = 0, b = 0, c = 0, sign = 1, left = 1, hasC = false;
|
||||
} s = std::accumulate(equation.begin(), equation.end(), Status{}, [](Status s, char z) {
|
||||
switch (z) {
|
||||
case 'x':
|
||||
return Status{s.a + (s.hasC ? s.sign * s.left * s.c : s.sign * s.left), s.b, 0, s.sign, s.left, false};
|
||||
case '+':
|
||||
return Status{s.a, s.b + s.sign * s.left * s.c, 0, 1, s.left, false};
|
||||
case '-':
|
||||
return Status{s.a, s.b + s.sign * s.left * s.c, 0, -1, s.left, false};
|
||||
case '=':
|
||||
return Status{s.a, s.b + s.sign * s.left * s.c, 0, 1, -1, false};
|
||||
default:
|
||||
return Status{s.a, s.b, s.c * 10 + z - '0', s.sign, s.left, true};
|
||||
}
|
||||
}); s.a)
|
||||
return "x=" + std::to_string(-(s.b + s.sign * s.left * s.c) / s.a);
|
||||
else
|
||||
return s.b + s.sign * s.left * s.c ? "No solution" : "Infinite solutions";
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -127,12 +127,6 @@ public class Solution {
|
|||
}
|
||||
}
|
||||
b += sign * left * c;
|
||||
if (a == 0 && b == 0) {
|
||||
return "Infinite solutions";
|
||||
} else if (a == 0) {
|
||||
return "No solution";
|
||||
} else {
|
||||
return "x=" + -b / a;
|
||||
}
|
||||
return a == 0 ? b == 0 ? "Infinite solutions" : "No solution" : "x=" + -b / a;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -104,7 +104,5 @@ class Solution:
|
|||
case '=': b, c, sign, left, has_c = b + sign * left * c, 0, 1, -1, False
|
||||
case _: c, has_c = c * 10 + int(z), True
|
||||
b += sign * left * c
|
||||
match (a, b):
|
||||
case (0, 0): return "Infinite solutions"
|
||||
case (0, _): return "No solution"
|
||||
case _: return f"x={-b // a}"
|
||||
return f"x={-b // a}" if a != 0 else "Infinite solutions" if b == 0 else "Infinite solutions"
|
||||
|
||||
|
|
|
@ -80,9 +80,7 @@ impl Solution {
|
|||
_ => (a, b, c * 10 + (z - b'0') as i32, sign, left, true)
|
||||
}) {
|
||||
(a, b, c, sign, left, _) => match (a, b + sign * left * c) {
|
||||
(0, 0) => "Infinite solutions".to_owned(),
|
||||
(0, _) => "No solution".to_owned(),
|
||||
(_, b) => format!("x={}", -b / a)
|
||||
(a, b) => if a == 0 { if b == 0 { "Infinite solutions".to_owned() } else { "No solution".to_owned() } } else { format!("x={}", -b / a) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue