当前位置:网站首页>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
边栏推荐
- LNMP architecture setup (deploy discuz Forum)
- 剑指 Offer 笔记: T45. 把数组排成最小的数
- 82.(cesium之家)cesium点在3d模型上运动
- Regular expression of shell programming (grep of shell script text three swordsmen)
- Several banks adjusted the redemption rules of cash management financial products: the confirmation time limit of redemption changed from "t+0" to "t+1"
- STM32 compilation error: l6235e: more than one section matches selector - cannot all be first/l
- 意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!
- 美现首例孕妇猴痘病例:新生儿被注射免疫球蛋白,已安全出生
- C programming language (2nd Edition) -- Reading Notes -- 1.3
- 【机器学习-白板推导系列】学习笔记---条件随机场
猜你喜欢
Synchronous use reference of the new version of data warehouse (for beginners)

求不同采样周期下的传递函数有限零点

VSCode复制代码时去掉样式/语法高亮/代码高亮/黑色背景

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

源码编译安装LAMP

Maker Hongmeng application development training 04

Analysis of the use of JUC framework from runnable to callable to futuretask

Interaction free shell programming

Codeforces round #664C

MATLAB画带延时系统的伯德图
随机推荐
C programming language (2nd Edition) -- Reading Notes -- 1.5.1
[machine learning whiteboard derivation series] learning notes - support vector machine and principal component analysis
数据包传输:应用层-内核-硬件
SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
[machine learning whiteboard derivation series] learning notes --- conditional random fields
第13章 IO流
The C programming language (2nd) -- Notes -- 1.10
The C programming language (2nd) -- Notes -- 1.7
Shell script text three swordsmen sed
LeetCode 03: T58. 最后一个单词的长度(简单); 剑指 Offer 05. 替换空格(简单); 剑指 Offer 58 - II. 左旋转字符串(简单)
Maker Hongmeng application development training 04
1. Introduction and basic use of flume
开源flink有写holo的Datastream connector或者flink sql conn
[special topic] summary of RMQ question brushing with ST table
微商的差商近似
第10章 枚举类与注解
C programming language (2nd Edition) -- Reading Notes -- 1.5.2
希腊字母读法
Several banks adjusted the redemption rules of cash management financial products: the confirmation time limit of redemption changed from "t+0" to "t+1"
LeetCode-SQL练习题总结(MySQL实现)