[Algorithm] 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.
Solution
/**
* @param str: An array of char
* @param offset: An integer
* @return: nothing
*/
void reverseString(string &str, int start, int end)
{
int swap = ; while(start < end){
// Exchange the element between the start and the end.
swap = str[start];
str[start] = str[end];
str[end] = swap; // Move to the next index.
start++; end--;
} return ;
} void rotateString(string &str, int offset) {
// Get the length of the string.
int len = str.length();
if( !len || offset <= ) return ;
if( offset > len ) offset %= len; // Reverse the first section.
reverseString(str, , len-offset-);
// Reverse the second section.
reverseString(str, len-offset, len-);
// Reverse the entire section.
reverseString(str, , len-); return ;
}
[Algorithm] 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) ...
- 8. Rotate String
8. Rotate String Description Given a string and an offset, rotate string by offset. (rotate from lef ...
- 【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 ...
随机推荐
- [RK3288][Android6.0] 调试笔记 --- Goodix GT9和GT9F区别【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/78341425 Platform: RK3288 OS: Android 6.0 Kernel ...
- Proxy authentication confirmation prompt keeps popping up although the user/password is saved 火狐浏览器一直提示输入代理的账号和密码
https://support.mozilla.org/zh-CN/questions/943488 signon.autologin.proxy;true network.proxy.share_p ...
- luogu 4427 求和
bjoi 2018 求和 唯一一道可能切的题一个数组还没开long long就成0分了 题目大意: 一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的k次方和,而且每次的k可能是不同的 此处 ...
- Java实现二叉树遍历
参考资料: http://blog.csdn.net/wuwenxiang91322/article/details/12231657 环境: Java: jdk1.8.0_91 import jav ...
- sqlserver2000连接失败,不存在或拒绝访问
一 看ping 服务器IP能否ping通. 这个实际上是看和远程sql server 2000服务器的物理连接是否存在.如果不行,请检查网络,查看配置,当然得确保远程sql server 2000服务 ...
- E20171106-hm
pulldown adj. 折叠式的; pulldown menu 下拉菜单
- bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛【bfs】
满脑子dp简直魔性 模拟题意bfs转移即可 #include<iostream> #include<cstdio> #include<queue> using na ...
- bzoj 4297: [PA2015]Rozstaw szyn【瞎搞】
从叶子往上先拓扑一下,建立虚拟root,从root开始dfs.注意到每个点的最优取值一定是一个区间(中位数区间),从儿子区间推出父亲区间即可 #include<iostream> #inc ...
- bzoj1076: [SCOI2008]奖励关(期望dp+状压dp)
1076: [SCOI2008]奖励关 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2989 Solved: 1557[Submit][Statu ...
- Java经典算法之折半查找(二分法)
采用二分法时,数据应是有序并且不重复的 与小时候玩的猜数游戏是一样的,会让你猜一个他所想的1~100之间的数,当你猜了一个数后,他会告诉你三种选择中的一个,比他想的大,或小,或猜中了,为了能用最少的次 ...