【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));
	}
}

【LeetCode】- Length of Last Word(最后一个单词的长度),码迷,mamicode.com

时间: 2024-11-06 20:48:10

【LeetCode】- Length of Last Word(最后一个单词的长度)的相关文章

[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. Note: A word is defined as a character sequence consists of non-space char

58. Length of Last Word最后一个单词的长度

[抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误判lastindexof().所以必须用.trim() [思维问题]: [一句话思路]: 用函数 再次强调是最后一位的索引是length() - 1 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [二刷]: [三刷]: [四刷]: [

[LeetCode] 58. 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 cha

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 cha

leetcode—58 Length of Last Word Total(字符串中最后一个单词的长度)

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 cha

leetcode 58. 最后一个单词的长度(Length of Last Word)

目录 题目描述: 示例: 解法: 题目描述: 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: "Hello World" 输出: 5 解法: class Solution { public: int lengthOfLastWord(string s) { int sz = s.size(); int i = 0, j = 0; string

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. Note: A word is defined as a character sequence consists of non-space cha

【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】

[058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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

LeetCode: Length of Last Word [057]

昨天同事在做主从时,从库报如下错误: Got fatal error 1236 from master when reading data from binary log: 'Misconfigured master - server id was not set' 粗粗看好像是master的server-id没有设置,但同事做如下查询: 备库采集: [email protected] Fri May 23 14:18:59 2014 14:18:59 [(none)]> show variab