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开启刷题之 ...
随机推荐
- Version 1.7.0_80 of the JVM is not suitable for this product.Version: 1.8 or greater is required.
Eclipse启动失败,设置eclipse启动jdk有2种方法 第一种: 直接安装eclipse对应的jdk版本,并设置环境变量 第二种: 修改eclipse配置文件eclipse.ini 在plug ...
- 立即掌握SSM框架的要诀
ssm框架的总结: 1. 首先是POM.xml 文件的配置,他的作用主要是添加依懒的关系和自动下载相关的包. 2.对jdbc.properties进行配置 ,作用就是连接你的数据库的配置. 3.对接着 ...
- ios开发 第三天
1.复合 对象可以引用其它对象,可以利用其它对象提供的特性. 通过包含作为实例变量的对象指针实现的. 2.OC是单一继承 3.继承-重构 4.类实例化对象时,self指向了对象的首地址. 类对象isa ...
- 【编程技巧】EXTJS中Ext.grid.GridPanel配置项autoExpandColumn的使用方法
autoExpandColumn的作用是自动伸展,占满剩余区域.一般使用在列比较少,并且大多数列都比较窄,有一列比较宽的情况下,当然什么时候使用,还是得按照实际情况确定. 使用的时候主要有三点要注意的 ...
- 如何跳出页面的Frame框架
摘录自:http://blog.csdn.net/clare504/article/details/9347363 很多网页都是框架结构的,在很多的情况下会通过按钮点击事件或链接,跳出框架转到其它界面 ...
- 使用git bash提交代码到github托管
1.首先登录到https://github.com注册Github帐号,并且创建一个repository. 或者登录到 https://git.oschina.net/注册账号,并且创建一个rep ...
- bxslider使用教程
bxSlider下载+参数说明 "bxSlider"就是一款响应式的幻灯片js插件 bxSlider特性 充分响应各种设备,适应各种屏幕: 支持多种滑动模式,水平.垂直以及淡入淡出 ...
- python3 第十七章 - sequence(序列)
之前我们在讲for循环语句时就提到过序列,那么什么是序列(sequence)? 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 —— 它的索引(位置),第一个索引是0,第二个索引 ...
- linux_Nginx优化
1. 更改默认用户 a. 在nginx.conf中添加user指定用户 user web_nginx web_nginx; # nginx 用户大家都知道,通过更改默认用户实现,和http同级 b. ...
- python_如何定义装饰器类?
案例: 实现一个能将函数调用信息记录到日志的装饰器 需求: 把每次函数的调用时间,执行时间,调用次数写入日志 可以对被装饰函数分组,调用信息记录到不同日志 动态修改参数,比如日志格式 动态打开关闭日志 ...
