zero-base 프론트엔드 스쿨 ⛪️

2월 코딩테스트 스터디 - 2주차

엄성준 2023. 2. 21. 18:38

[Leet Code - eazy] 66. Plus One

You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.

Increment the large integer by one and return the resulting array of digits.

 

Example 1:

Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].

Example 2:

Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
Incrementing by one gives 4321 + 1 = 4322.
Thus, the result should be [4,3,2,2].

Example 3:

Input: digits = [9]
Output: [1,0]
Explanation: The array represents the integer 9.
Incrementing by one gives 9 + 1 = 10.
Thus, the result should be [1,0].

 

Constraints:

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
  • digits does not contain any leading 0's.

 

나의 첫 번째 풀이

/**
 * @param {number[]} digits
 * @return {number[]}
 */
var plusOne = function(digits) {
    return (Number(digits.join('')) + 1).toString().split('')

};
Wrong Answer
Input
digits = [6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]
 
Output
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,0,0,0]
 
Expected
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,4]
 

- 위와 같이 풀었을 때 71번째 케이스에서 오류가 났습니다. 왜냐하면 [6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,0,0,0]을 join을 통해서 숫자타입으로 변경을 하게되면 bigInt이기 때문에 값이 변형되는걸 알 수 있습니다.

 

그래서 저는 아래와 같이 코드를 수정하였습니다.

/**
 * @param {number[]} digits
 * @return {number[]}
 */
var plusOne = function(digits) {
    return (BigInt(digits.join('')) + BigInt(1)).toString().split('')

};

- BIgInt를 통해서 digits를 join한 값을 감싼 후 BigInt로 감싼(1)을 더한 후 toString을 통해서 문자열로 변환 후 split('')함수를 통해서 나눠진 배열을 return 해주었습니다.

 

[Leet Code - eazy] 67. Add Binary

Given two binary strings a and b, return their sum as a binary string.

 

Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"

 

Constraints:

  • 1 <= a.length, b.length <= 104
  • a and b consist only of '0' or '1' characters.
  • Each string does not contain leading zeros except for the zero itself.

 

나의 풀이

 

/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
var addBinary = function(a, b) {
    let answer = 0

    a10 = parseInt(BigInt(a),2)
    b10 = parseInt(BigInt(b),2)
    console.log(a10)

    answer = BigInt(a10) + BigInt(b10)

    

    return answer.toString(2)
};

- 테스트 케이스에서 계속 걸려서 BIgInt형으로 감싼 뒤 정수형 2진수로 변환하고 더할 때도 BigInt로 감싼뒤 더한 값을 2진수형으로 변환한 값을 return 해주었습니다.

 

[Leet Code - eazy] 7. Reverse Integer

 

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

 

Example 1:

Input: x = 123
Output: 321

Example 2:

Input: x = -123
Output: -321

Example 3:

Input: x = 120
Output: 21

 

Constraints:

  • -2(31승) <= x <= 2(31승) - 1

 

나의 풀이

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * @param {number} x
 * @return {number}
 */
var reverse = function(x) {
  let stringX
    // if(x > math.)
  if(x > 0){
      stringX = String(x).split('').reverse().join('')
      if(stringX > Math.pow(2,31)) return 0
  }else{
      stringX =String(Math.abs(x)).split('').reverse().join('')
      stringX *=-1
       if(stringX < Math.pow(2,31)*-1return 0
  }
    
 
  return stringX
};
cs

 

문제를 풀며 느낀 점 

 

- 음수 양수를 구별해서 문자열을 거꾸로 뒤집는 부분은 어렵지 않았는데 요구조건에 x값이 -2(31승) <= x <= 2(31승) - 1 사이에 있지 않으면 0을 return 하라는 점을 간과한 것 같습니다.