8. Rotate String
8. Rotate String
Description
Given a string and an offset, rotate string by offset. (rotate from left to right)
Example
Given "abcdefg"
.
offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"
Challenge
Rotate in-place with O(1) extra memory.
三步旋转法:
1.将末尾offset个字符旋转
2.将 0- (length-ofset)字符 旋转
3.将全部的字符旋转
public class Solution {
/**
* @param str: An array of char
* @param offset: An integer
* @return: nothing
*/
public void rotateString(char[] str, int offset) {
// write your code here
if(offset == 0 )
return;
if(str.length == 0)
return;
for(int i = 0; i < offset%str.length; i++)
{
char temp = str[str.length-1];
int j = str.length-2;
while(j >= 0)
{
str[j+1] = str[j];
j--;
}
str[0] = temp;
}
}
}
public class Solution {
/**
* @param str: An array of char
* @param offset: An integer
* @return: nothing
*/
public void rotateString(char[] str, int offset) {
// write your code here
if(str.length==0){
return;
}
if(offset==0){
return;
}else if(offset%str.length==0){
return;
}else{
offset=offset%str.length;
int[] temp=new int[offset];
for(int i=str.length-offset;i<str.length;i++){
temp[i-str.length+offset]=str[i];
}
/*for(int j=0;j<temp.length;j++){
System.out.println((char)temp[j]);
}*/
for(int i=str.length-offset-1;i>=0;i--){
str[i+offset]=str[i];
}
for(int i=0;i<offset;i++){
str[i]=(char)temp[i];
}
}
}
}
给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转)
8. Rotate String的更多相关文章
- Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...
- Lintcode: Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...
- LeetCode算法题-Rotate String(Java实现)
这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- [Algorithm] 8. Rotate String
Description Given a string and an offset, rotate string by offset. (rotate from left to right) Examp ...
- 【Leetcode_easy】796. Rotate String
problem 796. Rotate String solution1: class Solution { public: bool rotateString(string A, string B) ...
- 796. Rotate String - LeetCode
Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...
- [Swift]LeetCode796. 旋转字符串 | Rotate String
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- [LeetCode] Rotate String 旋转字符串
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
随机推荐
- Brup Suite 渗透测试笔记(六)
接上次笔记这章记payload的类型分类做一说明: 1.simplelist是一个简单的payload类型,通过配置一个字符串作为payload,也可以手动添加字符串列表. 2.运行文件 Runtim ...
- anaconda利用pip安装module
开始_程序 中搜索:anaconda prompt (控制台) 输入pip 出现pip的一些信息,可以忽略 接着输入 pip install 模块名称 例如:pip install alphalens ...
- 状态压缩dp小结
最近一段时间算是学了一些状态压缩的题目,在这里做个小结吧 首先是炮兵布阵类题目,这类题目一开始给定一个矩形,要求在上面放置炮兵,如果在一格放了炮兵那么周围的某些格子就不能放炮兵,求最大能放置炮兵的数量 ...
- 用HBuilderX 打包 vue 项目 为 App 的步骤
首先打包你的 vue 项目 生成 dist 文件夹,教程请移步 https://www.cnblogs.com/taohuaya/p/10256670.html 看完上面的教程,请确保 你是 将: ...
- Java Insets获取窗口的顶、底、左、右的大小
import java.awt.Insets; import javax.swing.JFrame; public class NewFrame { public static void main(S ...
- HDU 4763 Theme Section(KMP灵活应用)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- tensorflow:验证码的识别(下)
上两篇详细的说明了验证码的识别,不过我们采用的是方法二,下面采用方法一.注意和方法二的区别. 验证码识别方法一: 把标签转为向量,向量长度为40.(4位数字验证码) 验证码的生成和tf.record的 ...
- 页面布局之--Font Awesome+导航
页面布局之--Font Awesome+导航 Font Awesome为您提供可缩放的矢量图标,您可以使用CSS所提供的所有特性对它们进行更改,包括:大小.颜色.阴影或者其它任何支持的效果. 下载地址 ...
- LVS(IPVS)了解
从来都只是看文章,现在手工作一下. 参考URL: https://blog.csdn.net/langyue919/article/details/80935197 https://www.cnblo ...
- 【转】flannel网络的VXLAN及host-gw
http://www.fly63.com/article/detial/1738 VXLAN是Linux内核本身支持的一种网络虚拟化技术,是内核的一个模块,在内核态实现封装解封装,构建出覆盖网络,其实 ...