zoukankan      html  css  js  c++  java
  • 【LeetCode】- 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.

    给你一个字符串,设法获取它最后一个单词的长度。假设这个单词不存在,则返回0。

    [ 分析 ]

    A word is defined as a character sequence consists of non-space characters only.
    For example, 
    Given s = "Hello World",
    return 5.

    [ 解法]

    public class Solution {
    
    	public int lengthOfLastWord(String s) {
    		if (s != null && !s.trim().equals("")) {
    			String[] arr = s.trim().split(" ");
    			int length = arr[arr.length - 1].length();
    			return length;
    		}
    		return 0;
    	}
    
    	public static void main(String[] args) {
    		String s = "leet code";
    		System.out.println(new Solution().lengthOfLastWord(s));
    	}
    }


  • 相关阅读:
    计网 | 文件传输协议
    Java | JDK8 | Integer
    2.项目管理-应用工具
    1.需求管理
    1.短视频运营基础
    10.视频效果---变形稳定器
    9.时间重映射
    8.效果控件之移动&&缩放
    7.标记
    6.子剪辑与合并剪辑
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/3756688.html
Copyright © 2011-2022 走看看