[알고리즘] LeetCode - Maximum Subarray

Jerry·2021년 1월 16일
0

LeetCode

목록 보기
10/73
post-thumbnail

LeetCode - Maximum Subarray [난이도:Easy]

문제 설명

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

Note

The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.

Solution

/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
let addStrings = function(num1, num2) {
    
    let sum = '';
    let i = num1.length - 1;
    let j = num2.length - 1;

    let carrige = 0;
    for (; i >= 0 || j >= 0;){
        let x = i >= 0 ? num1[i] - '0' : 0;
        let y = j >= 0 ? num2[j] - '0' : 0;
        
        let tempSum = carrige + x + y;
        sum = (tempSum % 10) + sum;
        
        //carrige = Math.trunc(tempSum / 10);
        carrige = (tempSum / 10)>>0; //소수점 제거 비트 연산. Math.trunc(tempSum / 10) 보다 빠름
        i--;
        j--;
    }

    return sum;
};

새로 학습한 내용

^~^

profile
제리하이웨이

0개의 댓글