Skip to content

Commit 92ea613

Browse files
committed
6/2
1 parent 3140945 commit 92ea613

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Easy Leetcode 1422
2+
3+
Maximum Score After Splitting a String
4+
5+
tags: String
6+
7+
方法1:
8+
自己的方法纯intuitive就是遍历两遍
9+
10+
方法2:
11+
非常聪明的方法解法如下
12+
Logic behind this -
13+
Result = Max of (ZerosOnLeft + OnesOnRight)
14+
= Max of (ZerosOnLeft + (TotalOnes - OnesOnLeft))
15+
= Max of (ZerosOnLeft - OnesOnLeft) + TotalOnes (as TotalOnes is constant)
16+
17+
18+
19+
```
20+
21+
/*
22+
方法1
23+
*/
24+
25+
public int maxScore(String s) {
26+
int one = 0;
27+
for (int i = 0; i < s.length(); i++) if(s.charAt(i) == '1') one++;
28+
int left = 0;int max = 0;
29+
for (int i = 0; i < s.length()-1; i++) {
30+
if(s.charAt(i) == '1') {
31+
one--;
32+
}else{
33+
left++;
34+
}
35+
max = Math.max(max, left + one);
36+
}
37+
return max;
38+
}
39+
40+
/*
41+
方法2
42+
*/
43+
44+
public int maxScore(String s) {
45+
int zeros = 0, ones = 0, max = Integer.MIN_VALUE;
46+
for(int i=0;i<s.length();i++) {
47+
if(s.charAt(i) == '0') zeros++; else ones++;
48+
if(i != s.length()-1) max = Math.max(zeros - ones, max);
49+
}
50+
return max + ones;
51+
}

0 commit comments

Comments
 (0)