forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
46 lines (45 loc) · 778 Bytes
/
Solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
func solveEquation(equation string) string {
f := func(s string) []int {
x, y := 0, 0
if s[0] != '-' {
s = "+" + s
}
i, n := 0, len(s)
for i < n {
sign := 1
if s[i] == '-' {
sign = -1
}
i++
j := i
for j < n && s[j] != '+' && s[j] != '-' {
j++
}
v := s[i:j]
if s[j-1] == 'x' {
a := 1
if len(v) > 1 {
a, _ = strconv.Atoi(v[:len(v)-1])
}
x += sign * a
} else {
a, _ := strconv.Atoi(v)
y += sign * a
}
i = j
}
return []int{x, y}
}
es := strings.Split(equation, "=")
a, b := f(es[0]), f(es[1])
x1, y1 := a[0], a[1]
x2, y2 := b[0], b[1]
if x1 == x2 {
if y1 == y2 {
return "Infinite solutions"
} else {
return "No solution"
}
}
return fmt.Sprintf("x=%d", (y2-y1)/(x1-x2))
}