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. zabbix监控项字体乱码的处理方法

    一.新安装完的查看项目的时候字体是乱码的 二.在控制面板的字体中复制出系统的“微软雅黑”字体,粘贴到桌面 粘贴出来之后会产生两个文件,msyhbd.ttf为加粗的字体 三.修改并替换字符集 我是编译安 ...

  2. 第三篇 -- XML Schema

    验证XML文档是否符合议定的XML结构有两种方法,分别是DTD模式与XML Schema.本文主要介绍XML Schema. 一.XML Schema的优点 XML Schema基于XML,没有专门的 ...

  3. python应用-给出行数,输出相应的杨辉三角

    def main(): num = int(input('Number of rows: ')) yh = [[]] * num for row in range(num): yh[row] = [N ...

  4. Spring Cloud Task 知识点

    Spring Cloud Task的目标是为Spring Boot应用程序提供创建短期运行微服务的功能. 出处:https://blog.csdn.net/peterwanghao/article/d ...

  5. nuxt 中使用 koa-session

    官方 github 网址:https://github.com/koajs/session 自己做的demo:https://github.com/cag2050/nuxt_session_demo ...

  6. Spring全家桶相关文章汇总(Spring,SpringBoot,SpringData,SpringCloud)

      因为Spring框架包含的组件比较多,写的博客内容也比较多,虽然有分专栏但是依然不方便查找,所以专门用一篇文章来记录相关文章,会不定期更新. 一.Spring 1.基础内容 Spring介绍 Sp ...

  7. UI系统的分类

    1.DSL系统:UI领域特定语言 html markdown; 与平台无关,只与通用UI领域有关: 2.平台语言系统(通用语言系统) UI概念在平台和通用语言中的表示. 一.信息表达: 基本信息:文本 ...

  8. LeetCode 722. Remove Comments

    原题链接在这里:https://leetcode.com/problems/remove-comments/ 题目: Given a C++ program, remove comments from ...

  9. ent 基本使用四 图遍历查询

    接上文,我们已经创建了基本的关系以及表实体,以下是通过图方式的查询 参考关系图 代码处理 创建图数据 func CreateGraph(ctx context.Context, client *ent ...

  10. jdk和Tomcat版本之间的关系,jdk尽量要比tomcat低才行

    注:本文来源于:6860 <jdk和Tomcat版本之间的关系,jdk尽量要比tomcat低才行> 用的tomcat是低版本的,但是用的jdk却是高版本的,用Servlet做的项目运行都没 ...