Issue 34: The length of the last word (high frequency)

I have prepared 1,000 e-books and 100 high-definition mind maps in various fields of computer, and I will reply to [Resources] after paying attention to get it! You can even reply [introduction] to join the BAT introduction group!
Issue 34: The length of the last word (high frequency)

01. Sample question


This is a simple question, which can be completed by ordinary traversal. But there will be some pitfalls, if you don't pay attention, it is still quite easy to make mistakes.


Question 58: The length of the last word


Given a string s containing only uppercase and lowercase letters and spaces'', return the length of its last word. If the string scrolls from left to right, then the last word is the last word that appears.


Example:

输入: "Hello World" 
输出: 5

Description : a word refers only of letters, does not contain any space characters maximum substring.

02, problem analysis


Because what we want to get is the length of the last word , it is not difficult to think that we can traverse from the end.

The catch in the question is that there may still be spaces at the end.

So the general idea of ​​solving the problem is to remove the trailing spaces first, and then traverse from the end to the front, until it ends at the first space.

But here is a trick, we can count by a count, starting from the first number that is not a space . In other words, if there is a space at the end, the count value is 0 and you can skip it directly.
Issue 34: The length of the last word (high frequency)

The implementation code is as follows:


//JAVA
class Solution {    
    public int lengthOfLastWord(String s) {        
        if(s == null || s.length() == 0) return 0;        
        int count = 0;        
        for(int i = s.length()-1; i >= 0; i--){            
            if(s.charAt(i) == ' '){                
                if(count == 0) continue;               
                break;           
            }            
            count++;      
        }        
        return count;          
    }
}

Of course, it's not that we can't directly use APIs to solve some "witty" problems. The big deal is to be hung.

//java
class Solution {
    public int lengthOfLastWord(String s) {
        s = s.trim();
        int start = s.lastIndexOf(" ") + 1;
        return s.substring(start).length();
    }

}
This code should be understood by everyone. First trim off the spaces on both sides, then directly locate the last word, intercept it to get the length.

amount. Now that we have used trim, why don't we directly use split to get the length of the last word?

//java
public class Solution {
    public int lengthOfLastWord(String s) {
        String[] words = s.split(" ");
        if (words.length < 1) return 0;
        return words[words.length - 1].length();
    }
}

03, function learning


Trim is used in the above solution. In addition to removing the spaces on both sides, does trim also remove other characters? Let's take a look at the source code of trim.

//JAVA
public String trim() {  
    int len = value.length;  
    int st = 0;  
    char[] val = value;    /* avoid getfield opcode */  
    while ((st < len) && (val[st] <= ' ')) {      
        st++;  
    }  
    while ((st < len) && (val[len - 1] <= ' ')) {      
        len--;  
    }  
    return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
}

As you can see, in addition to removing spaces, the trim function in Java also removes all characters that rank less than or equal to spaces in the ASCII code table.
Issue 34: The length of the last word (high frequency)

The ranking of spaces in the ASCII code table is 32 bits. You can see that tab, line feed, carriage return, etc. are all within the control range of trim.

Guess you like

Origin blog.51cto.com/15076236/2608550