return array;
public int[] rotateLeft3(int[] nums) {
int num1=nums[0];
int num2=nums[1];
int num3=nums[2]
}
public int[] rotateLeft3(int[] nums) {
int len = nums.length ;
int first = nums[0] ;
for (int i = 0 ; i < len-1 ; i++) {
nums[i] = nums[i+1] ;
}
nums[len-1] = first ;
return nums ;
}
public int[] rotateLeft3(int[] nums) {
nums[0]^=nums[1];
nums[1]^=nums[0];
nums[0]^=nums[1];
nums[1]^=nums[2];
nums[2]^=nums[1];
nums[1]^=nums[2];
return nums;
}
a ^= b;
b ^= a;
a ^= b;
That is exactly what I've done in first three lines to swap first two elements, similarly last three lines are swapping last two elements.
nums[0]^=nums[1];
nums[1]^=nums[0];
nums[0]^=nums[1];
nums[1]^=nums[2];
nums[2]^=nums[1];
nums[1]^=nums[2];
public int[] rotateLeft3(int[] nums) {
int len = nums.length ;
int first = nums[0] ;
for (int i = 0 ; i < len-1 ; i++) {
nums[i] = nums[i+1] ;
}
nums[len-1] = first ;
return nums ;
}
above also passed all tests
public int[] rotateLeft3(int[] nums) {
int len = nums.length ;
int first = nums[0] ;
for (int i = 0 ; i < len-1 ; i++) {
nums[i] = nums[i+1] ;
}
nums[len-1] = first ;
return nums ;
}
Did you figure out how this works and why it would work for rotating arrays of any length >= 1?
public int[] rotateLeft3(int[] nums) {
int len = nums.length ;//find lengh of nums array
int first = nums[0] ;assign fist element of arrya to first int variable
for (int i = 0 ; i < len-1 ; i++) {//loop through till the end of the array lenth
nums[i] = nums[i+1] ;//assign right hand side element to left hand side element except first i think??
}
nums[len-1] = first ;//assign first to last elemt now
return nums ; return the nums array which is modified
}
Title | # Comments | Views | Activity |
---|---|---|---|
network + | 7 | 73 | |
How Complex Is This Java Course ? | 9 | 63 | |
wordlen challenge | 3 | 49 | |
JDeveloper 12c for 32 bit | 4 | 36 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
12 Experts available now in Live!