LeetCode 最后一个单词的长度

版权声明:欢迎提问:[email protected] https://blog.csdn.net/include_heqile/article/details/81813993

asdsa

https://leetcode-cn.com/problems/length-of-last-word/submissions/1

我的解决方案:

class Solution {
    public int lengthOfLastWord(String s) {
        if(s.equals("")) return 0;
        int i=s.length()-1;
        int len=0;
        while(i>=0&&s.charAt(i)==' ') {
            len++;
            i--;
        }
        len=s.length()-len;
        if(len==0) return 0;
        else
            s=s.substring(0, len); 
        int res=0;  
        for(int j=i;j>=0;j--) {
            if(s.charAt(j)==' '||j==0) {
                if(s.charAt(j)==' ')
                    res = i-j;
                else if(j==0)
                    res = i-j+1; 
                break;
            }
        }
        return res;  
    }
}

在我提交的时候,我的耗时是最短的。。。。。

猜你喜欢

转载自blog.csdn.net/include_heqile/article/details/81813993