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 characters only.

For example, 
Given s = "Hello World",
return 5.

这道题整体很简单

刚开始这道题想的太简单了,首先最开始的想法是得知s的长度,然后从头开始遍历找到最后一个空格的位置,作减法就可以得到最后单词的长度

然而忽略了,如果这个字符串结尾有空格的情况。如“a    ”

于是改变思路,从后面开始遍历,找到最后一个单词的最后一个字符开始计数,然后依次向前知道遇到第一个空格。

下面附上代码:

public static int lengthOfLastWord(String s) {

		int length = 0;
		char[] chars = s.toCharArray();
		for (int i = s.length() - 1; i >= 0; i--) {
			if (length == 0) {
				if (chars[i] == ‘ ‘) {
					continue;
				} else {
					length++;
				}
			} else {
				if (s.charAt(i) == ‘ ‘) {
					break;
				} else {
					length++;
				}
			}
		}

		return length;
	}

  

时间: 2024-10-12 13:02:05

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 cha

leetcode 题解: Length of Last Word

leetcode: 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

【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

[LeetCode][JavaScript]Length of Last Word

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 consi

Java [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-spa

leetCode 58. Length of Last Word 字符串

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 c

LeetCode#58 Length of Last Word

Problem Definition: 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 consi

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

[leetcode] 18. 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 charact

Java for LeetCode 058 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