KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • js
  • 다이나믹 프로그래밍
  • Level 0
  • Level 2
  • 문자열
  • LeetCode
  • C++
  • codeup
  • 정렬
  • Level 1
  • string
  • 백준
  • 그래프
  • 수학
  • tree
  • programmers
  • 제코베 JS 100제
  • recursion
  • C
  • Level1

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / String / 43번 / Multiply Strings / JS

2023. 4. 19. 23:29

< 문제 바로가기 >

 

Multiply Strings - LeetCode

Can you solve this real interview question? Multiply Strings - Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Note: You must not use any built-in BigInteger library

leetcode.com

 

< 문제 간단설명 >

문자열로 들어온 두 수의 곱을 반환하면 되는 문제이다. 단 문자열로 들어온 수는 정수의 범위를 넘는다. 그리고 되도록이면 BigInt 내장 라이브러리는 사용하지 않고 구현하도록 한다.

 

/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */

/* 
    < example >
    123 * 456

          1  2  3
       *  4  5  6
    ----------------
             1  8
          1  2
          6
    ----------------
          1  5     
       1  0
       5
    ----------------
       1  2
       8
    4
    ----------------
  = 5  6  0  8  8
*/
var multiply = function (num1, num2) {
  if (num1 === '0' || num2 === '0') {
    return '0';
  }

  let digitList = Array.from({ length: num1.length + num2.length }, () => 0);

  let [curNum1, curNum2] = [0, 0];
  let [idx1, idx2] = [0, 0];
  let carry = 0;
  let sum = 0;
  for (let i = num2.length - 1; i >= 0; i -= 1) {
    curNum2 = Number(num2[i]);
    for (let j = num1.length - 1; j >= 0; j -= 1) {
      curNum1 = Number(num1[j]);

      // i와 j는 각 숫자의 자릿수를 나타냄
      // 한자리 수 * 한자리 수 = 한자리 수 || 두자리 수
      // 따라서 두 자리수를 확보해놓고 진행
      idx1 = i + j;
      idx2 = i + j + 1;

      sum = digitList[idx2] + curNum1 * curNum2;
      digitList[idx2] = sum % 10;
      // 만약 두 자리수가 되면 올림수를 윗 자리에다가 더해줌
      carry = Math.floor(sum / 10);
      digitList[idx1] += carry;
    }
  }

  // 맨 앞에 올림수가 없어서 기존에 설정해놓은 0이 그대로 있을 경우,
  // shift()로 없애줌
  if (digitList[0] === 0) {
    digitList.shift();
  }

  return digitList.join('');
};
저작자표시 (새창열림)

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Linked List / 234번 / Palindrome Linked List / JS  (0) 2023.04.19
LeetCode / Linked List / 19번 / Remove Nth Node From End of List / JS  (0) 2023.04.19
LeetCode / String / 14번 / Longest Common Prefix / JS  (0) 2023.04.19
LeetCode / Simulation / 54번 / Spiral Matrix / JS  (0) 2023.04.18
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Linked List / 234번 / Palindrome Linked List / JS
    • LeetCode / Linked List / 19번 / Remove Nth Node From End of List / JS
    • LeetCode / String / 14번 / Longest Common Prefix / JS
    • LeetCode / Simulation / 54번 / Spiral Matrix / JS
    KimMinJun
    KimMinJun

    티스토리툴바