Question :
Validate if a given string is numeric.
Some examples:
"0"
=>true
" 0.1 "
=>true
"abc"
=>false
"1 a"
=>false
"2e10"
=>true
Note:It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
Anwser 1 :
class Solution {
public:
bool isNumber(const char *s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (s == NULL)
return false;
while(isspace(*s))
s++;
if (*s == '+' || *s == '-')
s++;
bool eAppear = false;
bool dotAppear = false;
bool firstPart = false;
bool secondPart = false;
bool spaceAppear = false;
while(*s != '\0')
{
if (*s == '.')
{
if (dotAppear || eAppear || spaceAppear)
return false;
else
dotAppear = true;
}
else if (*s == 'e' || *s == 'E')
{
if (eAppear || !firstPart || spaceAppear)
return false;
else
eAppear = true;
}
else if (isdigit(*s))
{
if (spaceAppear)
return false;
if (!eAppear)
firstPart = true;
else
secondPart = true;
}
else if (*s == '+' || *s == '-') // behind of e/E
{
if (spaceAppear)
return false;
if (!eAppear || !(*(s-1) == 'e' || *(s-1) == 'E'))
return false;
}
else if (isspace(*s))
spaceAppear = true;
else
return false;
s++;
}
if (!firstPart) {
return false;
} else if (eAppear && !secondPart) {
return false;
}
return true;
}
};
Anwser 2 :
class Solution {
public:
bool isNumber(const char *s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int mat[11][7] = {
0 ,0 ,0 ,0 ,0 ,0 ,0, // false
0 ,2 ,3 ,0 ,1 ,4 ,0, // 1
0 ,2 ,5 ,6 ,9 ,0 ,10, // 2
0 ,5 ,0 ,0 ,0 ,0 ,0, // 3
0 ,2 ,3 ,0 ,0 ,0 ,0, // 4
0 ,5 ,0 ,6 ,9 ,0 ,10, // 5
0 ,7 ,0 ,0 ,0 ,8 ,0, // 6
0 ,7 ,0 ,0 ,9 ,0 ,10, // 7
0 ,7 ,0 ,0 ,0 ,0 ,0, // 8
0 ,0 ,0 ,0 ,9 ,0 ,10, // 9
10,10,10,10,10,10,10 // 10
};
int i = 0;
int stat = 1;
while(s[i] != '\0')
{
int type = 0;
if(s[i] >= '0' && s[i] <= '9')
type = 1;
else if(s[i] == '.')
type = 2;
else if(s[i] == 'e')
type = 3;
else if(s[i] == ' ')
type = 4;
else if(s[i] == '+' || s[i] == '-')
type = 5;
if(stat == 0)
return false;
stat = mat[stat][type];
i++;
}
stat = mat[stat][6];
if(stat == 10) {
return true;
}
return false;
}
};
参考推荐:
Valid Number
分享到:
相关推荐
java java_leetcode题解之Number of Valid Words for Each Puzzle.java
javascript js_leetcode题解之65-valid-number.js
leetcode最难LeetCode_ValidNumber 因为我疯了,所以我去了 LeetCode,并按 Hardest:LeastSolved 排序。 没有汗! 语言:C# 我学到的是 这个很疯狂,因为它在定义实际可以算作数字方面确实做得很差。 如果这是面对面...
260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [C++](./C++/single-number-iii.cpp) [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium || 268| [Missing ...
3. **字符串处理**:Java中的String类是常考主题,如"Valid Palindrome"要求判断一个字符串是否为回文,这涉及字符串的反转和比较操作。 4. **排序与查找**:快速排序、归并排序、二分查找等经典算法经常出现在...
根据提供的文件信息,我们能提炼出一系列IT相关知识点,主要是围绕LeetCode这本电子书的主题——即编程面试题目解答。此电子书内容丰富,涵盖了算法和数据结构中常见的问题及其解决方案,非常适合准备技术面试的读者...
* 回文数(Palindrome Number):判断数字是否为回文数。 3. 数学运算: * 两个排序数组的中位数(Median of Two Sorted Arrays):计算两个排序数组的中位数。 * 整数到罗马(Integer to Roman):将整数转换为...
第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most ...
- Valid Parentheses:这是一个用来检测字符串中括号是否有效匹配的问题,可以用栈(Stack)数据结构来解决,属于基础的算法题目。 - Two Sum II - Input array is sorted:与前一个Two Sum题目的不同之处在于,本题...
力码解决方案 Leetcode是一个网站,人们——主要是软件工程师——练习他们的编码技能。 有 800 多个问题(并且还在不断增加),每个问题都有多个解决方案。 问题按难度等级排列:简单、中等和...├── Valid Number │
leetcode 分类C++ Boost 演示 观察如何用 . 还要观察标题模板如何“爆炸”出来,即。 我包含了 2 个头文件,并且有一大堆头文件由bcp提取用于静态编译。 OSX 构建说明 通过安装 Boost 和 CMake brew install boost ...
20. Valid Parentheses:判断给定的字符串是否是有效的括号字符串。 LeetCode的答案大全中整理的这些问题,包含了算法面试中经常考察的高频知识点,如数组操作、字符串处理、链表处理、数学问题、回溯算法、动态...
- **验证回文字符串(Valid Palindrome)**: 判断一个字符串是否是回文。 - **最长回文子串(Longest Palindromic Substring)**: 找出字符串中最长的回文子串。 - **通配符匹配(Wildcard Matching)**: 实现通配符‘*’...
number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to integer ,easy , 模拟 count and say , easy , 模拟 Anagrams 字符串处理,map Simplify Path,字符串处理,stack Length of Last Word,字符串...
java lru leetcode :ice_cream: LeetCode ...Valid Parentheses 26 Remove Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73
字符串处理题目也是LeetCode的重要部分,例如"Reverse Words in a String"(翻转字符串中的单词)和"Valid Palindrome"(验证回文串)。这些题目通常涉及到字符串的遍历、分割、比较以及特殊字符的处理。 四、位...
number of nodes along the shortest path from the root node down to the nearest leaf node. 解题思路: 思路一:深度优先遍历,递归遍历左右子树,遇到叶子节点时返回 1,返回过程中,比较左右子树,较小深度...
Number 回文数 11 Container With Most Water 盛最多水的容器 13 Roman to Integer 罗马数字转整数 14 Longest Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from Sorted ...
1. **Valid Anagram**:检查两个字符串是否为彼此的字母异位词。这通常通过计算每个字符串的字符频率并比较它们是否相同来解决,可以使用Python的字典数据结构。 2. **Delete Node in a Linked List**:在链表中...