lintcode做题总结, 题目Rotate String 8
来源:互联网 发布:泡沫 网络歌手 编辑:程序博客网 时间:2024/05/16 11:11
这个题目很简单,只是在旋转的时候有一个很巧妙的方法,从转动的位置开始将两个部分反转,然后再将整个字符串反转即可
比如需要将12345变成34512
1. 12345 -> 21345
2. 21345 -> 21543
3. 21543 -> 34512
public class Solution { /** * @param str: an array of char * @param offset: an integer * @return: nothing */ public void rotateString(char[] A, int offset) { // write your code here if (A == null || A.length == 0) { return; } offset = offset % A.length; reverse(A, 0, A.length - offset - 1); reverse(A, A.length - offset, A.length - 1); reverse(A, 0, A.length - 1); return; } private void reverse(char[] A, int start, int end) { for (int i = start, j = end; i < j; i++, j--) { char temp = A[i]; A[i] = A[j]; A[j] = temp; } }}
0 0
- lintcode做题总结, 题目Rotate String 8
- leedcode做题总结,题目Rotate Image 2012/03/17
- leetcode做题总结,题目Rotate List 2012/03/27
- 【LintCode】Rotate String 旋转字符串
- [Lintcode]Rotate String 两步翻转法
- Lintcode题目总结
- lintcode做题总结, Sept 15
- lintcode做题总结, Sept 16
- lintcode做题总结, Sept 18
- lintcode做题总结, Sept 30
- lintcode做题总结, Oct 01
- lintcode做题总结, Oct 05
- lintcode做题总结, Oct 07
- lintcode做题总结, Oct 31 A
- lintcode做题总结, Oct 31 B
- #8 Rotate String
- leedcode做题总结, 题目Surrounded Regions130
- leedcode做题总结, 题目Median 80
- 高效程序的秘密_hacker's delight读书笔记
- LeetCode Convert Sorted List to Binary Search Tree
- matlab绘图时设置原点的位置
- LeetCode Convert Sorted Array to Binary Search Tree
- cyuqpcljzwrmku
- lintcode做题总结, 题目Rotate String 8
- Sublime Text 3 开发php
- LeetCode Reorder List
- zt我的Delphi开发经验谈
- Epoll模型服务器实现
- 网络通信 Tcp
- Coproject - a RIA Caliburn.Micro demo, part 13
- 第五章 Fork/Join框架
- Altium Designer 笔记之生成gerber文件