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 ...
随机推荐
- npm run build 打包后(直接打包白屏),如何运行在本地查看效果(Apache服务)
转载: https://www.cnblogs.com/qiu-Ann/p/7477593.html 目前,使用vue-cli脚手架写了一个前端项目,之前一直是使用npm run dev 在8080端 ...
- 步步为营-84-数字转化为金额的Js+enter键取消页面刷新
说明:来不及细说了,老铁快上车 function fmoney(s, n) { console.log(s); n = n > && n <= ? n : ; s = pa ...
- Controller中方法返回值其他类型需要添加jackson依赖
第一个 第二个: 第三个 https://www.cnblogs.com/codejackanapes/p/5569013.html:json的博客园 springmvc默认的是:2.Jackson: ...
- vsftp日志xferlog格式分析
vsftp日志xferlog格式分析 [日期:2014-06-25] 来源:Linux社区 作者:Linux [字体:大 中 小] 1.开始vsftp记录日志.修改/etc/vsftpd/vsf ...
- excel vba获取拼音
Function PinYin2(Hz As String) Dim PinMa As String Dim MyPinMa As Variant Dim Temp As Integer, i As ...
- python pop方法
在两个地方见到了pop方法的使用,看起来是之前自己确实故略寡闻了. 在pandas的DataFrame中 import pandas as pd dataframe = pd.read_csv('ir ...
- JavaScript中this 详解
涵义 this 关键字是一个非常重要的语法点.毫不夸张地说,不理解它的含义,大部分开发任务都无法完成. 首先, this 总是返回一个对象,简单说,就是返回属性或方法“当前”所在的对象. this.p ...
- Linux系统监控命令及定位Java线程
1.PID.TID的区分 uid是user id,即用户id,root用户的uid是0,0为最高权限,gid是group id,用户组id,使用 id 命令可以很简单的通过用户名查看UID.GID:~ ...
- 秒懂C#通过Emit动态生成代码
首先需要声明一个程序集名称, 1 // specify a new assembly name 2 var assemblyName = new AssemblyName("Kitty&qu ...
- Eclipse+Maven整合开发Java项目(二)➣webapp3.0以上的Maven项目
概述 Eclipse集成Maven插件,新建maven-archetype-webapp项目的时候,采用的webapp的版本较低,默认是2.3,有些时候,我们希望升级Webapp的版本到3.0(Tom ...