LeetCode58_Length of Last Word

题目:

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.

Example:

Input: "Hello World"
Output: 5

题目比较简单,输入最后一个单词的长度,这里的数组只包含字母和空格。

主要思路是,遇到字母的时候count++;遇到空格的时候count=0。

应该注意的有几个地方:

1.空数组的情况应该返回0

2.只含空格的数组应该返回0

3.如果遇到空格将count重新置0的话,此时空格后面的元素必须为字母,不能是空格,也不能是结束符。

4.如果想用字母直接判断的话,

数字0-9的ASCII码为:048到057;


大写字母A-Z的ASCII为:065-090;

小写字母a-z的ASCII为:097到122;

5.c语言里记得使用strlen函数返回数组长度。

先贴自己的第一版代码:

int lengthOfLastWord(char* s) {
    int i=0,count=0;
    if(s[0]=='\0'){return 0;}//考虑数组为空,但其实并不需要这个判断:如果为空的话不会进入while循环,count会保持初值0
    while(s[i]!='\0'){
        if(s[i]==' '){count=0;}//没有考虑全面,如果遇到空格将count重新置0的话,此时空格后面的元素必须为字母,不能是空格,也不能是结束符。
        else{count++;}
        i++;
    }
  return count;  

}

最后提交的代码:

int lengthOfLastWord(char* s) {
    int i,count=0;
    int len=strlen(s);
    for(i=0;i<len;i++){
        if(s[i]==' '&&s[i+1]!=' '&&s[i+1]!='\0'){count=0;}
        else{
            if(s[i]!=' '){count++;}//如果用复杂的条件判断是否重置0,注意不要将count++简单的放在对应的else里,因为此时的else范围很大并不只是字母的情况
        }
    }
  return count;  
}

java版本的代码:

第一次写的思路是,从后往前判断,遇到空格就return,如果一直遇不到就return字符串的长度。

class Solution {
    public int lengthOfLastWord(String s) {
        char[] str=s.toCharArray();
        int len=str.length;
        for(int i=len-1;i>=0;i--){
            if(str[i]!=' '){
                len=i+1;
                break;
            }
        }
//第一次写这段的时候没有考虑最后一位出现空格的情况,比如'a '这种。这样处理是先得到了不带末尾空格的字符长度。
        for(int i=len-1;i>=0;i--){
            if(str[i]==' '){
                return len-i-1;
            }
        }
        return len;
    }
}

 

第二次看了博客里原来的思路,从前往后判断,遇到空格就count置零,不是空格的时候count++

01版本

class Solution {
    public int lengthOfLastWord(String s) {
        char[] str=s.toCharArray();
        int len=str.length;
        int count=0;
        for(int i=0;i<len;i++){
            if(str[i]!=' '){
                count++;
            }
            else{
                count=0;
            }
        }
        return count;
    }
}//写这段的时候没有考虑最后一位出现空格的情况,比如'a '这种。

02版本

class Solution {
    public int lengthOfLastWord(String s) {
        char[] str=s.toCharArray();
        int len=str.length;
        int count=0;
        for(int i=0;i<len;i++){
            if(str[i]!=' '){
                count++;
            }
            else if(str[i]==' '&&str[i+1]!=' '){//因为数组范围而抛出异常
                count=0;
            }
        }
        return count;
    }
}

03版本

class Solution {
    public int lengthOfLastWord(String s) {
        char[] str=s.toCharArray();
        int len=str.length;
        int count=0;
        if(len==0){return 0;}//排除空字符串
        if(str[0]!=' '){count++;}//初始化count,如果第一个值不为空的话为1,为空的话维持0
        for(int i=1;i<len;i++){
            if(str[i]!=' '){
                if(str[i-1]==' '){
                    count=1;
                }
                else{count++;}
            }//分为两种情况,字符不为空,字符不为空且前一字符为空。前者count++;后者count=1;
        }
        return count;
    }
}

04版本

class Solution {
    public int lengthOfLastWord(String s) {
        int count=0;
        char[] str=s.toCharArray();
        int len=str.length;
        for(int i=0;i<len;i++){
            if(str[i]==' '&&str[i+1]!=' '&&str[i+1]!='\0'){count=0;}//i+1会超出数组范围,与语言的strlen返回值不一样
            else{
                if(str[i]!=' '){count++;}
            }
        }
        return count;
    }
}

05版本

class Solution {
    public int lengthOfLastWord(String s) {
        int count=0;
        char[] str=s.toCharArray();
        int len=str.length;
        if(len==0){return 0;}//注意处理空字符串
        if(str[0]!=' '){count++;}
        for(int i=1;i<len;i++){
            if(str[i-1]==' '&&str[i]!=' '&&str[i]!='\0'){count=1;}
            else{
                if(str[i]!=' '){count++;}
            }
        }
        return count;
    }
}

猜你喜欢

转载自blog.csdn.net/lilililililydia/article/details/81044378