码迷,mamicode.com
首页 > 其他好文 > 详细

Lintcode:

时间:2015-04-15 06:04:24      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

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]; 空数组

 1 public class Solution {
 2     /*
 3      * param A: A string
 4      * param offset: Rotate string with offset.
 5      * return: Rotated string.
 6      */
 7     public char[] rotateString(char[] A, int offset) {
 8         // wirte your code here
 9         if (A==null || A.length == 0) return new char[0];
10         String str = new String(A);
11         offset %= str.length();
12         if (offset == 0) return str.toCharArray();
13         String first = str.substring(0, str.length()-offset);
14         String second = str.substring(str.length()-offset);
15         String res = second + first;
16         return res.toCharArray();
17     }
18 };

 

Lintcode:

标签:

原文地址:http://www.cnblogs.com/EdwardLiu/p/4427493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!