关闭

Rotate Array

169人阅读 评论(0) 收藏 举报
分类:
<gs id="79dc6257-769a-42f4-81f6-b33ab08ff9a5" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">class</gs> Solution <gs id="05170077-7bfc-4351-b8a4-fa2515562794" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">{
</gs>public:
    void <gs id="ac5838b1-a61a-4aee-82f4-fdd288dfc507" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">rotate</gs><gs id="a17fda3f-5eb1-43e9-98a9-0cfec7befc3a" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">(</gs>vector<<gs id="046abc55-372e-44ff-a6f9-c9e11a7e6a3a" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">int</gs>>& <gs id="58754dbb-13ca-4dfc-bc17-ab61a01814a6" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs>, <gs id="d33ff531-d83e-424b-8308-8f013ff6987f" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">int</gs> k) <gs id="ffb46127-ae00-4fbe-9f5a-58c825ff3aa7" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">{
        </gs><gs id="7ff00198-7bc4-4e89-85a0-2ce6babf5066" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">int</gs> n = <gs id="5bde9243-1bbf-43bb-a9d1-d04f0520ca8c" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs><gs id="8865a852-2907-4ba4-9984-a6c5105a27d0" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">.</gs>size<gs id="a6290452-ae3c-470c-8a4a-6ea7c12985cb" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">(</gs>);
        if (<gs id="0dc241d9-b716-4bf7-bba0-00eacf229b7b" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">k > n</gs>) k = k % n;
        if (n == k || k < 1) return;

        vector<<gs id="dc4d2b6a-7874-4b68-afe4-aa62ed5879c9" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">int</gs>> temp<gs id="0d00a42a-f117-4e74-9011-810c2ed25333" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">(</gs><gs id="7babb397-0ae2-48c7-9063-3b422632fba3" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs><gs id="eb194a4a-5c57-4aca-94d3-c055655cf215" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">.</gs><gs id="c2e8f797-380b-44d7-a707-ee21a928609e" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">begin</gs><gs id="615e1331-58e2-41a9-8a30-8ab0a5aaefac" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">(</gs>), <gs id="3844ea73-8aba-47bd-a1ac-4080eba3d6d5" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs><gs id="f9c18198-3aca-4c3a-a7ce-97c57bf90fc7" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">.</gs><gs id="95a965d2-e598-45b4-8ec6-163c035aef8a" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">begin</gs><gs id="7e5e43a1-363c-4cb4-9124-1fc9950ebccd" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">(</gs>) + n - k);
    
        for (<gs id="ed62b337-063d-492b-ad8c-25c619114f01" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">int</gs> <gs id="1eaa8f00-4bdf-4a42-a9f1-44106bab2675" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs> = 0; <gs id="33bbaa53-b3a4-40d2-abb9-9c232065535c" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs> < k; i++) <gs id="332c2524-93e8-40ee-ae7c-c5729720f3d2" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">{
            </gs><gs id="c71bee51-118a-47c5-889f-9809c87aaee4" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs><gs id="b4cca93c-fc0c-4be3-892c-f11057990db0" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">[</gs><gs id="43b5f029-f16d-4a8b-9c68-7f6f41e68184" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs>] = <gs id="c7724ef4-6b07-4262-8fcc-8860777e67ca" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs><gs id="c5947857-f7cd-4c65-b51a-c4e65e0753cb" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">[</gs>n - k + <gs id="e7f7d96b-74b1-4c83-8141-106e80ea91dd" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs>];<gs id="8911ab8a-c5b8-4797-893d-f79de363da21" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">
        }</gs>
        for (<gs id="1e99f4e0-655b-470d-9eff-54345355201c" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">int</gs> <gs id="5ee78fb7-1a06-4ef7-a400-3142abaf9028" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs> = k, j = 0; <gs id="599bd548-6b40-4b30-937b-64ff41fc52eb" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs> < n; i++, j++) <gs id="48706289-7deb-4782-8350-0a3b94a37e8e" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">{
            </gs><gs id="678437c6-ad1c-41f6-b5f8-a976a5efc97a" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs><gs id="4f9cfbf7-3f0c-47fa-bba1-e53fd12c08ca" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">[</gs><gs id="737dfb44-9e94-4afb-b3fe-436f7a710476" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs>] = temp<gs id="25009f8d-add6-4453-a5d3-d77a2c3ec4c5" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">[</gs>j];<gs id="c8c7112d-0a08-4c40-89f1-7744b909b7f0" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">
        }</gs><gs id="457f0602-7a5c-4dd6-b843-ba9782d6a710" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">
    }</gs><gs id="de806a80-2d5a-457e-a6e5-29fbbda86e11" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">
}</gs>;

Time O (n), space O (n);


class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        int n = nums.size();
        if (k > n) k = k % n;
        if (n == k || k < 1) return;

        reverse(nums.begin(), nums.begin()+n-k);
        reverse(nums.begin()+n-k, nums.end());
        reverse(nums.begin(), nums.end());
    }
};
Time O(n), space O(1)

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:51527次
    • 积分:2258
    • 等级:
    • 排名:第16607名
    • 原创:182篇
    • 转载:7篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论