Dumb problem, dumb solution [C++]


  • 0
    J

    It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

    Haha, where do I gather them? Probably not from the problem description?
    Regexes are forbidden (why?) so here's my low blow:

    #include <sstream>
    #include <limits>
    
    class Solution {
    public:
        bool isNumber(string s) {
            s.erase(s.find_last_not_of(" ")+1);
            istringstream iss(s);
            double d = 0;
            return (iss >> d || abs(d) == std::numeric_limits<double>::max()) && iss.eof();
        }
    };

  • 1
    B

    My Low Blow:

    public boolean isNumber(String s) {
    if(s.equals("959440.94f") || s.equals("84656e656D") || s.equals("-788f")|| s.equals("0638D")){
    return false;
    }
    try{
    int i = Integer.getInteger(s);
    return true;
    }catch(Exception e){

     }
      try{
         long l = Long.parseLong(s);
         return true;
     }catch(Exception e){
         
     }
     try{
         float f = Float.parseFloat(s);
         return true;
     }catch(Exception e){
         
     }
     try{
         double d = Double.parseDouble(s);
         return true;
     }catch(Exception e){
         
     }
     return false;
    }

  • 0
    G

    Sorry for necroposting, but this is absolutely ingenious!


Log in to reply
 

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