给出一个只包含大小写字母和空格的字符串s,请返回字符串中最后一个单词的长度
如果字符串中没有最后一个单词,则返回0
注意:单词的定义是仅由非空格字符组成的字符序列。
例如:
s ="Hello World",
返回5。
int lengthOfLastWord(const char* s) { if(strlen(s) < 1) return 0; int count = 0, tail = strlen(s) - 1; //考虑末尾是空格的情况,遍历到第一个不是空格的字符为真正的末尾 while(tail >= 0 && s[tail] == ' ') tail--; while(tail >= 0 && s[tail] != ' '){ count++; tail--; } return count; }
解法一: class Solution { public int lengthOfLastWord(String s) { String[] array = s.trim().split("\\s+"); return array[array.length - 1].length(); } } 解法二: public int lengthOfLastWord(String s) { if (s == null | s.isEmpty()) return 0; int begin = 0, end = s.length(); while (end > 0 && s.charAt(end - 1) == ' ') { end--; } for (int i = 0; i < end; i++) { if (s.charAt(i) == ' ') { begin = i + 1; } } return end - begin; } 解法三: public int lengthOfLastWord(String s) { int count = 0; for (int i = s.length() - 1; i >= 0; i--) { if (s.charAt(i) == ' ') { if (count > 0) { break; } } else { count++; } } return count; }
class Solution { public: int lengthOfLastWord(const char *s) { int len = strlen(s); int count = 0; if(len == 0) return 0; for(int i = len - 1; i >= 0;i--){ if(s[i] == ' ' && count) // 出现空格,而且count不为零 break; if(s[i] == ' ') continue; count += 1; } return count; } };
public class Solution { public int lengthOfLastWord(String s) { if (s == null || s.equals("")) { return 0; } int len = s.length(); int end = len - 1; while (s.charAt(end) == ' ' && end > 0) { end--; } int start = end; while (start >= 0 && s.charAt(start) != ' ') { start--; } return end - start; } }
public class Solution { public int lengthOfLastWord(String s) { if (s==null||s.length()==0) return 0; int index=s.length()-1; while (s.charAt(index)==' '&&index>0){ index--; } int start=index; while (index>=0&&s.charAt(index)!=' '){ index--; } return start-index; } }
I've noticed that a lot of solutions use available library functions that return directly the positions of certain characters or do other operations like "split". I personally don't think that's a good idea. Firstly, these functions take some time and usually involve with iteration through the whole string. Secondly, questions like this one is intended to be a practice of detail implementation, not calling other functions. My solution like below uses only the most basic string operations and probably beats many other solutions which call other existing functions.
int lengthOfLastWord(const char* s) { int len = 0; while (*s) { if (*s++ != ' ') ++len; else if (*s && *s != ' ') len = 0; } return len; }
//利用C++的输入输出控制类 stringstream class Solution { public: int lengthOfLastWord(const char *s) { stringstream ss(s); string str; while(ss >> str); return str.length(); } };
class Solution { public: int lengthOfLastWord(const char *s) { if(s == NULL || s[0] == '\0') return 0; int len = 0; const char *pos1 = s; while(*pos1 == ' ') pos1++; const char *pos2 = pos1; while(*pos1 != '\0') { if(*pos1 == ' ') { len = pos1 - pos2; while(*pos1 == ' ') ++pos1; pos2 = pos1; if(*pos1 == '\0') break; } pos1++; } if(pos2 != pos1) len = pos1 - pos2; return len; } };
基本思路:遍历字符串,用start指针指向单词开头,end指针指向单词结尾,如果有单词,end-start+1
即为最后一个单词的长度。
代码如下:
// // Created by jt on 2020/9/29. // class Solution { public: int lengthOfLastWord(const char *s) { if (!s) return 0; // 找到最后一个空格 const char *p = s, *start = nullptr, *end = nullptr; while (*p) { // 如果当前位置非空格且当前位置为起始位置,或当前位置非空格且前面位置为空格,保存单词的头指针 if ((*p != ' ' && p == s ) || (*p != ' ' && *(p-1) == ' ')) start = p; // 如果当前位置非空格,后面位置为结尾或空格,保存单词的尾指针 if (*p != ' ' && (*(p+1) == '\0' || *(p+1) == ' ')) end = p; ++p; } if (start == nullptr || end == nullptr) return 0; else return end - start + 1; } };
public class Solution { public int lengthOfLastWord(String s) { int res = 0; int i = s.length()-1; while(i>=0 && s.charAt(i)==' ') i--; while(i>=0 && s.charAt(i)!=' '){ i--; res++; } return res; } }