Created
May 15, 2021 21:57
-
-
Save vrat28/d9609e0497ce58cb323d138e53d5f3e9 to your computer and use it in GitHub Desktop.
Valid Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
func isNumber(_ s: String) -> Bool { | |
var isNum = false | |
var isDecimal = false | |
var isExp = false | |
var arr = Array(s) | |
for (index,elem) in arr.enumerated() { | |
if elem >= "0" , elem <= "9" { | |
isNum = true | |
} | |
else if "+-".contains(elem) { | |
if index != 0 , arr[index - 1].lowercased() != "e" { | |
return false | |
} | |
} | |
else if elem == "." { | |
if isDecimal || isExp { | |
return false | |
} | |
isDecimal = true | |
} | |
else if elem.lowercased() == "e"{ | |
if isExp || isNum == false { | |
return false | |
} | |
isNum = false | |
isExp = true | |
} | |
else{ | |
return false | |
} | |
} | |
return isNum | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment