LeetCode – Refresh – Length of Last Word

Tried it with spliting words method. But need to check empty situation.

 1 class Solution {
 2 public:
 3     int lengthOfLastWord(const char *s) {
 4         int len = strlen(s);
 5         if (len == 0) return 0;
 6         for (int i = len-1; i >= 0; i--) {
 7             if (s[i] == ‘ ‘) {
 8                 len = i;
 9             } else if (i == 0 || s[i-1] == ‘ ‘) {
10                 return len - i;
11             }
12         }
13     }
14 };

This method uses a flag to record whether we have encountered real chars before.

 1 class Solution {
 2 public:
 3     int lengthOfLastWord(const char *s) {
 4         int len = strlen(s), result = 0;
 5         bool flag = false;
 6         for (int i = len-1; i >= 0; i--) {
 7             if (s[i] == ‘ ‘) {
 8                 if (flag) return result;
 9             } else {
10                 flag = true;
11                 result++;
12             }
13         }
14         return result;
15     }
16 };
时间: 2024-10-13 15:39:05

LeetCode – Refresh – 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