-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create RotateArray.java #149
base: master
Are you sure you want to change the base?
Conversation
return nums; | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alternative solution
class Solution {
private void reverse(int[] nums, int l, int r) {
while (l <= r) {
int temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l++;
r--;
}
}
public void rotate(int[] nums, int k) {
int n = nums.length;
int t = n - (k % n);
reverse(nums, 0, t - 1);
reverse(nums, t, n - 1);
reverse(nums, 0, n - 1);
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both solution are correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
solution are correct
return nums; | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both solution are correct
one more alternate solution. public class Solution {
} |
This is LeetCode solution to the "189.) Rotate Array" problem. Enjoy!