leetcode || 58、Length of Last Word
problem:
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
.
thinking:
(1)充分理解题意是解这道题的关键。
对于一些特殊情况: char *s='abc " (最后一个是空格), char *s = "a b "(连续空格)要注意到
(2)刚開始考虑使用 指针之差 来计算字符串的长度。发现掉入了一个无底深渊。各种特殊情况都要考虑在内,能解出来,可是比較绕
(3)採用 计数法 来解这道题最简单高效,遇到连续的非空格字符串開始计数,遇到空格保存计数结果,也有一些细节要注意。在代码中有凝视
(4)这道题考擦的是 分析问题的全面性 和 细节处理能力
code:
class Solution {
public:
int lengthOfLastWord(const char *s) {
int i=0;
int count=0;
int length=0;
while(*(s+i)!='\0')
{
if(*(s+i)!=' ')
count++;
else
{
if(count>0) //出现连续空格时,防止清空上次有效计数结果
length=count;
count=0;
}
i++;
}
if(count!=0) //善后处理:以非空格结束的字符串
return count;
else
return length; //以空格结束 }
};
leetcode || 58、Length of Last Word的更多相关文章
- Leetcode(58)题解:Length of Last Word
https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...
- LeetCode OJ: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
这是悦乐书的第155次更新,第157篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第14题(顺位题号是58).给定一个字符串,包含戴尔字母.小写字母和空格,返回最后一个单 ...
- 【LeetCode算法-58/66】Length of Last Word/Plus One
LeetCode第58题: Given a string s consists of upper/lower-case alphabets and empty space characters ' ' ...
- [LeetCode] 58. Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- LeetCode练题——58. Length of Last Word
1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...
- 【一天一道LeetCode】#58. Length of Last Word
一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...
- 【LeetCode】58. Length of Last Word 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
随机推荐
- POP数值动画
POP数值动画 效果 源码 https://github.com/YouXianMing/Animations // // PopNumberController.m // Animations // ...
- 【BZOJ】【3675】【APIO2014】序列分割
DP+斜率优化 首先我们根据这个分割的过程可以发现:总得分等于k+1段两两的乘积的和(乘法分配律),也就是说与分割顺序是无关的. 再对乘积进行重分组(还是乘法分配律)我们可以转化为:$ans=\sum ...
- Python在Windows下操作CH341DLL
#! /usr/bin/env python #coding=utf-8 import os import time from ctypes import * class USBI2C(): ch34 ...
- Informatica 常用组件Lookup缓存之四 使用不高速缓存的查找或静态高速缓存
默认情况下,在为高速缓存配置查找转换时,PowerCenter 将创建静态查找高速缓存.PowerCenter 将在处理第一个查找请求时创建高速缓存.它将根据查找条件为传递给转换的每行查询高速缓存.P ...
- go语言基础之数组做函数参数是值拷贝
1.数组做函数参数是值拷贝 示例: package main //必须有个main包 import "fmt" //数组做函数参数,它是值传递 //实参数组的每个元素给形参数组拷贝 ...
- iOS开发-委托实战
昨天晚上头疼,写了一部分草草的收笔了,早上起来补发一篇文章,昨天关于委托的基本使用和概念都稍微讲了一下,最开始学习委托的时候苹果官网和中文的博客文章看了不少,相似指数比较高.委托在命名要准确,最好是一 ...
- MVC 与 MVP 架构 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 有关于腾讯地图服务端IP定位接口的获取当前城市的处理
接口说明:http://apis.map.qq.com/ws/location/v1/ip 说明里面写了ip可以缺省,然并卵,经过测试的到结果并不能获取到当前城市,理由是腾讯ip库的对应ip精度没有定 ...
- 软件project(一)——宏观总结
曾经看视频,看过去就忘.不想再这样子下去了,所以总结了好久.想让自己忘不了.这个过程花费时间有点长,可是假设让自己忘不了.一切都值了. 以下先来一张导图来总结一下<软件project>这门 ...
- [Javascript] Funciton Expression
//This will load the code into the memory no matter //you call it or not function diffOfSquares(a,b) ...