class Solution {
public int[] sortedSquares(int[] nums) {
int[] a=new int [nums.length];
for(int i=0;i<nums.length;i++){
a[i]=nums[i]*nums[i];
}
Arrays.sort(a);
return a;
}
}
简单的自己做了出来,基本有了了解!
下面是一道例题!
class Solution {
public void rotate(int[] nums, int k) {
int n=nums.length;
int[] newArr= new int [n];
for(int i=0;i<n;i++){
newArr[(i+k)%n]=nums[i];
}
System.arraycopy(newArr,0,nums,0,n);
}
}
这道题有很多解法,我这个是最简单但是是最失败的,时间和空间复杂度都为O(n)。
下面这个是大神的解法,恕我看不懂,但还是感觉很牛逼!
nums = "----->-->"; k =3
result = "-->----->";
reverse "----->-->" we can get "<--<-----"
reverse "<--" we can get "--><-----"
reverse "<-----" we can get "-->----->"
this visualization help me figure it out :)