leetcode 之 Length of Last Word
2015-06-05 17:31
405 查看
LeetCode : Lendth of Last Word
这道题非常简单,需要考虑的细节也很少,在这个基础上,我们需要考虑的是怎么将代码的运行效率写得比较高。
形如: “hello ” 返回的是5
代码如下(leetCode 测得运行时间为0ms):
这道题非常简单,需要考虑的细节也很少,在这个基础上,我们需要考虑的是怎么将代码的运行效率写得比较高。
形如: “hello ” 返回的是5
代码如下(leetCode 测得运行时间为0ms):
int lengthOfLastWord(char *s) { int wordLen = 0; int tmp = 0; //!< 中间变量 char *str = s; if (s == NULL) //!< 每次解引用之前,都应该判断是否为空 { printf("The string is NULL!\n"); return 0; } while (*str != '\0') { tmp = (*str == ' ') ? 0 : ++tmp; //!< 不为空格则加1 wordLen = (tmp == 0) ? wordLen : tmp; //!< worldLen 保存最新的非0值 ++str; } return wordLen; }
相关文章推荐
- memcached 分布式集群算法
- 用Windows PowerShell 控制管理 Microsoft Office 365
- 如何做一名优秀、甚至卓越的WEB前端工程师
- 【android】夜间模式简单实现
- 长度小于8000(varchar类型),以"逗号"分隔的字符串
- 一次性更改数据库內拥有者不屬dbo的物件改成为dbo
- C# List<T>用法
- 完整的FreeBSD软件安装方法
- Linux守护进程的编程实现
- UIWebView中Html中用JS调用OC方法及OC执行JS代码
- ORA-00257: archiver error. Connect internal only, until freed【日志归档清理】
- 【虚拟化】 vSphere 6.0
- java之合并链表的两种方法
- C语言字符数组的初始化
- unity手游开发--如何得到当前物体播放的动画
- php模版引擎(smarty3)—display()渲染文本字符串
- 最小堆的删除
- 队列出队三个
- 如何设置浏览器禁止使用UC浏览器
- mysql使用基础 sql语句与数据完整性(二)