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
- Method
- Stack
- Counting
- string
- Class
- hash table
- Data Structure
- Number Theory
- 구현
- database
- 자바
- 파이썬
- bit manipulation
- Binary Tree
- SQL
- implement
- 코딩테스트
- greedy
- java
- simulation
- Tree
- two pointers
- Math
- geometry
- dynamic programming
- sorting
- Binary Search
- array
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 989. Add to Array-Form of Integer 본문
1. Input
1) int[] num
2) int k
2. Output
1) 배열로 표현된 num과 k의 합을 배열로 표현한 결과
3. Constraint
1) 1 <= num.length <= 10^4
2) 0 <= num[i] <= 9
3) num은 0으로만 구성되어 있지 않다.
4) 1 <= k <= 10^4
4. Example
Input: num = [1,2,0,0], k = 34 -> Output: [1,2,3,4]
Input: num = [2,1,5], k = 806 -> Output: [1,0,2,1]
5. Code
1) 첫 코드(2023/04/03)
Stack<Integer> s = new Stack<Integer>();
List<Integer> answer = new ArrayList<Integer>();
int index = num.length-1, sum = 0;
while(k>0 && index>=0){
sum += k%10 + num[index--];
k /= 10;
s.push(sum%10);
sum = sum>=10 ? 1 : 0;
}
for(int i=index ; i>=0 ; i--){
sum += num[i];
s.push(sum%10);
sum = sum>=10 ? 1 : 0;
}
if(k>0)
while(k>0){
sum += k%10;
s.push(sum%10);
k /= 10;
sum = sum>=10 ? 1 : 0;
}
if(sum==1)
s.push(1);
while(!s.empty())
answer.add(s.pop());
return answer;
- 무지무지 성능 나쁜 코드..
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[백준 온라인 저지] 11005. 진법 변환 2 (0) | 2023.04.03 |
---|---|
[백준 온라인 저지] 2745. 진법 변환 (0) | 2023.04.03 |
[LeetCode/Easy] 933. Number of Recent Calls (0) | 2023.03.31 |
[LeetCode/Easy] 917. Reverse Only Letters (0) | 2023.03.31 |
[백준 온라인 저지] 11050. 이항 계수 1 (0) | 2023.03.31 |