[虚拟机OA]Shifting Strings字符串移位

We define the following operations on a string:
Left Shift: A single circular rotation of the string in which the first character becomes the last character and all other characters are shifted one index to the left. For example, abcde becomes bcdea after one left shift and cdeab after two left shifts.

Right Shift: A single circular rotation of the string in which the last character becomes the first character and all other characters are shifted one index to the right. For example, abcde becomes eabcd after one right shift and deabc after two right shifts.

Function Description

Complete the function getShiftedString in the editor below. The function must return the string s after performing the stated shifts.

getShiftedString has the following parameter(s):

s: the string to shift

leftShifts: integer

rightShifts: integer

题意:
字符串移位。

思路

取摸。

代码

 1 public String getShiftedString(String str, int leftShifts, int rightShifts) {
 2     if (str == null || str.length() == 0) {
 3         return null;
 4     }
 5     if (leftShifts + rightShifts == 0) {
 6         return str;
 7     }
 8     leftShifts = (leftShifts - rightShifts) % str.length();
 9     return str.substring(leftShifts) + str.substring(0, leftShifts);
10 }

猜你喜欢

转载自www.cnblogs.com/liuliu5151/p/11508068.html