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 ...
随机推荐
- Zookeeper的Watcher 机制的实现原理
基于 Java API 初探 zookeeper 的使用: 先来简单看一下API的使用: public class ConnectionDemo { public static void main(S ...
- MySql在windows上的安装
知乎安装教程 csdn安装教程 一.官网下载 ZIP Archive 内的软件包,mysql-xxx-win64.zip. 二.新建 MySQL 文件夹,解压缩下载包,进入文件夹(mysql-8.0. ...
- burpsuite使用教程和实战详解(一)
1.最近做渗透测试,其实使用一种方式很难全面的对一个web或者app等安全服务器做安全评估,所以要尽可能的对网络安全的渗透测试有一个较全面的认知.不光要熟悉前端和 后天的编程,还有掌握基于这两种编程的 ...
- Axure之全局变量
****全局变量*****1.定义:变量是一个数据的容器,是一个字符串,可设置默认值:2.功能:两个功能:读.写3.特点:随时随地可以对变量进行读和写,不限页面.也就是在不同的页面也可以访问同一个全局 ...
- 无废话-API-01
说明 我的开发环境:VS2013 浏览器:谷歌浏览器(Google Chrome) 1创建项目 1.1添加一个 应用程序"ASP.NET MVC 4 Web 应用程序" 1.2选 ...
- Ubuntu下创建桌面快捷方式(以Pycharm为例)
之后要在Ubuntu虚拟机上玩PyTorch,安装了Pycharm. 然而每次打开Pycharm需要在其bin目录下进入终端,然后输入sh pycharm.sh,很麻烦.既然Ubuntu是桌面系统,为 ...
- 20165206 预备作业3 Linux安装及学习
Linux的安装与学习 - 在自己笔记本上安装Linux操作系统 在安装虚拟机的过程中遇到了不少问题,但也都进行了尝试并得到了解决.首先是在安装VirtulBox的安装上,按照老师给的链接下载安装,不 ...
- spring boot引入json,jsonobject,需要指定jdk15
spring boot引入json,需要指定jdk15 <dependency> <groupId>net.sf.json-lib</groupId> <ar ...
- Nancy 自寄宿
一:简介 Self Hosting 顾名思义,就是自己Host自己.也就是不需要依赖别的应用,而让应用本身就是服务.一个Console程序或者一个Winform程序都是一个应用,Self Hostin ...
- H5利用pattern属性和oninvalid属性验证表单
HTML代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...