본문 바로가기

알고리즘-연습문제

배열 문제

면접때  배열관련한 문제가 나왔는데  집에와서 다시 풀어봄

 

input 2차원 배열을  시계방향으로 90도 돌려서 output 을 출력한다

example)

input

{1,  2,   3},

{4,  5,  6},

{7,  8,  9}

 

output

{7,  4,  1},

{8,  5,  2},

{9,  6,  3}

 

 

 

package main

import "fmt"

func main() {
    input := [][]int{
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9},
        {10, 11, 12},
    }

    runFunc(input)
}

func runFunc(input [][]int) {
    x := len(input)
    y := len(input[0])

    output := make([][]int, y)
    for i := range output {
        output[i] = make([]int, x)
    }

    tempArr := []int{}
    for i := 0; i < y; i++ {
        for j := x - 1; j >= 0; j-- {
            tempArr = append(tempArr, input[j][i])
        }
    }

    k := 0
    for i := 0; i < len(output); i++ {
        for j := 0; j < len(output[0]); j++ {
            output[i][j] = tempArr[k]
            k++
        }
    }

    fmt.Println(output)
}