<Java> length, length(), size()의 차이
1. length : 배열의 길이, arrays(int[], double[], String[])
ex:) new int[7]
2. length() : 문자열의 길이, String related Object(String, StringBuilder etc)
ex:) "length"
3. size() : 컬렉션프레임워크 타입의 길이, Collection Object(ArrayList, Set etc)
ex:) new ArrayList<Object>
[내가 푼 방식]
1. arr 길이 구해서 답 arr[][] 길이 지정
2. 각 위치의 값의 합을 두 개의 for문으로 입력
* length, length(), size()의 차이를 잘 몰랐음.
*new int[][] 방식이 헷갈림.(파이썬과의 명확한 구분 알아두기 !)
class Solution {
public int[][] solution(int[][] arr1, int[][] arr2) {
int a1 = arr1.length;
int a2 = arr1[0].length;
int[][] answer = new int[a1][a2];
for(int y=0; y< a2; y++){
for(int x=0; x<a1; x++){
answer[x][y] = arr1[x][y]+arr2[x][y];
}
}
return answer;
}
}
[참고하고 싶은 방식]
class SumMatrix {
int[][] sumMatrix(int[][] A, int[][] B) {
int row = Math.max(A.length, B.length);
int col = Math.max(A[0].length, B[0].length);
int[][] answer = new int[row][col];
for(int i=0; i<row ; i++){
for(int j=0; j<col; j++){
answer[i][j] = A[i][j] + B[i][j];
}
}
return answer;
}
'Algorithm 알고리즘' 카테고리의 다른 글
[프로그래머스 Lv1]평균구하기(JAVA) (0) | 2021.08.27 |
---|---|
[프로그래머스 Lv1]하샤드 수(JAVA) (0) | 2021.08.27 |
[프로그래머스 Lv1]핸드폰 번호 가리기(JAVA) (0) | 2021.08.26 |
[프로그래머스 Lv1]x만큼 간격이 있는 n개의 숫자(JAVA) (0) | 2021.08.24 |
[프로그래머스 Lv1]직사각형 별찍기(JAVA) (0) | 2021.08.23 |