[LeetCode]Length of Last Word
2013-05-29 19:43
435 查看
class Solution { //be careful with the edge case " ", "a ", "a" public: int lengthOfLastWord(const char *s) { // Start typing your C/C++ solution below // DO NOT write int main() function bool hasWordBegin = false; int wordBegin = 0; int wordEnd = 0; const char* p = s; int step = 0; while (true) { char nowChracter = *(p+step); if (nowChracter == '\0') { if (true == hasWordBegin) wordEnd = step; break; } if(nowChracter == ' ') { if (true == hasWordBegin) wordEnd = step; hasWordBegin = false; } else { if(false == hasWordBegin) wordBegin = step; hasWordBegin = true; } step++; } int len = wordEnd-wordBegin; return len < 0 ? 0 : len; } };
second time
class Solution { public: int lengthOfLastWord(const char *s) { // Start typing your C/C++ solution below // DO NOT write int main() function if(s == NULL) return 0; int ans = 0; while(*s != '\0') { if(*s != ' ') { int curLen = 0; while(*s != '\0' && *s != ' ') s++, curLen++; ans = curLen; } else s++; } return ans; } };
相关文章推荐
- [LeetCode] Length of Last Word
- [LeetCode] Length of Last Word
- [LeetCode] Length of Last Word
- LeetCode——Length of Last Word
- leetcode: Length of Last Word
- Length of last word leetcode java
- LeetCode--Length of Last Word (Python)
- [LeetCode] Length of Last Word
- LeetCode——Length of Last Word
- Leetcode - Length of Last Word
- leetcode——58——Length of Last Word
- leetcode[58]:Length of Last Word
- Leetcode 58 Length of Last Word
- LeetCode Length of Last Word
- LeetCode(48)-Length of Last Word
- leetcode:Length of Last Word
- 【LeetCode】Length of Last Word
- 【leetcode】Length of Last Word
- LeetCode | Length of Last Word
- [leetcode]Length of Last Word