LeetCode-Easy部分中标签为String 58. Length of Last Word
来源:互联网 发布:茶叶推广方案 知乎 编辑:程序博客网 时间:2024/06/04 18:16
原题
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.
For example,
Given s = "Hello World",return 5.
实现代码
方法一
public int LengthOfLastWord1(string s) { s = s.Trim(); //去掉前,后空格 if (s == "") return 0; string[] splitstring = s.Split(' '); string slast = splitstring[splitstring.Length - 1]; return slast.Length == 0 ? 1 : slast.Length; }
方法二
public int LengthOfLastWord2(string s) { s = s.Trim(); if (s == "") return 0; for (int i = s.Length-1;i>0; i--) { if (s[i] == ' ') return s.Length - i - 1; } return s.Length; }
2 0
- LeetCode-Easy部分中标签为String 58. Length of Last Word
- LeetCode-Easy部分中标签为String 434. Number of Segments in a String
- LeetCode-Easy部分中标签为String的所有题目
- LeetCode-Easy部分中标签为String 20. Valid Parentheses
- LeetCode-Easy部分中标签为String 520. Detect Capital
- LeetCode-Easy部分中标签为String 383. Ransom Note
- LeetCode-Easy部分中标签为Two Pointers 345. Reverse Vowels of a String
- [Leetcode 58, easy] Length of Last Word
- 【LeetCode】(58)Length of Last Word(Easy)
- <LeetCode><Easy> 58 Length of Last Word
- Leetcode 58. Length of Last Word (Easy) (cpp)
- 58.leetcode Length of Last Word(easy)[字符串 分割]
- leetcode 58. Length of Last Word(easy)
- LeetCode-Easy部分中标签为String 459. Repeated Substring Pattern
- LeetCode-Easy部分中标签为String 14. Longest Common Prefix
- LeetCode-Easy部分中标签为Math 231. Power of Two
- LeetCode-Easy部分中标签为LinkedList 160. Intersection of Two Linked Lists
- LeetCode-Easy部分中标签为 Binary Search 349. Intersection of Two Arrays
- sh 脚本相关使用记录
- ubuntu 下的PDF阅读器
- cs231n 课程作业 Assignment 2
- 微信视频直播服务
- RHEL7--UNI3--MAN使用方法
- LeetCode-Easy部分中标签为String 58. Length of Last Word
- 122. Best Time to Buy and Sell Stock II
- 海尔“小微”怎么玩
- OPENCV threshold type类型选择
- javascript中prototype、constructor以及__proto__之间的三角关系
- java,hibernate和sql server对应的数据类型表
- React之ref详细用法
- Pixhawk原生固件PX4之驱动ID
- OpenCV图像拷贝方法与注意点(=、copy、clone)