๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/LeetCode

[LeetCode] 008. String to Integer (atoi) JAVA

by Bhinney 2022. 12. 21.
 

String to Integer (atoi) - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com


๐Ÿ“ ๋ฌธ์ œ

ENG

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 in and ignore any leading whitespace.
2๏ธโƒฃ 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.
3๏ธโƒฃ 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.
4๏ธโƒฃ 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).
5๏ธโƒฃ If the integer is out of the 32-bit signed integer range [-2^31, 2^31 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -2^31 should be clamped to -2^31, and integers greater than 2^31 - 1 should be clamped to 2^31 - 1.
6๏ธโƒฃ 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.
ํ•œ๊ตญ์–ด (ํ•„์ž๊ฐ€ ์ดํ•ดํ•œ ๊ฒƒ์„ ๋ฐ”ํƒ•์œผ๋กœ)

๋ฌธ์ž์—ด์„ ๋ถ€ํ˜ธ ์žˆ๋Š” 32๋น„ํŠธ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋Š” myAtoi(string s) ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค(C/C++์˜ atoi ํ•จ์ˆ˜).

์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค : 

1๏ธโƒฃ ์„ ํ–‰ ๊ณต๋ฐฑ์€ ์ฝ๊ณ  ๋ฌด์‹œ.
2๏ธโƒฃ ๋ฌธ์ž์—ด์ด ๋๋‚˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ๋‹ค์Œ ๋ฌธ์ž๊ฐ€ '-' ๋˜๋Š” '+'์ธ์ง€ ํ™•์ธ, ๋‘˜ ์ค‘ ํ•˜๋‚˜์ธ ๊ฒฝ์šฐ ์ด ๋ฌธ์ž๋ฅผ ์ฝ์Œ.
      ์ด๊ฒƒ์€ ์Œ์ˆ˜์ธ์ง€ ์–‘์ˆ˜์ธ์ง€๋ฅผ ๊ฒฐ์ •. ๋‘˜ ๋‹ค ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ์–‘์„ฑ์ด๋ผ๊ณ  ๊ฐ€์ •.
3๏ธโƒฃ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋‹ค์Œ ๋ฌธ์ž ๋˜๋Š” ๋์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ์ฝ์Œ. ๋‚˜๋จธ์ง€ ๋ฌธ์ž์—ด์€ ๋ฌด์‹œ.
4๏ธโƒฃ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜(์˜ˆ์‹œ: "123" -> 123, "0032" -> 32). ์ˆซ์ž๋ฅผ ์ฝ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ, 0.
      ํ•„์š”์— ๋”ฐ๋ผ ๋ถ€ํ˜ธ๋ฅผ ๋ณ€๊ฒฝ(2๋ฒˆ ์ฐธ์กฐ).
5๏ธโƒฃ ๊ฐ’์ด ์ •์ˆ˜ ๋ฒ”์œ„[-2^31, 2^31 - 1]๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ ๋ฒ”์œ„์— ๋‚จ๋„๋ก ์„ค์ •. 
      (์˜ˆ์‹œ : -2^31(Integer.MIN_VALUE)๋ณด๋‹ค ์ž‘์€ ์ˆ˜ -> -2^31(Integer.MIN_VALUE),
                2^31 - 1(Integer.MAX_VALUE)๋ณด๋‹ค ํฐ ์ˆ˜ -> 2^31 - 1(Integer.MAX_VALUE))
6๏ธโƒฃ ์ •์ˆ˜๋กœ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜.

Note:
๊ณต๋ฐฑ ๋ฌธ์ž ' '๋งŒ ๊ณต๋ฐฑ ๋ฌธ์ž.
์„ ํ–‰ ๊ณต๋ฐฑ ํ˜น์€ ์ˆซ์ž ๋’ค์˜ ๋‚˜๋จธ์ง€ ๋ฌธ์ž์—ด ์ด์™ธ์˜ ๋ฌธ์ž๋Š” ๋ฌด์‹œํ•˜์ง€ ๋ง ๊ฒƒ.

๐Ÿ“ ์˜ˆ์‹œ

Input: s = "42"
Output: 42
Explanation: The underlined characters are what is read in, the caret is the current reader position.
Step 1: "42" (no characters read because there is no leading whitespace)
         ^
Step 2: "42" (no characters read because there is neither a '-' nor '+')
         ^
Step 3: "42" ("42" is read in)
           ^
The parsed integer is 42.
Since 42 is in the range [-2^31, 2^31 - 1], the final result is 42.

 

Input: s = "   -42"
Output: -42
Explanation:
Step 1: "   -42" (leading whitespace is read and ignored)
            ^
Step 2: "   -42" ('-' is read, so the result should be negative)
             ^
Step 3: "   -42" ("42" is read in)
               ^
The parsed integer is -42.
Since -42 is in the range [-231, 231 - 1], the final result is -42.

๐Ÿ“ ํ’€์ด ์„ค๋ช…

  • ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ํ’€์—ˆ๋‹ค.
  • ๊ฐ€์žฅ ๋จผ์ € stripLeading()์„ ์ด์šฉํ•˜์—ฌ ์„ ํ–‰ ๊ณต๋ฐฑ์„ ์ง€์›Œ์ฃผ์—ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์ค‘๊ฐ„์— ๊ณต๋ฐฑ์ด ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ์ดํ›„์˜ ๋ฌธ์ž์—ด์„ ์ง€์›Œ์ฃผ๊ณ , ๊ทธ ๋‚˜๋ˆˆ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋งŒ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ค‘๊ฐ„์— ๊ณต๋ฐฑ์ด ๋‚˜ํƒ€๋‚˜๋ฉด ํ•ด๋‹น ์ดํ›„์˜ ๋ฌธ์ž๋Š” ์˜๋ฏธ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 0์ด๋ฉด, 0์ด ๋ฆฌํ„ด๋˜๊ฒŒ ํ•˜์˜€๋‹ค.
public static int myAtoi(String s) { 
   s = s.stripLeading().split(" ")[0];
   if (s.length() == 0) {return 0;}
}

  • ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์„ String ๊ฐ’์„ ์„ ์–ธํ•ด์ฃผ์—ˆ๋‹ค.
  • ์ฒซ ๋ฌธ์ž์—ด์— '-'๋‚˜ '+'๊ฐ€ ๋“ค์–ด๊ฐ€๋Š”์ง€ ํ™•์ธํ•ด์ฃผ๊ณ , isPositive๋ฅผ ๋…ผ๋ฆฌ ํƒ€์ž…์œผ๋กœ ์ •ํ•ด์ฃผ์–ด ์Œ์ˆ˜๋ฉด ๊ฑฐ์ง“, ์–‘์ˆ˜๋ฉด ์ฐธ์œผ๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฌธ์ž๊ฐ€ ์•„๋‹ˆ๊ณ , ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๊ฐ€ ์ฒ˜์Œ์— ๋“ค์–ด์˜ค๋ฉด ๋ฐ”๋กœ 0์„ ๋ฆฌํ„ด์‹œ์ผœ ์ฃผ์—ˆ๋‹ค.

 

 

public static int myAtoi(String s) { 

   ...

   String result = "";

   char ch = s.charAt(0);
   boolean isPositive = true;
   if (ch == '-') {
      isPositive = false;
      s = s.substring(1);
   } else if (ch == '+') {
      s = s.substring(1);
   } else if(!Character.isDigit(ch)) {
      return 0;
   }
}

  • ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ธ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์กฐ๊ฑด์‹์— ๋ฌธ์ž๊ฐ€ ์ˆซ์ž์ธ ๊ฒฝ์šฐ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ ์ˆ˜ ์žˆ๋„๋ก ์กฐ๊ฑด์‹์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.
  • ์•„๊นŒ ์„ ์–ธํ•ด์ค€ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์„ ๊ฐ’์— ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.
  • ํ•ด๋‹น ๊ฐ’์ด ์ •์ˆ˜ ํƒ€์ž…์„ ๋ฒ—์–ด ๋‚  ์ˆ˜ ์žˆ์–ด์„œ Long ํƒ€์ž…์œผ๋กœ ๋จผ์ € ๋ณ€ํ™˜์„ ํ•ด์ฃผ๊ณ , ํ•ด๋‹น ๊ฐ’์ด ์ •์ˆ˜ํƒ€์ž…์„ ๋ฒ—์–ด๋‚˜๋ฉด ๋น„๊ต๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ ์ž‘์€ ์ •์ˆ˜ ํ˜น์€ ๊ฐ€์žฅ ํฐ ์ •์ˆ˜์˜ ๊ฐ’์„ ์ž„์˜๋กœ ๋ฆฌํ„ด์‹œ์ผœ์ฃผ์—ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์ฒ˜์Œ์— ์Œ์ˆ˜์ธ์ง€ ์–‘์ˆ˜์ธ์ง€ ํŒ๋ณ„ํ–ˆ๋˜ ๊ฒƒ์— ๋”ฐ๋ผ, ๊ฒฐ๊ณผ ๊ฐ’์„ ์ถœ๋ ฅํ•ด ์ฃผ์—ˆ๋‹ค.
public static int myAtoi(String s) { 
   ...

   for (int i = 0; i < s.length() && Character.isDigit(s.charAt(i)); i++) {
      result += String.valueOf(s.charAt(i));

      if (Long.valueOf(result) > Integer.MAX_VALUE || Long.valueOf(result) < Integer.MIN_VALUE) {
         return isPositive? Integer.MAX_VALUE : Integer.MIN_VALUE;
      }
   }

   if (result.length() == 0) {
      return 0;
   }

   return isPositive? Integer.valueOf(result) : Integer.valueOf(result) * -1;
}

๐Ÿ“ ์ตœ์ข… ํ’€์ด

public static int myAtoi(String s) { 
   s = s.stripLeading().split(" ")[0];
   if (s.length() == 0) {return 0;}

   String result = "";

   char ch = s.charAt(0);
   boolean isPositive = true;
   if (ch == '-') {
      isPositive = false;
      s = s.substring(1);
   } else if (ch == '+') {
      s = s.substring(1);
   } else if(!Character.isDigit(ch)) {
      return 0;
   }

   for (int i = 0; i < s.length() && Character.isDigit(s.charAt(i)); i++) {
      result += String.valueOf(s.charAt(i));

      if (Long.valueOf(result) > Integer.MAX_VALUE || Long.valueOf(result) < Integer.MIN_VALUE) {
         return isPositive? Integer.MAX_VALUE : Integer.MIN_VALUE;
      }
   }

   if (result.length() == 0) {
      return 0;
   }

   return isPositive? Integer.valueOf(result) : Integer.valueOf(result) * -1;
}

โž• ๋‹ค๋ฅธ

์Šคํ„ฐ๋””๋ฅผ ์ค€๋น„ํ•˜๋ฉฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด๋ฅผ ๋ณด๋˜ ์ค‘, ์ฐธ๊ณ ํ•  ๋งŒํ•œ ๊ฒŒ ์žˆ์–ด์„œ ํ•ด๋‹น ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•ด์„œ ๋‚˜์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด ์ฃผ์—ˆ๋”๋‹ˆ

์‹œ๊ฐ„์ด ์ข€ ๋” ๋‹จ์ถ•์ด ๋˜์—ˆ๋‹ค.

์•„๋ž˜์ฒ˜๋Ÿผ ๋ฐ˜๋ชฉ๋ฌธ ์•ˆ์—์„œ ์•„์— ๊ฐ’์„ ๊ณ„์‚ฐํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์†๋„๊ฐ€ ์กฐ๊ธˆ ๋” ๋นจ๋ž๋‹ค.

public static int myAtoi2(String s) { 

   s = s.stripLeading().split(" ")[0];
   if (s.length() == 0) {return 0;}

   char ch = s.charAt(0);
   boolean isPositive = true;
   if (ch == '-') {
      isPositive = false;
      s = s.substring(1);
   } else if (ch == '+') {
      s = s.substring(1);
   } else if(!Character.isDigit(ch)) {
      return 0;
   }

   /* ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ๋ฌธ์ž์—ด์—์„œ ์ˆซ์ž๋ฅผ ์ฝ๊ธฐ */
   long value = 0;
   for (int i = 0; i < s.length() && Character.isDigit(s.charAt(i)); i++) {

      /* getNumericValue() ์œผ๋กœ ์ž๋™์œผ๋กœ ์ˆซ์ž ๋ณ€ํ™˜ + 10์„ ๊ณฑํ•˜๋ฉด์„œ ์ž๋ฆฌ ์ˆ˜ ์˜ฌ๋ ค์„œ ๊ณ„์‚ฐํ•ด์ฃผ๊ธฐ */
      value = value * 10 + Character.getNumericValue(s.charAt(i));

      if (value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
         return isPositive ? Integer.MAX_VALUE : Integer.MIN_VALUE;
      }
   }

   /* ์–‘์ˆ˜๋ฉด ๊ทธ๋Œ€๋กœ ์ •์ˆ˜ ๊ฐ’ ์ถœ๋ ฅ, ์Œ์ˆ˜์ด๋ฉด -1์„ ๊ณฑํ•ด ์Œ์ˆ˜ ์ถœ๋ ฅ */
   return (int)(isPositive? value: value * -1);
}

 

'์•Œ๊ณ ๋ฆฌ์ฆ˜ > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[LeetCode] 012. Integer to Roman JAVA  (0) 2022.12.23
[LeetCode] 004. Median of Two Sorted Arrays JAVA  (0) 2022.12.22
[LeetCode]006. Zigzag Conversion JAVA  (0) 2022.12.21
[LeetCode]007. Reverse Integer JAVA  (0) 2022.12.19
[LeetCode]005. Longest Palindromic Substring JAVA  (0) 2022.12.19

๋Œ“๊ธ€