-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1422.java
More file actions
38 lines (34 loc) · 959 Bytes
/
LeetCode1422.java
File metadata and controls
38 lines (34 loc) · 959 Bytes
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
public class LeetCode1422 {
public static void main(String[] args) {
// 输入:s = "011101"
// 输出:5
System.out.println(new Solution1422().maxScore("011101"));
// 输入:s = "00111"
// 输出:5
System.out.println(new Solution1422().maxScore("00111"));
// 输入:s = "1111"
// 输出:3
System.out.println(new Solution1422().maxScore("1111"));
}
}
class Solution1422 {
public int maxScore(String s) {
int ans = 0;
int left = 0;
int right = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1') {
right++;
}
}
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == '1') {
right--;
} else {
left++;
}
ans = Math.max(ans, left + right);
}
return ans;
}
}