标签:
1,java的swap函数。交换
public static void swap ( int [] data, int a, int b) {
int t = data [a];
data [a] = data [b];
data [b] = t;
}
2,向右移动数组k位(rotate array);
public class Solution {
public void rotate(int[] nums, int k) {
if(nums.length <= 1){
return;
}
//step each time to move
int step = k % nums.length;
reverse(nums,0,nums.length - 1);//放到最后就是向左移
reverse(nums,step,nums.length - 1);
reverse(nums,0,step - 1);
}
//reverse int array from n to m
public void reverse(int[] nums, int n, int m){
while(n < m){
nums[n] ^= nums[m];
nums[m] ^= nums[n];
nums[n] ^= nums[m];
n++;
m--;
}
}
}
3,
标签:
原文地址:http://www.cnblogs.com/yueyebigdata/p/4842106.html