The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.
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 |
---|---|---|---|
login jsp example | 24 | 69 | |
hibernate insert example | 13 | 34 | |
Unhandled exception type Exception | 18 | 43 | |
add projects t working set in maven | 2 | 24 |
Join the community of 500,000 technology professionals and ask your questions.