티스토리 뷰
leetcode.com/problems/multiply-strings/
구현물로 Medium정도의 난이도이다
문자열 길이가 최대 109라 Number 형태의 곱셈이 아닌 각 자리수의 곱을 계산해야 한다.
import java.util.Arrays;
public class MultiplyString {
public static void main(String[] args) {
System.out.println(multiply("5", "12"));
}
public static String multiply(String num1, String num2) {
if (isZero(num1) || isZero(num2)) return "0";
int[] res = new int[220];
for(int i = num1.length() - 1, x=0; i >= 0; i--, x++) {
int n1 = convertCharToInteger(num1.charAt(i));
for(int j = num2.length() - 1, y=0; j >= 0; j--, y++) {
int n2 = convertCharToInteger(num2.charAt(j));
res[x+y] += n1 * n2;
}
}
StringBuilder sb = new StringBuilder();
for(int i = 0; i < num1.length() + num2.length(); i++) {
if (res[i] >= 10) {
res[i+1] += res[i] / 10;
res[i] = res[i] % 10;
}
sb.insert(0, res[i]);
}
if(sb.charAt(0) == '0') sb.deleteCharAt(0);
return sb.toString();
}
private static int convertCharToInteger(char c) {
return c - 48;
}
private static boolean isZero(String num) {
return num.length()==1 && num.charAt(0) == '0';
}
}
'Computer Science > 알고리즘' 카테고리의 다른 글
[Leetcode] Find the Difference (0) | 2020.09.25 |
---|---|
[Leetcode] Car Pooling (1) | 2020.09.22 |
[정올] 순서찾기 (문제번호 : 2437) (0) | 2020.02.17 |
[정올] 자리배치 (문제번호 : 2098) (0) | 2020.02.14 |
[KOITP] 꽃 진열 (문제ID : IOI_1999_FLOWER) (0) | 2020.02.14 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- SpringBoot
- 비트코인
- excel parsing
- 백준
- k8s
- Bruteforce
- CARDANO
- DP
- 암호화폐
- white paper
- gRPC
- Vue.js
- 동적계획법
- Bitcoin
- Redis
- 블록체인
- Spring
- Blockchain
- architecture
- 아키텍처
- 스프링
- Nealford
- 사토시 나가모토
- 알고리즘
- vuejs
- leetcode
- 스프링 시큐리티
- Java
- kubernetes
- 카르다노
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함