leetcode 58

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

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

这道题比较简单,注意考虑字符串最后的空格以及全为空格的字符串的这几种特殊情况。

代码如下:

 1 class Solution {
 2 public:
 3     int lengthOfLastWord(string s) {
 4         if(s.length() == 0)
 5         {
 6             return 0;
 7         }
 8         int n = 0;
 9         int l = s.length();
10         for(int i = l-1; i >= 0; i--)
11         {
12             if(n == 0 && s[i] == ‘ ‘)
13             {
14                 continue;
15             }
16             if(s[i] != ‘ ‘)
17             {
18                 n ++;
19             }
20             else
21             {
22                 break;
23             }
24         }
25         return n;
26     }
27 };
时间: 2024-08-30 10:41:31

leetcode 58的相关文章

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

https://leetcode.com/problems/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 de

Leetcode 58 Length of Last Word 难度:0

https://leetcode.com/problems/length-of-last-word/ int lengthOfLastWord(char* s) { int ans = 0; int fans = 0; for(int i = 0;s[i];i++){ if (s[i] ==' '){fans = ans;ans = 0;while(s[i + 1] == ' '){i++;}} else ans++; } return ans?ans:fans; }

LeetCode 58 - II. 左旋转字符串

面试题58 - II. 左旋转字符串 难度简单 字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部.请定义一个函数实现字符串左旋转操作的功能.比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab". 示例 1: 输入: s = "abcdefg", k = 2 输出: "cdefgab" 示例 2: 输入: s = "lrloseumgh", k = 6

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

problem: 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#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 58 Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路:与上一篇类似.仅仅要不越界就ok public class Solution { pu