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 ...
随机推荐
- Ubuntu 14.04 为 root 帐号开启 SSH 登录
1. 修改 root 密码 sudo passwd root 2. 以其他账户登录,通过 sudo nano 修改 /etc/ssh/sshd_config : xxx@ubuntu14:~$ su ...
- wflag
http://stackoverflow.com/questions/41312622/how-to-echo-an-alert-in-php-a-string-some-confusion-with ...
- Levenshtein distance
https://en.wikipedia.org/wiki/Levenshtein_distance 验证码识别 图片中的二维码截取
- Python - 求斐波那契数列前N项之和
n = int(input("Input N: ")) a = 0 b = 1 sum = 0 for i in range(n): sum += a a, b = b, a + ...
- Startssl 现在就启用 HTTPS,免费的!
为什么要使用HTTPS 主要是为了安全,虽然没有100%的安全,但是我们可以尽量提高安全级别,目前大型网站都已经使用HTTPS了 注册StartSSL 注册页面 选择国家 和 输入 邮箱 他们会通过 ...
- 我的第一个chrome扩展(0)——目标
当前有两个方向: 一.实现一个自动解码的地址栏监视器 扩展程序在后台不断监视地址栏输入,地址栏输入并回车后检查输入,若输入符合解码条件则调用网站信息进行解码,并将结果输出到地址栏,否则不改变: 初始阶 ...
- EFI
有CSM的UEFI BIOS应该可以支持EFI Native和legacy两种启动方式吧,在BIOS SETUP选项里面有的选. EFI在开机时的作用和BIOS一样,就是初始化PC,但在细节上却又不一 ...
- Linux的常用基本命令
Linux的常用基本命令. 首先启动Linux.启动完毕后需要进行用户的登录,选择登陆的用户不同自然权限也不一样,其中“系统管理员”拥有最高权限. 在启动Linux后屏幕出现如下界面显示: …… Re ...
- grep与find
grep命令可以指定文件中搜索特定的内容,并将含有这些内容的行标准输出.grep全称是Global Regular Expression Print -c:只输出匹配行的计数.-I:不区分大小写(只适 ...
- webKit和chromium的文章地址
http://blog.csdn.net/column/details/yongsheng.html?&page=1