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 ...
随机推荐
- [daily][CentOS][yum] 删除包的同时一同清理掉安装时一起装进来的依赖包
说起来有点绕口,这个需求是这样的. 就是我yum装A包的时候,同时安装了A的依赖包a1,a2,a3. 当我们使用yum remove A卸载A包的是,a1,a2,a3包并不会一同被卸载掉.如果他们没有 ...
- css+div盒模型研究笔记
红色标记的为默认值 1.border(边框):border-top,border-bottom,border-left,border-right 1.border-color(边框颜色): 2.bor ...
- 采用CSS3设计的登陆界面
body部分内容: <body> <form id="form_id" name="form_id" method="get&quo ...
- ArcGIS中利用ArcMap将地理坐标系转换成投影坐标系(从WKID=4326到WKID=102100)
原文:ArcGIS中利用ArcMap将地理坐标系转换成投影坐标系(从WKID=4326到WKID=102100) 对于非地理专业的开发人员,对与这些生涩的概念,我们不一定都要了解,但是我们要理解,凡是 ...
- SQLSERVER 表名数据库名作为变量 必须使用动态SQL(源自网络)
动态语句基本语法: 1 :普通SQL语句可以用exec执行 Select * from tableName exec('select * from tableName') exec sp_execut ...
- (leetcode)Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 数据库.mdf
对于.mdf文件和.ldf数据库文件, 首先打开SQL Server Management Studio Express,登陆上后,右键点击数据库,附加->选择目标文件就可以了.
- windows10 环境下theano安装
前言:我用的是 Anaconda2 安装python 1. 在Anaconda prompt中输入 conda install mingw libpython 2. 添加环境变量 C:\Anacond ...
- iOS工程如何支持64-bit
苹果在2014年10月20号发布了一条消息:从明年的二月一号开始,提交到App Store的应用必须支持64-bit.详细消息地址为:https://developer.apple.com/news/ ...
- 智能生活 科技无限 CTO VOICE 第二期 智能硬件创新创业专场演讲嘉宾招募
生活不只有诗和远方,还有当下的痛点和需求 当可穿戴设备.虚拟现实.无人机.机器人进入人们视线甚至生活当中 下一个风口就在智能硬件领域上凸显 那么,创业者如何撕掉智能外衣,设计一款有竞争力的智能硬件? ...