-
Notifications
You must be signed in to change notification settings - Fork 20.9k
Description
Description
There is a bug in the myAtoi implementation where certain Unicode digit characters (e.g., Arabic-Indic digit '١' U+0661) pass the Character.isDigit() check but cause the parsing logic to fail.
Specifically, when the input contains these characters, the current logic identifies them as digits but fails to accumulate them into a valid numeric string, leading to an empty string being passed to Integer.parseInt(), which throws a NumberFormatException.
Steps to reproduce
Pass a string containing non-ASCII digits, such as "١" (Arabic-Indic digit 1), to the myAtoi function.
The code executes the following sequence:
-
Character.isDigit('١') returns true.
-
The logic proceeds to parse but fails to handle the non-ASCII mapping.
-
Integer.parseInt("") is eventually called (or a similar empty-string conversion).
Result: java.lang.NumberFormatException: For input string: ""
Excepted behavior
The function should follow the "parse as much as possible" rule:
-
If non-ASCII digits are not supported, they should be treated as non-digit characters.
-
The function should return 0 or the successfully parsed integer part instead of throwing an unhandled exception.
Screenshots
Additional context
No response