Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created May 15, 2021 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/d9609e0497ce58cb323d138e53d5f3e9 to your computer and use it in GitHub Desktop.
Save vrat28/d9609e0497ce58cb323d138e53d5f3e9 to your computer and use it in GitHub Desktop.
Valid Number
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