Leetcode: Length of Last Word

网友投稿 595 2022-10-11

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.

解答:

(这个问题不难,但是比较容易错,特别是有的情况考虑不周全)

int lengthOfLastWord(const char *s){ int len = strlen(s); int sum = 0; while(s[len - 1] == ' ') len--; for(int i = len - 1; i >= 0; i--) { if(s[i] != ' ') sum++; else break; } return sum;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:垃圾回收分类查询平台微信小程序端(查询垃圾分类的小程序)
下一篇:SpringBoot + thymeleaf 实现读取视频列表并播放视频功能
相关文章

 发表评论

暂时没有评论,来抢沙发吧~