当前位置:网站首页>String - Sword finger offer 58 - ii Rotate string left
String - Sword finger offer 58 - ii Rotate string left
2022-07-24 13:54:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
The finger of the sword Offer 58 - II. Left rotation string
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".
2 Title Example
Example 1:
Input : s = “abcdefg”, k = 2
Output : “cdefgab”
Example 2:
Input : s = “lrloseumgh”, k = 6
Output : “umghlrlose”
3 Topic tips
1 <= k < s.length <= 10000
4 Ideas
There are many ways to solve this problem , This paper mainly introduces “ String slice ” , “ List traversal ” , “ String traversal splicing ” Three methods .
Because the multiple solutions of this problem involve The string is an immutable object Related concepts of , This leads to a great difference in efficiency . therefore , Make a separate section Efficiency analysis of three methods , Hope to help you .
Method 1 : String slice
Apply string slicing function , It is convenient to rotate the string left .
Get string s[n :] Slicing and s[:n] section , Use "+" Operator splice and return .
Complexity analysis :
Time complexity o(N): among N For the string s The length of , The string slicing function has linear time complexity ;
Spatial complexity o(N): The total length of the two string slices is N.
Method 2 : List traversal
If the interview rules do not allow the use of slicing functions , Use this method .
Algorithm flow :
1. Create a new one list(Python)、StringBuilder(Java), Write it down as res ;
2. First direction res add to “ The first n+1 Characters from bit to last ”;
3. Again to res add to “ First to n A character ”;4. take res Convert to a string and return .
Complexity analysis :
Time complexity o(N): Linear traversal s And add , Use linear time ;.
Spatial complexity O(N)︰ New auxiliary res Use O(N) The size of the extra space
Method 3 : String traversal splicing
If specified Python Out of commission join( function , Or regulation Java Only use String, Use this method .
This method and method two train of thought — Cause , The difference is to use strings instead of lists .
Complexity analysis :
Time complexity o(N): Linear traversal s And add , Use linear time ;
Spatial complexity O(N): Suppose that the memory will be reclaimed in time during the cycle , There is at least a length of N and N―1 Two strings of ( New length is N Of res Previous length is required Ⅳ-1 Of res ) , So at least use O(N) Extra space .
5 My answer
Method 1 : String slice
class Solution {
public String reverseLeftWords(String s, int n) {
return s.substring(n, s.length()) + s.substring(0, n);
}
}
Method 2 : List traversal
class Solution {
public String reverseLeftWords(String s, int n) {
StringBuilder res = new StringBuilder();
for(int i = n; i < s.length(); i++)
res.append(s.charAt(i));
for(int i = 0; i < n; i++)
res.append(s.charAt(i));
return res.toString();
}
}
Method 3 : String traversal splicing
class Solution {
public String reverseLeftWords(String s, int n) {
String res = "";
for(int i = n; i < n + s.length(); i++)
res += s.charAt(i % s.length());
return res;
}
}
边栏推荐
- Nmap安全测试工具使用教程
- Detailed explanation of switch link aggregation [Huawei ENSP]
- 微信小程序 TODO案例
- 22-07-23周总结
- Sringboot plugin framework implements pluggable plug-in services
- [机缘参悟-51]:既然人注定要死亡,为什么还要活着?
- Flink容错机制(五)
- R语言使用epiDisplay包的summ函数计算dataframe中指定变量在不同分组变量下的描述性统计汇总信息并可视化有序点图、自定义cex.main参数配置标题文本字体的大小
- SQL Server 启停作业脚本
- On the number of solutions of indefinite equations
猜你喜欢

【C语言笔记分享】——动态内存管理malloc、free、calloc、realloc、柔性数组

Hcip day 13

软链接、硬链接

Apache2 ha experiment with raspberry pie

网络安全——WAR后门部署

天然气潮流计算matlab程序

Game thinking 04 summary: a summary of frame, state and physical synchronization (it was too long before, and now it's brief)

CSP2021 T3 回文

Network security - file upload content check bypass

CSDN垃圾的没有底线!
随机推荐
Network security - file upload content check bypass
Unity pedestrians walk randomly without collision
Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]
R语言ggpubr包的ggarrange函数将多幅图像组合起来、annotate_figure为组合图像添加注释、注解、标注信息、使用left参数在可视化图像左侧添加注解信息(字体颜色、旋转角度等)
Flink advanced features and new features (VIII)
如何在Ubuntu 18.04和Debian 9上安装PHP 5.6
Flink综合案例(九)
栈与队列——20. 有效的括号
JQ remove an element style
Flex layout
网络安全——文件上传黑名单绕过
position: -webkit-sticky; /* for Safari */ position: sticky;
OWASP zap security testing tool tutorial (Advanced)
Default color setting in uiswitch off state
R language uses the sum function of epidisplay package to calculate the descriptive statistical summary information of the specified variables in dataframe under different grouping variables, visualiz
An error is reported when using activiti to create a database table,
SQL Server 启停作业脚本
FlinkTable&SQL(六)
rhce第一次作业
[机缘参悟-51]:既然人注定要死亡,为什么还要活着?