Skip to content

Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function) #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions Java/string-to-integer-atoi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.bst.myexamples;

/**
* Solution accepted on Leetcode with 7ms runtime and 39.2MB memory
*
* 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).
* Approach
* 1.Prepare String that is having only +- sign and number value
* Convert to BigInteger to compare value with 32bit signed Integer range and clamp if goes out of range
* return output with 0 if no string number found or non number value found before any number value
*/

import java.math.*;
class StringToIntegerATOI {

public static void main(String[] args){
/* StringToIntegerATOI sta = new StringToIntegerATOI();
System.out.println(sta.myAtoi("-20000000000000"));
*/

}
public int myAtoi(String s) {

StringBuilder sb = new StringBuilder();

for(int i=0; i<s.length(); i++){

if(sb.length() == 0){
if(Character.isWhitespace(s.charAt(i)))
continue;

if('-' == s.charAt(i)){
sb.append("-");
continue;
}

if('+' == s.charAt(i)){
sb.append("+");
continue;
}
}

if(Character.isDigit(s.charAt(i)))
sb.append(s.charAt(i));
else
break;

}

String val = sb.toString().replace("+","");

if(val.length() == 0 || val.equals("-"))
val="0";

BigInteger lvar = new BigInteger(val);

if(lvar.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0)
lvar = BigInteger.valueOf(Integer.MAX_VALUE);
else if(lvar.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0)
lvar = BigInteger.valueOf(Integer.MIN_VALUE);

return lvar.intValue();
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Java](./Java/buddy-strings.java) | _O(n)_ | _O(1)_ | Easy | | |
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Java](./Java/palindrome-number.java) | _O(n)_ | _O(1)_ | Easy | | |
| 767 | [Reorganize String](https://leetcode.com/problems/reorganize-string/) | [Python](./Python/reorganize-string.py) | _O(n)_ | _O(n)_ | Medium | | |

| 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 | | |
<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
Expand Down Expand Up @@ -490,7 +490,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
| [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) |
| [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) |
| [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/) |

| [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) |
<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
Expand Down