zoukankan      html  css  js  c++  java
  • 058 Length of Last Word 最后一个单词的长度

    给定一个字符串, 包含大小写字母、空格 ' ',请返回其最后一个单词的长度。
    如果不存在最后一个单词,请返回 0 。
    注意事项:一个单词的界定是,由字母组成,但不包含任何的空格。
    案例:
    输入: "Hello World"
    输出: 5
    详见:https://leetcode.com/problems/length-of-last-word/description/

    Java实现:

    class Solution {
        public int lengthOfLastWord(String s) {
            int n=s.length();
            if(n==0||s.isEmpty()){
                return 0;
            }
            int cnt=0;
            int right=n-1;
            while(right>=0&&s.charAt(right)==' '){
                --right;
            }
            while(right>=0&&s.charAt(right)!=' '){
                --right;
                ++cnt;
            }
            return cnt;
        }
    }
  • 相关阅读:
    Python Day7(相关补充)
    Python Day7
    Python Day6
    Python Day5
    Python Day4
    Python Day3
    Python Day2
    Python Day1
    复杂装饰器原理分析
    Unity 坐标
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8698643.html
Copyright © 2011-2022 走看看