题目

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 class Solution {public int lengthOfLastWord(String s) {if(s.length()==0) return 0;char[] chars=s.toCharArray();char empty=' ';int lengthOfLastWord=0;int lengthOfLastWord_temp=0;for(int i=0;i<s.length();i++){if(chars[i]!=empty){lengthOfLastWord++;lengthOfLastWord_temp=lengthOfLastWord;}else {lengthOfLastWord=0;}}if(chars[s.length()-1]==empty) return lengthOfLastWord_temp;else  return lengthOfLastWord;}
}
代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

【LeetCode从零单排】No58.Length of Last Word相关推荐

  1. leetcode python3 简单题58. Length of Last Word

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十八题 (1)题目 英文: Given a string s consists ...

  2. 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)

    1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...

  3. 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array

    题目      题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...

  4. 【LeetCode从零单排】No.7 Reverse Integer

    前话       今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目        没想到做的第一道题目,虽然看似 ...

  5. 【LeetCode从零单排】No.135Candy(双向动态规划)

    1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. 【LeetCode从零单排】No221.Maximal Square

    题目 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  7. 【LeetCode从零单排】No121 Best Time to Buy and Sell Stock

    题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...

  8. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

    题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...

  9. 【LeetCode从零单排】No189 .Rotate Array

    题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

最新文章

  1. 服务器系统加速,系统加速技巧:让Linux启动更加快速
  2. php删除垃圾文件,Python删除windows垃圾文件的方法
  3. windows 7搭建流媒体服务
  4. bzoj2561 最小生成树
  5. 一行python代码值多少钱_一行python代码
  6. codeigniter:去掉 URL 中的 index.php
  7. python3怎么用_当同时安装Python2和Python3后,如何兼容并切换使用详解(比如pip使用)...
  8. QT实现头像图片剪切框
  9. 进入Docker容器的4种方式
  10. Highcharts 将默认文字更改为中文
  11. 触屏java版象棋下载 游戏下载安装_中国象棋官方版下载
  12. windows下icns转png或ico
  13. matlab x对数坐标,matlab 对数坐标
  14. 块级元素、行内元素、行内块级元素的特点
  15. 计算机组成二进制除法,计算机组成原理:3.4.1 定点原码 除法器
  16. 代码函数从零开始学习OpenCL开发(二)一个最简单的示例与简单性能分析
  17. Burp Macros功能实现token验证爆破
  18. mysqldump命令备份数据 mysql备份数据
  19. CentOS7设置笔记本合盖不断网
  20. EasyAR_SDK在unity制作ar视频黑屏,只有声音问题

热门文章

  1. GRE核心词汇助记与精练-List12转
  2. tcp/ip 协议栈Linux内核源码分析14 udp套接字接收流程一
  3. 新浪短链api java_php调用新浪短链接API的方法
  4. mysql外键约束语句级连_mysql之外键约束(级联操作等) 父表子表
  5. 计算机设计大赛海洋世界,全国大学生海洋文化设计大赛第二届获奖作品(三)...
  6. php 微信 返回,php版微信返回用户text输入的方法
  7. word回车后间距太大_Word这些神奇的功能,你知道多少?学会这4招,工作不再发愁...
  8. stm32入门c语言下载,STM32入门C语言详解.docx
  9. java手动输入函数_Java程序如何添加一个函数,如果玩家输入错误,它将返回代码。...
  10. 在浙学计算机基础2020答案,浙江大学2020年硕士研究生复试分数线的基本要求