Why e returns positive in leetcode when it should return false??


  • 0
    F

    In my local machine if the input is e it returns false, but in leetcode returns true. I can't find my error. Anyone please?

    public class Solution {
            public boolean isNumber(String s) {
                 if(s == null){
        			 return false;
        		 }
        		 s = s.toLowerCase();
        		 if(s == "e" || s == "." || s == "/"){
        			 return false;
        		 }
        		 
        		char[] myObject = new char[]{'0','1','2','3','4','5','6','7','8','9','.','/','e'};
        		 char[] myChar = s.toCharArray();
        		 
        		 for (int i = 0; i < myChar.length; i++) {
        			 
        				 int temp = 0;
        				while(myChar[i] != myObject[temp])
        				{
        					temp++;
        					if(temp == myObject.length){
        						return false;
        					}
        				}
        		}
        		 
        		 return true;
            }
        }

  • 1
    Z

    I guess you use isNumber("e") , so you get false.
    Look the the code you write

         if(s == "e" || s == "." || s == "/"){
             return false;
         }
    

    You should rewrite to

        s.equals("e")
    

    To avoid the problem.

    To compare string, use equals not ==.
    == is to compare the ref's equality.

    I guess leetcode's test data is load from file, so the ref are not same.


Log in to reply
 

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