-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_1427.java
More file actions
30 lines (26 loc) · 845 Bytes
/
Copy pathP_1427.java
File metadata and controls
30 lines (26 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package leetcode.easy;
public class P_1427 {
public String stringShift(String s, int[][] shift) {
int k = 0;
for (int[] dir : shift) {
k += dir[0] == 1 ? dir[1] : -dir[1];
}
k = Math.floorMod(k, s.length());
final char[] chars = s.toCharArray();
rotate(chars, k);
return new String(chars);
}
public static void rotate(char[] chars, int k) {
k %= chars.length;
reverse(0, chars.length - 1, chars);
reverse(0, k - 1, chars);
reverse(k, chars.length - 1, chars);
}
private static void reverse(int from, int to, char[] chars) {
for (int i = from; 2 * i < to + from; i++) {
final char temp = chars[i];
chars[i] = chars[to + from - i];
chars[to + from - i] = temp;
}
}
}