> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/ladder_code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/ladder_code/chapter1/recover-rotated-sorted-array.md).

# Recover Rotated Sorted Array

题目：

Given a **rotated** sorted array, recover it to sorted array in-place.

**Example**

`[4, 5, 1, 2, 3]`->`[1, 2, 3, 4, 5]`

分析：

三步翻转法可以实现rotated

1.recover rotated sorted array

45 123 找到这个位置没必要二分，直接N过来· ->54 321-> 123 45

解法：

```
    public void recoverRotatedSortedArray(List<Integer> nums) {

        int pos = -1;
        for(int i = 0; i < nums.size()-1; i++){
            if(nums.get(i) > nums.get(i+1)){
                pos = i;
                break;
            }
        }
        reverse(nums, 0, pos);
        reverse(nums, pos+1, nums.size()-1);
        reverse(nums, 0, nums.size()-1);
    }

    public void reverse(List<Integer> nums, int start, int end){
        while(start < end){
            int temp = nums.get(start);
            nums.set(start++, nums.get(end));// arraylist set用法
            nums.set(end--, temp);
        }
    }
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://nataliekung.gitbook.io/ladder_code/chapter1/recover-rotated-sorted-array.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
