Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- Matrix
- string
- Class
- hash table
- Method
- Binary Search
- dynamic programming
- java
- implement
- Binary Tree
- greedy
- geometry
- two pointers
- Math
- Counting
- 코테
- Data Structure
- sorting
- 파이썬
- Stack
- 구현
- array
- SQL
- simulation
- bit manipulation
- Tree
- 코딩테스트
- database
- 자바
- Number Theory
Archives
- Today
- Total
코린이의 소소한 공부노트
[프로그래머스/Lv.0] 2차원으로 만들기 본문
1. Input
1) 정수 배열 num_list
2) 정수 n
2. Output
1) num_list를 2차원 배열로 바꾼 결과
2) Example에서 설명
3. Constraint
1) num_list의 길이는 n의 배수
2) 0 <= num_list의 길이 <= 150
3) 2 <= n < num_list의 길이
4. Example
Input: num_list={1,2,3,4,5,6,7,8}, n=2 -> Output: {{1,2},{3,4},{5,6},{7,8}}
설명: num_list의 길이가 8이고 n=2이므로 2*4배열로 변경. 이때 가로가 2이므로 배열의 열이 2, 세로가 4이므로 배열의 행이 4가 된다. 따라서 결과는 {{1,2},{3,4},{5,6},{7,8}}
- 비교: {{1,2,3,4},{5,6,7,8}}은 4*2배열이다.
5. Code
1) 첫 코드(2022/10/25)
int[][] answer = new int[num_list.length/n][n];
int index = 0;
for(int i=0 ; i<num_list.length/n ; i++){
for(int j=0 ; j<n ; j++){
answer[i][j] = num_list[index++];
}// for j
}// for i
return answer;
2) 간단하게 바꾼 코드(2022/10/25)
int[][] answer = new int[num_list.length/n][n];
for(int i=0 ; i<num_list.length ; i++)
answer[i/n][i%n] = num_list[i];
return answer;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[프로그래머스/Lv.0] 배열 회전시키기 (0) | 2022.10.25 |
---|---|
[프로그래머스/Lv.0] 공 던지기 (0) | 2022.10.25 |
[프로그래머스/Lv.0] 점의 위치 구하기 (0) | 2022.10.25 |
[프로그래머스/Lv.0] 구슬을 나누는 경우의 수 (0) | 2022.10.25 |
[프로그래머스/Lv.0] 가위 바위 보 (0) | 2022.10.25 |