[LeetCode] 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.
For example,
Given s = "Hello World"
,
return 5
.
这题比较简单,只是可能前面有空格,后面有空格。- -
- 排除最后的空格
- index 从后往前查第一个空格。
- 返回长度。
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Solution {
- public:
- int lengthOfLastWord(const char *s) {
- int n = strlen(s);
- if(n <) return ;
- int idx=n-;
- while(idx>=&&s[idx]==' ') idx--;
- n = idx+;
- while(idx>=){
- if (s[idx]==' ') break;
- idx --;
- }
- // if(idx<0) return 0;
- return n - idx -;
- }
- };
- int main()
- {
- char s[] = " 12 ";
- Solution sol;
- cout<<sol.lengthOfLastWord(s)<<endl;
- return ;
- }
[LeetCode] Length of Last Word 字符串查找的更多相关文章
- 【LeetCode每天一题】Length of Last Word(字符串中最后一个单词的长度)
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [LeetCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- LeetCode——Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode]Length of Last Word @ Python
原题地址:https://oj.leetcode.com/problems/length-of-last-word/ 题意: Given a string s consists of upper/lo ...
- [Leetcode] Length of last word 最后一个单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...
- [LeetCode] Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Leetcode: Length of Last Word in python
Length of Last Word Total Accepted: 47690 Total Submissions: 168587 Given a string s consists of ...
- Leetcode 58 Length of Last Word 字符串
找出最后一个词的长度 class Solution { public: int lengthOfLastWord(string s) { , l = , b = ; while((b = s.find ...
- LeetCode Length of Last Word 最后一个字的长度
class Solution { public: int lengthOfLastWord(const char *s) { ; string snew=s; ,len=strlen(s); ]; ) ...
随机推荐
- 了解并使用springAOP(面向切面编程)
Aop是干嘛的为什么要使用它 在业务系统中,总有一些散落,渗透到系统的各处且不得不处理的事情,这些穿插在既定业务中的操作就是所谓的“横切逻辑”,也称切面, 我们怎样才不受这些附加要求的干扰,专心于真正 ...
- GC回收的对象
垃圾收集(Carbage Collection) java内存在运行时区域,程序计数器.java虚拟机栈.本地方法三个区域都是线程私有的内存区域,随着线程的启动和销毁而分配和回收.栈帧随着方法的调 ...
- C语言中strtod()函数的用法详解
函数原型: #include <stdlib.h> double strtod(const char *nptr, char **endptr); C语言及C++中的重要函数. 名称含义 ...
- bash shell命令与监测的那点事(三)
bash shell命令与监测的那点事之df与du 前两篇介绍了bash shell的进程监控指令,但是有时候你需要知道在某个设备上还有多少磁盘空间.首先介绍df命令: df命令 df命令就是用来轻松 ...
- IOS开发---菜鸟学习之路--(二十三)-直接利用键值对的方式来处理数据的感想
首先声明,本文纯粹只是做为本人个人新手的理解.文中的想法我知道肯定有很多地方是错的. 但是这就是我作为一个新人的使用方法,对于大牛非常欢迎指导,对于喷子请绕道而行. 由于这是早上跟我学长讨论数据处理时 ...
- Effictive C++ 学习记录
这是前段时间看的书,整理到这里吧,以后查看也方便. 这些条款需要反复查看. 条款01:视C++为一个语言联邦 条款02:尽量用const.enum.inline替换#define 条款03:尽可能的使 ...
- Leetcode 560.和为k的子数组
和为k的子数组 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1 ...
- 关于C++ STL标准库中map 的多元素应用
map的特性是,所有的元素会根据键值自动排序.map的所有元素都是pair,同时拥有实值(value)和键值(key).pair的第一个元素被视为键值,第二个被视为实质piar 的定义 templat ...
- [oldboy-django][3作业汇总]登录,注册最终版
# 作业(登录,注册)最终版 - 保留上次输入的值 - 用户数据格式的验证
- 只显示前几条数据的sql语句写法 七种数据库中Select Top的使用方法
七种数据库中Select Top的使用方法 1. Oracle数据库 SELECT * FROM TABLENAME WHERE ROWNUM <= N 2. Infomix数据库 SELECT ...