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 consists of non-space characters only.

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class  Solution {
public :
     vector<string> stringSplit(string s,  const  char  * split)
     {
         vector<string> result;
         const  int  sLen = s.length();
         char  *cs =  new  char [sLen + 1];
         strcpy (cs, s.data());
         char  *p;
      
         p =  strtok (cs, split);
         while  (p)
         {
             printf ( "%s\n" , p);
             string tmp(p);
             result.push_back(tmp);
             p =  strtok (NULL, split);
         }
         return  result;
     }
     int  lengthOfLastWord(string s) {
         if (s.size() == 0)
             return  0;
         vector<string> words = stringSplit(s, " " );
         if (words.size() == 0)
             return  0;
         return  words[words.size() - 1].size();
     }
};



本文转自313119992 51CTO博客,原文链接:http://blog.51cto.com/qiaopeng688/1836629



相关文章
|
7天前
|
算法
力扣每日一题 6/23 字符串/模拟
力扣每日一题 6/23 字符串/模拟
5 1
|
7天前
力扣经典150题第四十题:同构字符串
力扣经典150题第四十题:同构字符串
6 1
|
21天前
|
算法 索引 Python
二刷力扣--字符串
二刷力扣--字符串
|
22天前
|
算法 容器
【LeetCode刷题】滑动窗口解决问题:水果成篮、找到字符串中所有字母异位词
【LeetCode刷题】滑动窗口解决问题:水果成篮、找到字符串中所有字母异位词
|
25天前
|
存储 算法 数据挖掘
深入解析力扣166题:分数到小数(模拟长除法与字符串操作详解及模拟面试问答)
深入解析力扣166题:分数到小数(模拟长除法与字符串操作详解及模拟面试问答)
|
7天前
|
索引
力扣每日一题 6/27 字符串 贪心
力扣每日一题 6/27 字符串 贪心
5 0
|
7天前
|
Python
力扣随机一题 模拟+字符串
力扣随机一题 模拟+字符串
6 0
|
7天前
力扣每日一题 6/22 字符串/贪心
力扣每日一题 6/22 字符串/贪心
4 0
|
7天前
力扣每日一题 6/18 字符串/模拟
力扣每日一题 6/18 字符串/模拟
6 0
|
7天前
|
算法
力扣每日一题 6/16 字符串 + 随机一题 动态规划/数学
力扣每日一题 6/16 字符串 + 随机一题 动态规划/数学
6 0