【Leetcode】【Hard】Valid Number

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.

本题需要考虑:

1、字符前的空格

2、字符正负号

3、检查是否是数字,数字中可以包含一个‘.’小数点,数字至少应存在一位

4、略过数字和点后,检查是否有‘e’,如果有:

  (1)检查指数是否有正负

  (2)检查其后是否有数字,数字至少存在一位

5、字符后的空格

6、最后遇到‘\0‘则返回true,否则false

代码:

 1 class Solution {
 2 public:
 3     bool isNumber(string s) {
 4         int i = 0;
 5
 6         // skip the whilespaces
 7         for(; s[i] == ‘ ‘; i++) {}
 8
 9         // check the significand
10         if(s[i] == ‘+‘ || s[i] == ‘-‘) i++; // skip the sign if exist
11
12         int n_nm, n_pt;
13         for(n_nm=0, n_pt=0; (s[i]<=‘9‘ && s[i]>=‘0‘) || s[i]==‘.‘; i++)
14             s[i] == ‘.‘ ? n_pt++:n_nm++;
15         if(n_pt>1 || n_nm<1) // no more than one point, at least one digit
16             return false;
17
18         // check the exponent if exist
19         if(s[i] == ‘e‘) {
20             i++;
21             if(s[i] == ‘+‘ || s[i] == ‘-‘) i++; // skip the sign
22
23             int n_nm = 0;
24             for(; s[i]>=‘0‘ && s[i]<=‘9‘; i++, n_nm++) {}
25             if(n_nm<1)
26                 return false;
27         }
28
29         // skip the trailing whitespaces
30         for(; s[i] == ‘ ‘; i++) {}
31
32         return s[i]==0;  // must reach the ending 0 of the string
33     }
34 };
时间: 2025-01-02 04:14:24

【Leetcode】【Hard】Valid Number的相关文章

【leetcode刷题笔记】Valid Number

Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true 题解:题目不难,就是有点麻烦,要注意的地方很多,总结一下: 前面和后面的空格要用s.trim()去掉: 前导的'+'和'-'号需要忽略:

【leetcode刷题笔记】Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

【leetcode刷题笔记】Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

【leetcode刷题笔记】String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的

【leetcode刷题笔记】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【leetcode刷题笔记】Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 题解:递归.在每个空位