【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 ' ', 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的更多相关文章
- Leetcode(58)题解:Length of Last Word
https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...
- leetcode || 58、Length of Last Word
problem: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ret ...
- LeetCode(59)Length of Last Word
题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return th ...
- 【算法】LeetCode算法题-Length Of Last Word
这是悦乐书的第155次更新,第157篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第14题(顺位题号是58).给定一个字符串,包含戴尔字母.小写字母和空格,返回最后一个单 ...
- [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
一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...
- 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 解题报告(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 ...
随机推荐
- KVM虚拟机被OOM killer
一.线上环境的虚拟机被KVM物理机kill掉 Linux 内核根据应用程序的要求分配内存,通常来说应用程序分配了内存但是并没有实际全部使用,为了提高性能,这部分没用的内存可以留作它用,这部分内存是属于 ...
- sourceinsight sublimetext主题色配置
1.sourceinsight发布了4.0版本,全面支持了utf-8编码,这里sourinsight 颜色基本完全按照sublime默认的monokia主题来进行调色,效果如下图所示. 2.配色文件和 ...
- deployment:声明式的升级应用
9.1.使用RC实现滚动升级 #kubectl rolling-update kubia-v1 kubia-v2 --image=luksa/kubia:v2 使用kubia-v2版本应用来替换运行着 ...
- Palisection(Codeforces Beta Round #17E+回文树)
题目链接 传送门 题意 给你一个串串,问你有多少对回文串相交. 思路 由于正着做不太好算答案,那么我们考虑用总的回文对数减去不相交的回文对数. 而不相交的回文对数可以通过计算以\(i\)为右端点的回文 ...
- 2019年杭电多校第一场 1002题Operation(HDU6579+线性基)
题目链接 传送门 题意 初始时有\(n\)个数,现在有\(q\)次操作: 查询\([l,r]\)内选择一些数使得异或和最大: 在末尾加入一个数. 题目强制在线. 思路 对于\(i\)我们记录\([1, ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- A and B and Lecture Rooms(LCA)
题目描述 A and B are preparing themselves for programming contests. The University where A and B study i ...
- Echo团队Beta冲刺随笔集合
班级:软件工程1916|W 作业:项目Beta冲刺(团队) 团队名称:Echo 作业目标:完成项目Beta冲刺 凡事预则立 Day 0: 凡事预则立 冲刺随笔 Day 1: Beta冲刺第一天 Day ...
- ssm批量删除
ssm批量删除 批量删除:顾名思义就是一次性删除多个.删除是根据前台传给后台的id,那么所谓批量删除,就是将多个id传给后台,那么如何传过去呢,前后台的交互该如何实现? 1.jsp页面,先选中所有的要 ...
- python 查询文件存放地址
import os, time import sys import re def search(path,name): for root, dirs, files in os.walk(path): ...