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
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
micro services vs rest web services | 16 | 86 | |
more than one jdk and one jre | 1 | 34 | |
windows explorer path to command prompt | 5 | 33 | |
servlet example | 17 | 5 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
17 Experts available now in Live!