Movatterモバイル変換


[0]ホーム

URL:


企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案广告
# Length of Last Word### Source- leetcode: [Length of Last Word | LeetCode OJ](https://leetcode.com/problems/length-of-last-word/)- lintcode: [(422) Length of Last Word](http://www.lintcode.com/en/problem/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.Have you met this question in a real interview? YesExampleGiven s = "Hello World", return 5.NoteA word is defined as a character sequence consists of non-space characters only.~~~### 题解关键点在于确定最后一个字符串之前的空格,此外还需要考虑末尾空格这一特殊情况,故首先除掉右边的空白字符比较好。### Java~~~public class Solution { /** * @param s A string * @return the length of last word */ public int lengthOfLastWord(String s) { if (s == null | s.isEmpty()) return 0; // trim right space int begin = 0, end = s.length(); while (end > 0 && s.charAt(end - 1) == ' ') { end--; } // find the last space for (int i = 0; i < end; i++) { if (s.charAt(i) == ' ') { begin = i + 1; } } return end - begin; }}~~~### 源码分析两根指针。### 复杂度分析遍历一次,时间复杂度 O(n)O(n)O(n).

[8]ページ先頭

©2009-2025 Movatter.jp