LeetCode string related questions use String related methods to solve the problem

Question 1: Binary Summation

LeetCode 67:
Description:
Given two binary strings, return their sum (in binary).
The input is a non-empty string and contains only the numbers 1 and 0.
insert image description here

Problem solving ideas:

1. The number represented by the string, traverse the string, and add from the back to the front. The number of traversals is the longest of the two strings.
2. Use ret to determine whether to carry over, add from the end, and put the string into a single Characters are converted to int type and added. The added value is judged>=2Just carry.
3. Finally, after traversing, ifretIf it is still equal to 1, it needs to carry one more time.
4. UseStringBuilderorStringBufferConcatenate the obtained numbers.
5. Because the search from the back to the front, you also need to reverse the string.

Drawing analysis:

insert image description here

Code:

class Solution {
    
    
    public String addBinary(String a, String b) {
    
    
        StringBuilder sb = new StringBuilder();
        int n1 = a.length();
        int n2 = b.length();
        int max = n1 > n2 ? n1 : n2;
        int ret = 0;
        while(max-- != 0){
    
    
            int a1 = --n1 >= 0 ? a.charAt(n1) - '0' : 0;
            int b1 = --n2 >= 0 ? b.charAt(n2) - '0' : 0;
            sb.append((a1+b1+ret)%2);
            ret = (a1+b1+ret) >= 2 ? 1 : 0;
        }
        if(ret==1){
    
     sb.append(ret);}
        sb.reverse();
        return sb.toString();
    }
}

Question 2: Consecutive Characters

LeetCode 1446:
Description: Given a string s, the "energy" of the string is defined as: the length of the longest non-empty substring containing only one type of character.
Please return the energy of the string.
insert image description here

Problem solving ideas:

1. Traverse the string, add 1 if the two characters are equal, and store it with count.
2. There may be multiple equal places, such as strings“leeeetcooooood”
3. Traverse, whens[i] = s[i-1]when, count++, whens[i] != s[i-1], count = 1;
4. Record the maximum value of count maxcount
5. Return maxcount at the end of the traversal

Drawing analysis:

insert image description here

Code:

class Solution {
    
    
    public int maxPower(String s) {
    
    
        int n = s.length();
        int maxcount = 1;
        int count = 1;
        for(int i = 1 ; i < n;i++){
    
    
            char ch = s.charAt(i);
            char ch1 = s.charAt(i-1);
            if(ch == ch1){
    
    
                count++;
                maxcount = Math.max(maxcount,count);
            }else{
    
    
                count = 1;
            }
            
        }
        return maxcount;
    }
}

Question 3: Flip the words in the string

LeetCode 151:
Description:
Given a string s, flip all the words in the string one by one.

A word is a string of non-whitespace characters. Use at least one space in s to separate the words in the string.

Please return a string that reverses the order of the words in s and joins them with a single space.

illustrate:

  • The input string s can contain extra spaces before, after, or between words.
  • After flipping, words should be separated by only one space.
  • There should be no extra spaces in the flipped string.

insert image description here

Problem solving ideas:

1. UsesplitSplit string:
        Parameters of split method:
          ①\\sIndicates blank characters such as spaces
, carriage returns, and line           feeds②+ signIndicates the meaning of one
or more 2. Traversal using StringBuilderappendlink with a space" ";
3. Usetrim()Remove the extra spaces on the right

Drawing analysis:

insert image description here

Code:

class Solution {
    
    
    public String reverseWords(String s) {
    
    
    	// 这里的\\s+ 表示按空白部分进行拆分
        String[] str = s.split("\\s+");
        StringBuilder sb = new StringBuilder();
        for(int i = str.length - 1; i >= 0 ;i--){
    
    
            sb.append(str[i]+" ");
        }
        return sb.toString().trim();
    }
}

Guess you like

Origin blog.csdn.net/wwzzzzzzzzzzzzz/article/details/121639204