houzz phone interview



    • Given a string representing a number, such as “9,473.75”
    • Return the numeric value for the string. In this case the result is
    • 9473.75
    • If you need more classes, simply define them inline.
    • var result = parse(“9473.75”) = 9473.75
      */

    class Solution {
    public static void main(String[] args) {
    String s = “9,473.75”;
    System.out.println(traverse(s));
    }

    public static double traverse(String s) {
    int pos = -1;
    String s1 = “”;
    for(int i = 0; i < s.length(); i++) {
    if(Character.isDigit(s.charAt(i))){
    s1 += s.charAt(i);
    }
    else if(s.charAt(i) == ‘.’) {
    pos = i;
    }
    }
    Double res = Double.parseDouble(s1);
    if(pos == -1)
    return res;
    else
    return res/Math.pow(10, s.length() - pos - 1);
    }
    }


登录后回复
 

与 BitTiger Community 的连接断开,我们正在尝试重连,请耐心等待