📙 1. 문제
Link : https://leetcode.com/problems/string-to-integer-atoi/description/
String to Integer (atoi) - LeetCode
Can you solve this real interview question? String to Integer (atoi) - Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function). The algorithm for myAtoi(string s) is as follows: 1. Read
leetcode.com
문제 설명
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).
The algorithm for myAtoi(string s) is as follows:
Read in and ignore any leading whitespace.
Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.
Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.
Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).
If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.
Return the integer as the final result.
Note:
Only the space character ' ' is considered a whitespace character.
Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.
제한 사항
- 0 <= s.length <= 200
- s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.
풀이 1 :
/**
* @param {string} s
* @return {number}
*/
var myAtoi = function(s) {
// 범위(-2^31, 2^31-1)를 벗어나면 경계에 있도록
const num = parseInt(s)
if(isNaN(num)){
return 0
}else if (num > 2** 31 - 1){
return 2**31 -1
}else if (num < (-1)*2**31){
return (-1)*2**31
}else{
return num
}
};
- 처음 Wrong Answer에서는 s문자열에서 문자열이 먼저 온다면 NaN이 나와서, 이에 대한 처리를 해줘서 발생하였다. 처리해줬음에도 왜 안되지, 왜 0을 return하지 싶었다.
Convert these digits into an integer (i.e. "123" -> 123, "0032" > 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).
'읽어 들인 문자열이 숫자가 아니라면, 0으로 간주합니다.'라고 한다. 그래서, 숫자 앞에 '-', '+', ' '가 아니라 문자가 오는 경우라면 0을 return 한다.
🤔 2. 느낀 점
'맞게 풀었는데 뭐가 문제지' 싶어 문제를 여러 번 읽었다. 실제 시험 시간에는 다시 읽으면 시간이 상당히 소요가 될 것이다. 긴장해서 다시 읽어도 안 읽힐 것이다. 처음부터 문제를 "꼼꼼히" 읽도록 하자. 핵심적인 부분은 주석으로 작성하면서 말이다.
코딩테스트에서 가장 중요한 요소 중 하나가 '독해력'이다.
JS의 parseInt()라는 강력한 메서드가 있어서 가볍게 풀 수 있는 문제였다. 다른 문제였으면 상당히 복잡한 알고리즘을 작성했을 것이다.
🤩 3. 한 번 더 짚고 갈 점
- isNaN() : NaN인지 체크하는 메소드
'코딩 테스트 > LeetCode' 카테고리의 다른 글
[4월 15일] container-with-most-water (1) | 2023.04.15 |
---|---|
[4월 14일] Palindrome Number (0) | 2023.04.14 |
[4월 11일] Reverse Integer (0) | 2023.04.11 |
[4월 7일] Longest Palindromic Substring (Bruth Force) (0) | 2023.04.07 |
[4월 6일] Longest Substring Without Repeating Characters (0) | 2023.04.06 |
[4월 6일] Add Two Numbers (0) | 2023.04.06 |
[4월 2일] Two Sum (0) | 2023.04.02 |