Lintcode: Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right) Example
Given "abcdefg" for offset=0, return "abcdefg" for offset=1, return "gabcdef" for offset=2, return "fgabcde" for offset=3, return "efgabcd"
需要注意的是:if (A.length == 0) return new char[0]; 空数组
public class Solution {
/*
* param A: A string
* param offset: Rotate string with offset.
* return: Rotated string.
*/
public char[] rotateString(char[] A, int offset) {
// wirte your code here
if (A==null || A.length == 0) return new char[0];
String str = new String(A);
offset %= str.length();
if (offset == 0) return str.toCharArray();
String first = str.substring(0, str.length()-offset);
String second = str.substring(str.length()-offset);
String res = second + first;
return res.toCharArray();
}
};
法二(推荐):
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==null || str.length==0) return;
offset %= str.length;
if (offset == 0) return;
reverse(str, 0, str.length-1);
reverse(str, 0, offset-1);
reverse(str, offset, str.length-1);
} public void reverse(char[] str, int l, int r) { while (l < r) {
char temp = str[l];
str[l] = str[r];
str[r] = temp;
l++;
r--;
}
}
}
Lintcode: Rotate String的更多相关文章
- 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) ...
- 8. Rotate String
8. Rotate String Description Given a string and an offset, rotate string by offset. (rotate from lef ...
- [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 ...
- LintCode Interleaving String
Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...
- [LintCode] Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
随机推荐
- json 数组转换为js数组
$(function(){ var json = '[{"id":"1","tagName":"apple"},{&qu ...
- 设置myeclipse 项目编码(UTF-8)
设置myeclipse开发项目默认编码为UTF-8Window-->Preferences-->General-->Workspace-->(Text file encodin ...
- 使用dnspod进行简单的HTTP dns解析(防劫持)
https://www.dnspod.cn/httpdns/guide https://www.dnspod.cn/misc/D%2B免费版本接口说明.pdf 最简单的get接口去请求dnspod提供 ...
- 在VC6.0中编译头文件时产生moc文件
1.在FileView视图中 右键点击需要产生moc文件的头文件(就是类中包含Q_OBJECT宏,如果没有这个宏就不需要产生moc文件) 2.在右键菜单中选择Setting... 3.选择Custom ...
- App 上线流程
http://www.cocoachina.com/bbs/read.php?tid=330302
- sql Server 使某一列的值等于行号
declare @i INT update 表名 SET [列名]=@i,@i=@i+ WHERE 条件
- 进度太慢了,扫频仪PCB
实在是画过最纠结的一块PCB,左边布线很轻松,但是右边32芯片用到FSMC,还有很多个引出的IO口,相互交叉纠结在了一起,有几根线一路打了3,4个过孔,实在是难布. 工程查了下是6月17号画原理图的, ...
- iostart命令
Linux系统中的 iostat命令可以对系统的磁盘IO和CPU使用情况进行监控.iostat属于sysstat软件包,可以用yum -y install sysstat 直接安装. 1.基本使用:i ...
- Access数据库创建、使用
1.创建Access数据库表 1)在office中打开Microsoft Access2010,选择空数据库创建数据库StudentInfo. 2)创建新表,在表格第一列选择数据类型,并输入列名. 3 ...
- Power-BI 报表常用功能自适应设置
Power-BI 报表可以跨平台浏览,并自适应多种屏幕大小.在Power-BI 的开发界面下,就有多个属性用于设定在不同屏幕报表的展现方式,以达到更优的用户体验. 1.PC布局:设定报表在PC机上的布 ...