Skip to content

Commit 7d9d0a5

Browse files
authoredJul 2, 2021
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function) (#178)
* String to Integer converter like ATOI in C/C++ Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function) * Rename Java file as per standard and add details to .md file Rename Java file as per standard and add details to .md file
1 parent e78db76 commit 7d9d0a5

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed
 

‎Java/string-to-integer-atoi.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.bst.myexamples;
2+
3+
/**
4+
* Solution accepted on Leetcode with 7ms runtime and 39.2MB memory
5+
*
6+
* String to Integer (atoi)
7+
* Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).
8+
* Approach
9+
* 1.Prepare String that is having only +- sign and number value
10+
* Convert to BigInteger to compare value with 32bit signed Integer range and clamp if goes out of range
11+
* return output with 0 if no string number found or non number value found before any number value
12+
*/
13+
14+
import java.math.*;
15+
class StringToIntegerATOI {
16+
17+
public static void main(String[] args){
18+
/* StringToIntegerATOI sta = new StringToIntegerATOI();
19+
System.out.println(sta.myAtoi("-20000000000000"));
20+
*/
21+
22+
}
23+
public int myAtoi(String s) {
24+
25+
StringBuilder sb = new StringBuilder();
26+
27+
for(int i=0; i<s.length(); i++){
28+
29+
if(sb.length() == 0){
30+
if(Character.isWhitespace(s.charAt(i)))
31+
continue;
32+
33+
if('-' == s.charAt(i)){
34+
sb.append("-");
35+
continue;
36+
}
37+
38+
if('+' == s.charAt(i)){
39+
sb.append("+");
40+
continue;
41+
}
42+
}
43+
44+
if(Character.isDigit(s.charAt(i)))
45+
sb.append(s.charAt(i));
46+
else
47+
break;
48+
49+
}
50+
51+
String val = sb.toString().replace("+","");
52+
53+
if(val.length() == 0 || val.equals("-"))
54+
val="0";
55+
56+
BigInteger lvar = new BigInteger(val);
57+
58+
if(lvar.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0)
59+
lvar = BigInteger.valueOf(Integer.MAX_VALUE);
60+
else if(lvar.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0)
61+
lvar = BigInteger.valueOf(Integer.MIN_VALUE);
62+
63+
return lvar.intValue();
64+
}
65+
}

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
165165
| 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Java](./Java/buddy-strings.java) | _O(n)_ | _O(1)_ | Easy | | |
166166
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Java](./Java/palindrome-number.java) | _O(n)_ | _O(1)_ | Easy | | |
167167
| 767 | [Reorganize String](https://leetcode.com/problems/reorganize-string/) | [Python](./Python/reorganize-string.py) | _O(n)_ | _O(n)_ | Medium | | |
168-
168+
| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [Java](./Java/string-to-integer-atoi.java) | _O(n)_ | _O(1)_ | Medium | | |
169169
<br/>
170170
<div align="right">
171171
<b><a href="#algorithms">⬆️ Back to Top</a></b>
@@ -490,7 +490,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
490490
| [Aysia](https://www.linkedin.com/in/aysiaelise/) <br> <img src="https://avatars.githubusercontent.com/u/70167431?s=460&u=1637be8636b6db6e35343ed9c1318c23e909b463&v=4" width="100" height="100"> | USA | JavaScript | [GitHub](https://github.com/aysiae) |
491491
| [Poorvi Garg](https://github.com/POORVI111) <br> <img src="https://avatars.githubusercontent.com/u/68559217?s=400&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/POORVI111) |
492492
| [Lakshmanan Meiyappan](https://laxmena.com) <br> <img src="https://avatars.githubusercontent.com/u/12819059?s=400&v=4" width="100" height="100"> | India | C++ | [Website - Blog](https://laxmena.com)<br/> [GitHub](https://github.com/laxmena) <br/> [LinekdIn](https://www.linkedin.com/in/lakshmanan-meiyappan/) |
493-
493+
| [Sachin_Upadhyay](https://github.com/sachsbu) <br> <img src="https://avatars.githubusercontent.com/u/24941685?v=4" width="100" height="100"> | India | Java | [GitHub](https://github.com/sachsbu) |
494494
<br/>
495495
<div align="right">
496496
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)
Please sign in to comment.