Recover Rotated Sorted Array恢复旋转排序数组
public class Solution {
/**
* @param nums: An integer array
* @return: nothing
*/
public void recoverRotatedSortedArray(List<Integer> nums) {
// write your code here
int len = nums.size()-1 ;
for(int i = 0 ; i < nums.size()-1 ; i++){
if(i>0 && nums.get(i) < nums.get(i-1)){
swap(nums , 0 , i-1) ;
swap(nums , i , len) ;
swap(nums , 0 , len ) ;
return;
}
}
}
public void swap(List<Integer> nums , int start , int end){
for(int i = start , j = end ; i < j ; i++ , j--){
int temp = nums.get(i) ;
nums.set(i ,nums.get(j));
nums.set(j , temp) ;
}
}
}
该博客讨论了一个Java解决方案,用于恢复一个已旋转的排序数组。代码中包含了一个名为`recoverRotatedSortedArray`的方法,该方法通过比较数组元素来找到旋转点,并进行必要的交换操作,将数组重新排序。此外,还提供了一个辅助的`swap`方法来交换数组中的元素。
201

被折叠的 条评论
为什么被折叠?



