LeetCode: Length of Last Word

简单题目

 1 class Solution {
 2 public:
 3     int lengthOfLastWord(const char *s) {
 4         // IMPORTANT: Please reset any member data you declared, as
 5         // the same Solution instance will be reused for each test case.
 6         int ans = 0;
 7         int len = 0;
 8         while (*s != '' && *s == ' ') s++;
 9         if (*s == '') return 0;
10         while (*s != '') {
11             if (*s == ' ') {
12                 ans = len? len : ans;
13                 len = 0;
14             }
15             else if (isalpha(*s)) len++;
16             s++;
17         }
18         return len? len : ans;
19     }
20 };
原文地址:https://www.cnblogs.com/yingzhongwen/p/3422993.html