Skip to content

Latest commit

 

History

History
291 lines (249 loc) · 6.16 KB

File metadata and controls

291 lines (249 loc) · 6.16 KB

English Version

题目描述

给你一个由若干 0 和 1 组成的字符串 s ,请你计算并返回将该字符串分割成两个 非空 子字符串(即  子字符串和 子字符串)所能获得的最大得分。

「分割字符串的得分」为 子字符串中 0 的数量加上 子字符串中 1 的数量。

 

示例 1:

输入:s = "011101"
输出:5 
解释:
将字符串 s 划分为两个非空子字符串的可行方案有:
左子字符串 = "0" 且 右子字符串 = "11101",得分 = 1 + 4 = 5 
左子字符串 = "01" 且 右子字符串 = "1101",得分 = 1 + 3 = 4 
左子字符串 = "011" 且 右子字符串 = "101",得分 = 1 + 2 = 3 
左子字符串 = "0111" 且 右子字符串 = "01",得分 = 1 + 1 = 2 
左子字符串 = "01110" 且 右子字符串 = "1",得分 = 2 + 1 = 3

示例 2:

输入:s = "00111"
输出:5
解释:当 左子字符串 = "00" 且 右子字符串 = "111" 时,我们得到最大得分 = 2 + 3 = 5

示例 3:

输入:s = "1111"
输出:3

 

提示:

  • 2 <= s.length <= 500
  • 字符串 s 仅由字符 '0''1' 组成。

解法

Python3

class Solution:
    def maxScore(self, s: str) -> int:
        return max(s[:i].count('0') + s[i:].count('1') for i in range(1, len(s)))
class Solution:
    def maxScore(self, s: str) -> int:
        ans = t = (s[0] == '0') + s[1:].count('1')
        for i in range(1, len(s) - 1):
            t += 1 if s[i] == '0' else -1
            ans = max(ans, t)
        return ans

Java

class Solution {
    public int maxScore(String s) {
        int ans = 0;
        for (int i = 1; i < s.length(); ++i) {
            int t = 0;
            for (int j = 0; j < i; ++j) {
                if (s.charAt(j) == '0') {
                    ++t;
                }
            }
            for (int j = i; j < s.length(); ++j) {
                if (s.charAt(j) == '1') {
                    ++t;
                }
            }
            ans = Math.max(ans, t);
        }
        return ans;
    }
}
class Solution {
    public int maxScore(String s) {
        int t = 0;
        if (s.charAt(0) == '0') {
            t++;
        }
        for (int i = 1; i < s.length(); ++i) {
            if (s.charAt(i) == '1') {
                t++;
            }
        }
        int ans = t;
        for (int i = 1; i < s.length() - 1; ++i) {
            t += s.charAt(i) == '0' ? 1 : -1;
            ans = Math.max(ans, t);
        }
        return ans;
    }
}

C++

class Solution {
public:
    int maxScore(string s) {
        int ans = 0;
        for (int i = 1, n = s.size(); i < n; ++i) {
            int t = 0;
            for (int j = 0; j < i; ++j) {
                t += s[j] == '0';
            }
            for (int j = i; j < n; ++j) {
                t += s[j] == '1';
            }
            ans = max(ans, t);
        }
        return ans;
    }
};
class Solution {
public:
    int maxScore(string s) {
        int t = 0;
        if (s[0] == '0') ++t;
        for (int i = 1; i < s.size(); ++i) t += s[i] == '1';
        int ans = t;
        for (int i = 1; i < s.size() - 1; ++i) {
            t += s[i] == '0' ? 1 : -1;
            ans = max(ans, t);
        }
        return ans;
    }
};

Go

func maxScore(s string) int {
	ans := 0
	for i, n := 1, len(s); i < n; i++ {
		t := 0
		for j := 0; j < i; j++ {
			if s[j] == '0' {
				t++
			}
		}
		for j := i; j < n; j++ {
			if s[j] == '1' {
				t++
			}
		}
		ans = max(ans, t)
	}
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func maxScore(s string) int {
	t := 0
	if s[0] == '0' {
		t++
	}
	n := len(s)
	for i := 1; i < n; i++ {
		if s[i] == '1' {
			t++
		}
	}
	ans := t
	for i := 1; i < n-1; i++ {
		if s[i] == '0' {
			t++
		} else {
			t--
		}
		ans = max(ans, t)
	}
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

TypeScript

function maxScore(s: string): number {
    const n = s.length;
    let res = 0;
    let score = 0;
    if (s[0] === '0') {
        score++;
    }
    for (let i = 1; i < n; i++) {
        if (s[i] === '1') {
            score++;
        }
    }
    res = Math.max(res, score);
    for (let i = 1; i < n - 1; i++) {
        if (s[i] === '0') {
            score++;
        } else if (s[i] === '1') {
            score--;
        }
        res = Math.max(res, score);
    }
    return res;
}

Rust

impl Solution {
    pub fn max_score(s: String) -> i32 {
        let n = s.len();
        let mut res = 0;
        let mut score = 0;
        let bs = s.as_bytes();
        if bs[0] == b'0' {
            score += 1;
        }
        for i in 1..n {
            if bs[i] == b'1' {
                score += 1;
            }
        }
        res = res.max(score);
        for i in 1..n - 1 {
            if bs[i] == b'0' {
                score += 1;
            } else if bs[i] == b'1' {
                score -= 1;
            }
            res = res.max(score);
        }
        res
    }
}

...