Leetcode刷题C#版之 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.
Note: A word is defined as a character sequence consists of non-space characters only.
Example:
Input: "Hello World"
Output: 5
C#版本的代码如下(关键步骤已经注释):
public static int LengthOfLastWord(string s)
{
//将字符串分隔开
string[] midtest = s.Split(' ');
int result=;
//从最末尾一个开始向前搜索第一个非空值
for (int i = midtest.Length - ; i >= ; i--)
{
string test = midtest[i];
//找到第一个不为空的情况
if (!string.IsNullOrEmpty(test))
{
result = test.Length;
break;
}
}
return result;
}
再次运行时间为最少的,小嘚瑟,哈哈
Leetcode刷题C#版之 Length of Last Word的更多相关文章
- 【LeetCode刷题Java版】Reverse Words in a String
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- Leetcode刷题C#版之 Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- Leetcode刷题C#版之Toeplitz Matrix
题目: Toeplitz Matrix A matrix is Toeplitz if every diagonal from top-left to bottom-right has the sam ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- LeetCode刷题总结-数组篇(上)
数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...
- LeetCode刷题总结-数组篇(下)
本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...
- LeetCode刷题总结-树篇(上)
引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...
随机推荐
- XHR
xhr注入 XHR 注入技术是通过XMLHttpRequest来获取javascript的.但与eval不同的是,该机制是通过创建一个script的DOM元素,然后把XMLHttpRequest的响应 ...
- 阿里云Maven配置,Maven仓库配置,Maven镜像配置
阿里云Maven配置,Maven仓库配置,Maven镜像配置 ======================== 蕃薯耀 2018年1月29日 http://www.cnblogs.com/fanshu ...
- python arvg用法
转自:http://blog.csdn.net/vivilorne/article/details/3863545 在学python的过程中,一直弄不明白sys.argv[]的意思,虽知道是表示命令行 ...
- 找不到javax.servlet.Filter类,
找不到javax.servlet.Filter类, 在构建的依赖包中缺少servlet.api
- jquery判断数据类型和相同字符串不相等
typeof object返回object对象数据类型 encodeURIComponent(str)//可把字符串作为URI 组件进行编码. 若str1和str2字符串数值相同,encodeURIC ...
- Linux上常用软件安装和总结
Linux总结: 以前只顾着撸码,Linux这些一般都是运维玩的,然后也没怎么折腾过,每次上线也都只是发布下,最多也就是启停服务器.最近闲来无事就玩了玩Linux,还挺好的. 这里做一个总结来结束Li ...
- MySQL中查询表及索引大小的方法
查询MySQL表的大小及索引大小可以通过系统库information_schema中的TABLES表来实现. 该表常用的一些字段: TABLE_SCHEMA:数据库名TABLE_NAME:表名ENGI ...
- struts2.xml的配置问题
1.<package namespace="/"></package> namespace决定访问action的路径: 如果省略,将代表任意路径: 2.&l ...
- js中的深拷贝与浅拷贝
对象的深拷贝于浅拷贝 对于基本类型,浅拷贝过程就是对值的复制,这个过程会开辟出一个新的内存空间,将值复制到新的内存空间.而对于引用类型来书,浅拷贝过程就是对指针的复制,这个过程并没有开辟新的堆内存空间 ...
- Func常用模块及API
Func常用模块及API Func提供了非常丰富的功能模块,包括: CommandModule(执行命令) CopyFileModule(拷贝文件) CpuModule(CPU信息) DiskModu ...
