当前位置:网站首页>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
边栏推荐
- Moveit2 -- 2. Quick start of moveit in rviz
- The C programming language-2nd-- notes -- 4.11.3
- The difference between microcomputer and single chip microcomputer
- [unity entry program] creator kitfps: first person shooting 3D game
- Firewall firewall
- Maker paper data search training notes
- Temporary use of solo, difficult choice of Blog
- Shell script text three swordsmen sed
- IDEA: Can‘t use Subversion command line client:svn 解决方案
- makefile模板
猜你喜欢

Solution of digital tube flash back after proteus8 professional version cracking

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

源码编译安装LAMP

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network

Principle of control system based on feedback rate

Shell脚本文本三剑客之awk

Everything cannot be searched for startup_ Lpc11x.s file

SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series

为什么选择智能电视?

【机器学习-白板推导系列】学习笔记---概率图模型和指数族分布
随机推荐
C programming language (2nd Edition) -- Reading Notes -- 1.4
LAN SDN technology hard core insider 12 cloud CP's daily love - hardware vxlan forwarding plane
Why choose smart TV?
你真的会写二分查找吗——变种二分查找
C programming language (2nd Edition) -- Reading Notes -- 1.5
C custom set
暂用 Solo,博客选择困难
LAN SDN hard core technology insider 23 looking forward to the future - RDMA (Part 1)
Could not load dynamic library ‘libcudnn.so.8‘;
The C programming language (2nd) -- Notes -- 1.7
【机器学习-白板推导系列】学习笔记---条件随机场
STM32 compilation error: l6235e: more than one section matches selector - cannot all be first/l
Maker harmony OS application development training notes 01
LNMP architecture setup (deploy discuz Forum)
The C programming language (2nd) -- Notes -- 1.6
N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei
LAN SDN technology hard core insider 13 from LAN to Internet
C programming language (2nd Edition) -- Reading Notes -- 1.5.3
torch‘ has no attribute ‘inference_mode‘
C programming language (2nd Edition) -- Reading Notes -- 1.5.1