Length of Last Word -- LeetCode
来源:互联网 发布:正版办公软件价格 编辑:程序博客网 时间:2024/05/21 06:38
原题链接: http://oj.leetcode.com/problems/length-of-last-word/
这道题比较简单,就是进行一下字符串操作。唯一的细节就是要去掉尾部的空格,然后读到下一个空格,记录下长度。时间复杂度是O(n),n是字符串的长度,空间复杂度是O(1)。代码如下:
这道题比较简单,就是进行一下字符串操作。唯一的细节就是要去掉尾部的空格,然后读到下一个空格,记录下长度。时间复杂度是O(n),n是字符串的长度,空间复杂度是O(1)。代码如下:
public int lengthOfLastWord(String s) { if(s==null || s.length()==0) return 0; int idx = s.length()-1; while(idx>=0 && s.charAt(idx)==' ') idx--; int idx2 = idx; while(idx2>=0 && s.charAt(idx2)!=' ') idx2--; return idx-idx2;}这样子的题目比较简单,考查最简单的代码实现,一般也就是面试的第一道最基础的题目,这种题目要细心,实现不可以有bug哈。
59 0
- 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
- [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
- [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
- LeetCode | Length of Last Word
- LeetCode - Length of Last Word
- Leetcode: Length of Last Word
- NDK入门
- 磁盘设备栈的设备传递
- 【Two Sigma】分布式备份 Distributed Back-Up
- size()的用法
- OCM_Session7_0_which srvctl
- Length of Last Word -- LeetCode
- JavaScript Tips1
- OCM_Session7_0_CRS-0223
- code::Block单步调试中的一个问题
- 1010. Radix (25)
- IAR #pragma optimize 指令
- UTF8
- Codeforces Round #238 (Div. 2) 总结
- eclipse创建maven项目有个红叉