Codility_L2-1_ CyclicRotation

 


An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).

The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.

Write a function:

class Solution { public int[] solution(int[] A, int K); }

that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given

    A = [3, 8, 9, 7, 6]
    K = 3
the function should return [9, 7, 6, 3, 8]. Three rotations were made:

    [3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7]
    [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9]
    [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]
For another example, given

    A = [0, 0, 0]
    K = 1
the function should return [0, 0, 0]

Given

    A = [1, 2, 3, 4]
    K = 4
the function should return [1, 2, 3, 4]

Assume that:

N and K are integers within the range [0..100];
each element of array A is an integer within the range [−1,000..1,000].
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.




第一個參數要輸入目標要做shift的陣列
第二個參數輸入的K代表要往右位移多少單位

Code_Ver1. (62%)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;

// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");

class Solution {
    public int[] solution(int[] A, int K) {
        // write your code in C# 6.0 with .NET 4.5 (Mono)        
        int shiftUnit = K;
        int[] resArr = new int[A.Length];
        for(int idxArr=0;idxArr<A.Length;idxArr++)
        {
            //Console.Write($"{A[idxArr]},");
            int ele = A[idxArr];
            int targetIdx = idxArr+shiftUnit;
            if( targetIdx > A.Length-1){
                targetIdx = targetIdx - (A.Length);
            }
            resArr[targetIdx] = ele;
        }
        return resArr;
    }
}









這裡再次做些微調整

忽略到
假如今天是[1,2,3]但外部輸入的K是4的時候會超出索引

第一輪K=4,shiftIdx=4+0=4
理論上要再將4看成是1 也就是4-3=1已經跑過一輪後的位移1個
[0,1,0]

在第二輪K=4,shiftIdx=4+1=5
理論上要再將5看成是2 也就是5-3=2已經跑過一輪後的位移2個
[0,1,2]

在第三輪K=4,shiftIdx=4+2=6
理論上要再將6看成是3 也就是6-3=3已經跑過一輪後的位移3個
但此時會超出索引
應該在等於跟陣列長度時候改為索引為0從頭開始




但這樣子思考可能還有欠缺
假如今天是[1,2,3]但外部輸入的K是5的時候會超出索引


第一輪K=5,shiftIdx=5+0=5
理論上要再將5看成是2 也就是5-3=2已經跑過一輪後的位移2個
[0,1,0]

在第二輪K=5,shiftIdx=5+1=6
理論上要再將5看成是3 也就是6-3=3已經跑過一輪後的位移3個
[0,1,2]

在第三輪K=5,shiftIdx=5+2=7
理論上要再將7看成是4 也就是7-3=4已經跑過一輪後的位移4個
此時又會再度超出索引

應該在大於等於陣列長度時候再次把索引改為4-3

而這裡會發現判斷是否新位移的索引超出陣列上限的校正不斷重複

因此正確的解法如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;

// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");

class Solution {
    public int[] solution(int[] A, int K) {
        int shiftUnit = K;
            int[] resArr = new int[A.Length];
            for (int idxArr = 0; idxArr < A.Length; idxArr++)
            {
                //Console.Write($"{A[idxArr]},");
                int ele = A[idxArr];
                int targetIdx = idxArr + shiftUnit;
                while (targetIdx > A.Length - 1)
                {
                    targetIdx = targetIdx - (A.Length);
                }                
                resArr[targetIdx] = ele;
            }
            return resArr;
    }
}






留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題