JAVA Regex Solution


  • 0
    X

    This Solution is not high-efficency however. Just a simple solution use regex.

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Solution {
        public int myAtoi(String str) {
            try {
                Matcher matcher = Pattern.compile("^[-+]?\\d+").matcher(str.trim());
                if(matcher.find()) {
                    str = matcher.group();
                    return Integer.parseInt(str);
                }
                else
                    return 0;
            }catch (Exception e)
            {
                return str.startsWith("-")?Integer.MIN_VALUE:Integer.MAX_VALUE;
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.