length-of-last-word
来源:互联网 发布:java gc机制 编辑:程序博客网 时间:2024/06/10 17:57
题目:
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”,
return5.
程序:
class Solution {public: int lengthOfLastWord(const char *s) { int count = 0; int len = strlen(s);//反向查找,末尾空格忽略,行中出现空格就终止循环 for(int i = len-1; i >= 0 ; i--){ if(s[i] == ' '){ if(count) break; } else{ count++; } } return count; }};
点评:
不能创建多余的空间,增加空间复杂度,会导致段错误
阅读全文
0 0
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- 干货 | 超级账本Fabric 1.0 多节点集群的部署(1)
- PKS:企业管理和运维Kubernetes的利器
- Harbor 项目进入 Rancher 的Community Catalog
- 用Kubernetes部署超级账本Fabric的区块链即服务(2)
- String类详解
- length-of-last-word
- 探索Harbor镜像仓库新的管理功能和界面
- hadoop单机开发环境搭建
- 六大设计原则详解(2)-里氏替换原则
- element-ui笔记
- 用Kubernetes部署超级账本Fabric的区块链即服务(1)
- 赠书 | 超级账本Fabric 1.0多节点集群的部署(2)
- java.toString()和String.valueOf的区别
- 天天学Linux命令21--find命令之exec