思路简单,先统计出所有的 1 的个数,然后遍历 s 的前 n-1 个字符,如果字符为 0 则 zeros 加一,否则 ones 减一,然后将比较 score 和 ones + zeros 较大值为 score ,循环结束得到的 score 就是答案。 解答 class Solution(object): def maxScore(self, s): """ :type s: str :rtype: int """...
class Solution { public int scoreOfParentheses(String s) { Deque<Character> deque = new ArrayDeque(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '(') deque.addLast('('); else { char c = deque.removeLast(); if (c == '(') { deque.addLast('1'); ...
【97】Interleaving String 【115】Distinct Subsequences 【125】Valid Palindrome 【126】Word Ladder II 【151】Reverse Words in a String(2018年11月8日, 原地翻转字符串) Input: "the sky is blue", Output: "blue is sky the". 题解:用stringstream能做。 1classSolution {2public:3voidreverseWords(s...
classSolution{publicintmaxScore(String s){int max=0;int n=s.length();for(int i=0;i<s.length()-1;i++){StringBuilder sb1=newStringBuilder();StringBuilder sb2=newStringBuilder();for(int j=0;j<=i;j++){sb1.append(s.charAt(j));}for(int j=i+1;j<n;j++){sb2.append(s.charAt(j...
n个孩子站成一排,一个数组score。1、每一个孩子至少有一个糖果。2、相邻的两个孩子分数高的获得更多的糖果。 输入:ratings = [1,0,2]输出:5解释:你可以分别给第一个、第二个、第三个孩子分发 2、1、2 颗糖果。 从左遍历,只判断左边规则得到ans1,从右边遍历,只判断右边规则得到ans2。求ans1和ans2对应...
Notes: 2. Examples: 3.Solutions: 1/**2* Created by sheepcore on 2019-05-093*/4classSolution {5publicintscoreOfParentheses(String S) {6Stack<Integer> stack =newStack<>();7for(charc : S.toCharArray()) {8if(c == '(') {9stack.push(-1);10}else{11intcur = 0;12while(stack.pe...
The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring. Example 1: Input: s = "011101" Output: 5 Explanation: All possible ways of splitting s into two non-empty substrings are: ...
1793 Maximum Score of a Good Subarray 53.2% Hard 1794 Count Pairs of Equal Substrings With Minimum Difference 65.2% Medium 1795 Rearrange Products Table 90.4% Easy 1796 Second Largest Digit in a String 49.0% Easy 1797 Design Authentication Manager 56.1% Medium 1798 Maximum Number of Conse...
1793.Maximum-Score-of-a-Good-Subarray (M+) 1989.Maximum-Number-of-People-That-Can-Be-Caught-in-Tag (M+) 2354.Number-of-Excellent-Pairs (H-) 2422.Merge-Operations-to-Turn-Array-Into-a-Palindrome (H-) Sliding window 532.K-diff-Pairs-in-an-Array (H-) 611.Valid-Triangle-Number (M+...
1classSolution {2publicString reorganizeString(String S) {3if(S ==null|| S.length() == 0){4returnS;5}67intn =S.length();8int[] map =newint[26];9intmaxCount = 0;10charmaxChar = 'a';11for(charc : S.toCharArray()){12map[c - 'a']++;13if(map[c - 'a'] >maxCount){...