[LeetCode#58]Length of Last Word

The problem:

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example, 
Given s = "Hello World",
return 5.

My first analysis:

In order to solve this problem, we need to consider all cornor cases very carefully.
The key idea: find out the boundary of last word.
The method I used in this solution is, 
1. find out the pre index(target_index) of the last word. the S[target_index] must be ' '. It could involve the following situations:
1.1 "   word    "
1.2 "word       "
1.3 "word1      word2"
1.4 "    "
Apparently, we could not update the ' ' as the pre index of the last word, cause at the situation 1.1 and 1.2, the ' ' may be followed by no words. Now, the checking condition is complex! how could we get the right pre index of last word. The idea is to 

2. use the indexes "temp_index" and "target_index".
2.1 once we encounter a ' ', we update the temp_index as this index. 
2.2 iff the string contains a not ' 'char, we update the target_index as temp_index, which is the case 1.3
    iff all chars after S[temp_index] is ' ', we ignore the temp_index, which is the case 1.1, 1.2
Note: at the scan process, we could also record the end index of the last word.
for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) == ' ') {
        temp_index = i;
    }
    if (s.charAt(i) != ' ' && i > temp_index) {
        target_index = temp_index;
        end_index = i; 
    }
}

My first Solution:

public class Solution {
    public int lengthOfLastWord(String s) {
        if (s == null || s.length() == 0)
            return 0;
        int temp_index = -1;//to keep the invariant!
        int target_index = -1;
        int end_index = -1;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                temp_index = i;
            }
            if (s.charAt(i) != ' ' && i > temp_index) {
                target_index = temp_index;
                end_index = i; 
            }
        }
            
        return end_index - target_index;//start: last_index - 1; end:s.length() - 1
    }
}

My improved analysis:

The problem could be solved in a very elegant way by changing the thinking pattern.
???since we only care about the last word in the string, why must we need to scan from the string's beginning?
Why not we search the word from the last character of the string?
This way's idea is clear and instant.
1. To locate the end index of last word:
int end = s.length() - 1;
while (end >= 0 && s.charAt(end) == ' ') 
        end--;
note: use illegal condition "charAt(end) == ' '", we would finally stop the first legal condition word.

2. To locate the start index of last word:
skill: we could not decide whether current index is the start index(since we don't know iff the pre index is ' ' or not), but we could use the "s.charAt(start) != ' '" as loop condition. thus, we would stop at the pre index of start index.
int start = end; 
while (start >= 0 && s.charAt(start) != ' ') 
        start--;
Note: int start = end is very important, from here we enter into a different state!!! use while loop to scan through a string is a very important skill, you must master it!!! 
key:
1. use the right loop codition, and stop at the right place.
2. note the state transfer(from illegal to legal, then legal to legal, and then legal to illegal). 
Use the while to scan through the string is great!

My improved solution:

public class Solution {
    public int lengthOfLastWord(String s) {
        if (s == null || s.length() == 0)
            return 0;
        int end = s.length() - 1;
        while (end >= 0 && s.charAt(end) == ' ')
            end--;
        int start = end;
        while (start >= 0 && s.charAt(start) != ' ') 
            start--;
        return end - start;//start is the pre index of the last word!!
    }
}
原文地址:https://www.cnblogs.com/airwindow/p/4227555.html