LeetCode第58题:

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

翻译:

获取最后一个单词的长度

思路:

思路很简单,要注意一点就是一些特殊情况,比如全是空格、或者只有一个单词

代码:

class Solution {
public int lengthOfLastWord(String s) {
s = s.trim();
if(s.length() == 0 ){
return 0;
}
if(s.length() == 1){
return 1;
}
return s.length() - 1 -s.lastIndexOf(" ");
}
}

LeetCode第66题:

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

翻译:

说了一大推,其实就是数组最后一个数字加1,但是数组的每个数字必须是个位数

思路:

其实就是整数的加法逻辑,现在改成数组,把其中的逻辑写出来而已。必须注意的是9加1等于10,要进一位

代码:

class Solution {
public int[] plusOne(int[] digits) {
int length = digits.length;
digits[length - 1] += 1;
for(int i = length -1 ;i>=0;i--){
if(digits[i] == 10){
digits[i] = 0;
if(i!=0){
digits[i - 1] +=1;
}else{
            //新建一个数组
int[] result = new int[length+1];
result[0] = 1;
for(int j = 1;j<result.length;j++){
result[j] = digits[j-1];
}
return result;
}
}
}
return digits;
}
}

欢迎关注我的微信公众号:安卓圈

【LeetCode算法-58/66】Length of Last Word/Plus One的更多相关文章

  1. Leetcode(58)题解:Length of Last Word

    https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...

  2. leetcode || 58、Length of Last Word

    problem: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ret ...

  3. LeetCode(59)Length of Last Word

    题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return th ...

  4. 【算法】LeetCode算法题-Length Of Last Word

    这是悦乐书的第155次更新,第157篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第14题(顺位题号是58).给定一个字符串,包含戴尔字母.小写字母和空格,返回最后一个单 ...

  5. [LeetCode] 58. Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  6. 【一天一道LeetCode】#58. Length of Last Word

    一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...

  7. 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 ...

  8. 【LeetCode】58. Length of Last Word 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...

  9. LeetCode 58. Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

随机推荐

  1. 关于TCP/IP

    一.网络模型 计算机网络的两种模型:OSI 模型和 TCP/IP 模型 由于 OSI 模型过于复杂难以实现,导致 TCP/IP 模型更早地应用在现实中,这也使得 TCP/IP 模型成为标准 在 OSI ...

  2. python测试开发django-68.templates模板标签{% for %}

    前言 有些标签类似这样: {% tag %} ,需要开始和结束标签 例如:{% tag %} ...标签 内容 ... {% endtag %},一般用于循环列表对象输出内容. for 标签 {% f ...

  3. 那周余嘉熊掌将得队对男上加男,强人所男、修!咻咻! 团队的Beta产品测试报告

    作业格式 课程名称:软件工程1916|W(福州大学) 作业要求:Beta阶段团队项目互评 团队名称: 那周余嘉熊掌将得队 作业目标:项目互测互评 队员学号 队员姓名 博客地址 备注 221600131 ...

  4. 项目Beta冲刺(4/7)(追光的人)(2019.5.26)

    所属课程 软件工程1916 作业要求 Beta冲刺博客汇总 团队名称 追光的人 作业目标 描述Beta冲刺每日的scrum和PM报告两部分 队员学号 队员博客 221600219 小墨 https:/ ...

  5. 使用cookie登录网盘账号

    ①使用Chrome浏览器登录百度网盘网页版 https://pan.baidu.com/ ②查看当前使用的cookie ③获取BDUSS 注意是全选复制,不要直接复制,会不全的. ④获取STOKEN ...

  6. 大文件断点续传webupload插件

    javaweb上传文件 上传文件的jsp中的部分 上传文件同样可以使用form表单向后端发请求,也可以使用 ajax向后端发请求 1. 通过form表单向后端发送请求 <form id=&quo ...

  7. Numpy | 09 高级索引

    NumPy 比一般的 Python 序列提供更多的索引方式.除了之前看到的用整数和切片的索引外,数组可以由整数数组索引.布尔索引及花式索引. 整数数组索引 实例1:获取数组中(0,0),(1,1)和( ...

  8. systemd socket activation golang demo

    service define rongapp.service [Unit] Description=rong Hello World HTTP Requires=network.target rong ...

  9. cogs 998. [東方S2] 帕秋莉·诺蕾姬

    二次联通门 : cogs 998. [東方S2] 帕秋莉·诺蕾姬 交上去后发现自己没上榜 就想着加点黑科技 把循环展开一下 结果WA了.. 万恶的姆Q /* cogs 998. [東方S2] 帕秋莉· ...

  10. mac 使用tesseract识别图片中的中文

    安装 tesseractbrew install tesseract 加入环境变量export TESSDATA_PREFIX=/usr/local/Cellar/tesseract/4.1.0/sh ...