[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.

Hide Tags

String


    这题比较简单,只是可能前面有空格,后面有空格。- -

算法逻辑:

  1. 排除最后的空格
  2. index 从后往前查第一个空格。
  3. 返回长度。

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4
 5
 6 class Solution {
 7 public:
 8     int lengthOfLastWord(const char *s) {
 9         int n = strlen(s);
10         if(n <1)    return 0;
11         int idx=n-1;
12         while(idx>=0&&s[idx]==‘ ‘)  idx--;
13         n = idx+1;
14         while(idx>=0){
15             if (s[idx]==‘ ‘)  break;
16             idx --;
17         }
18 //        if(idx<0)   return 0;
19         return n - idx -1;
20     }
21 };
22
23 int main()
24 {
25     char s[] = " 12   ";
26     Solution sol;
27     cout<<sol.lengthOfLastWord(s)<<endl;
28     return 0;
29 }

时间: 2025-01-18 07:45:25

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

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] 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

LeetCode Length of Last Word

1. 题目 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 : Length of Last Word [基本功]

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #333333 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #333333; background-color: #ffffff } p.p3 { margin: 0.0px 0.0px 0.0px

Leetcode: Length of Last Word in python

Length of Last Word Total Accepted: 47690 Total Submissions: 168587 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:

leetcode Length of Last Word(easy) /java

坑主要在处理空格.因为字符串之间不止有一个空格. import java.io.*; import java.util.*; public class Solution { public static int lengthOfLastWord(String s) { int r=0; int len=s.length(); if(len==0) return 0; int i,j; char[] c=s.toCharArray(); int flag=1; for(i=0;i<len;i++)

LeetCode Length of Last Word 最后一个字的长度

1 class Solution { 2 public: 3 int lengthOfLastWord(const char *s) { 4 if(s=="") return 0; 5 string snew=s; 6 int n=0,len=strlen(s); 7 char *p=&snew[len-1]; 8 while(*p==' '&&len!=0){ 9 *p--; 10 len--; 11 } 12 while(*p!=' '&&l