当前位置:网站首页>Leetcode 03: t58. Length of the last word (simple); Sword finger offer 05. replace spaces (simple); Sword finger offer 58 - ii Rotate string left (simple)
Leetcode 03: t58. Length of the last word (simple); Sword finger offer 05. replace spaces (simple); Sword finger offer 58 - ii Rotate string left (simple)
2022-07-27 11:48:00 【Ignorant little nine】
List of articles
T7: 58. Length of last word ( Simple )
Give you a string s, It consists of several words , Separate words with spaces . Returns the length of the last word in a string . If there is no last word , Please return 0 .
word It's just letters 、 The largest substring that does not contain any space characters .
Example 1:
Input :s = "Hello World"
Output :5
Example 2:
Input :s = " "
Output :0
Tips :
1 <= s.length <= 104
s Only English letters and spaces ’ ’ form
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/length-of-last-word
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas
Double pointer upside down , Note that there are spaces at the end , s = " " The situation of , s = "a" The situation of
solution : Double pointer
class Solution {
public int lengthOfLastWord(String s) {
int right = s.length()-1;
while(right>=0&&s.charAt(right)==' ') right--;
if(right<0) return 0;
int left = right;
while(left>=0&&s.charAt(left)!=' ') left--;
return right - left;
}
}
Execution time :0 ms, In all Java Defeated in submission **100.00%** Users of
Memory consumption :36.4 MB, In all Java Defeated in submission **95.74%** Users of
On, O1
T8: The finger of the sword Offer 05. Replace blank space ( Simple )
Please implement a function , Put the string s Replace each space in with "%20".
Example 1:
Input :s = "We are happy."
Output :"We%20are%20happy."
Limit :
0 <= s The length of <= 10000
Ideas
direct replace
solution : replace
class Solution {
public String replaceSpace(String s) {
return s.replace(" ", "%20");
}
}
Execution time :0 ms, In all Java Defeated in submission **100.00%** Users of
Memory consumption :36.3 MB, In all Java Defeated in submission **62.78%** Users of
On, O1
T9: The finger of the sword Offer 58 - II. Left rotation string ( Simple )
The left rotation operation of string is to transfer several characters in front of string to the end of string . Please define a function to implement the left rotation operation of string . such as , Input string "abcdefg" And number 2, This function will return the result of rotating two bits to the left "cdefgab".
Example 1:
Input : s = "abcdefg", k = 2
Output : "cdefgab"
Example 2:
Input : s = "lrloseumgh", k = 6
Output : "umghlrlose"
Limit :
1 <= k < s.length <= 10000
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas
Use substring()
solution : substring
class Solution {
public String reverseLeftWords(String s, int n) {
return s.substring(n,s.length())+s.substring(0,n);
}
}
Execution time :0 ms, In all Java Defeated in submission **100.00%** Users of
Memory consumption :38.1 MB, In all Java Defeated in submission **87.75%** Users of
On, On
边栏推荐
- LAN SDN technology hard core insider 13 from LAN to Internet
- Wilcoxon rank sum and signed rank
- 哈希表 详细讲解
- STM32编译出现error: L6235E: More than one section matches selector - cannot all be FIRST/L
- Vscode removes style / syntax highlighting / code highlighting / black background when copying code
- LAN SDN hard core technology insider 23 looking forward to the future - RDMA (Part 1)
- C programming language (2nd Edition) -- Reading Notes -- 1.4
- SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
- Moveit2 - 1. Start
- TLC549Proteus仿真&Sallen-Key滤波器&AD736Vrms到DC转换&Proteus查看51寄存器值
猜你喜欢

Moveit2 - 4. robot model and robot state

箱型图介绍

为什么选择智能电视?

LNMP architecture setup (deploy discuz Forum)

Could not load dynamic library ‘libcudnn.so.8‘;

Tlc549proteus simulation &sallen key filter &ad736vrms to DC conversion &proteus view 51 register value

Proteus8专业版破解后用数码管闪退的解决

LNMP架构搭建(部署Discuz论坛)

基于反馈率的控制系统原理

Regular expression of shell programming (grep of shell script text three swordsmen)
随机推荐
第7章 异常处理
EfficientNet
Adobe Audition提示 音频输入的采样率与输出设备不匹配——问题解决
Arduino常见供电问题与解决
第12章 泛型
Can you really write binary search - variant binary search
剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
新版数据仓库的同步使用参考(新手向)
compute_class_weight() takes 1 positional argument but 3 were given
The C programming language (2nd) -- Notes -- 1.6
为什么选择智能电视?
数据包传输:应用层-内核-硬件
Detailed explanation of hash table
LeetCode 04: T26. 删除排序数组中的重复项(简单); 剑指 Offer 67. 把字符串转换成整数(中等); 面试题 01.08. 零矩阵 (简单)
Proteus8专业版破解后用数码管闪退的解决
检定和校准的区别
Database cli tool docker image
C programming language (2nd Edition) -- Reading Notes -- 1.5.4
What is private traffic?
剑指 Offer 笔记: T57 - II. 和为 s 的连续正数序列