Easy-题目45:58. Length of Last Word
来源:互联网 发布:mac如何卸载opera 编辑:程序博客网 时间:2024/05/22 17:33
题目原文:
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.
题目大意:
给出一个字符串s,求空格分隔的最后一个单词的长度
题目分析:
使用Java的split这个api,按空格分割成字符串数组,返回最后一个字符串的长度。
源码:(language:java)
public class Solution { public int lengthOfLastWord(String s) { String[] words= s.split(" "); if(words.length==0) return 0; return words[words.length-1].length(); }}
成绩:
3ms,beats 12.92%,众数0ms,47.42%
cmershen的碎碎念:
这道题还可以用trim修剪字符串两端的空格,再从后向前搜索,这样的时间应该会短一些,因为split的底层是依据正则表达式实现的,开销很大。
0 0
- Easy-题目45:58. Length of Last Word
- 58. Length of Last Word [easy] (Python)
- Leet Code OJ 58. Length of Last Word [Difficulty: Easy]
- Leetcode 58. Length of Last Word (Easy) (cpp)
- 58.leetcode Length of Last Word(easy)[字符串 分割]
- leetcode 58. Length of Last Word(easy)
- [Leetcode 58, easy] Length of Last Word
- 【LeetCode】(58)Length of Last Word(Easy)
- <LeetCode><Easy> 58 Length of Last Word
- Length of Last Word(easy)
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 什么是一阶逻辑(First-order Logic)
- tushare金融数据
- 视图
- memched1.0源码阅读(3)——运行流程
- GCC编译和GDB调试
- Easy-题目45:58. Length of Last Word
- [Maven]5,相关配置
- Java性能提高小常识
- 怎么快速看懂别人写的module和ipcore
- Android基础知识整合篇——Activity知识点
- Easy-题目46:205. Isomorphic Strings
- 鼠标经过旋转特效
- 神经网络浅析
- 【HDU】 1431 素数回文