【leetcode刷题笔记】Valid Number

Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true



题解:题目不难,就是有点麻烦,要注意的地方很多,总结一下:

  1. 前面和后面的空格要用s.trim()去掉;
  2. 前导的‘+‘和‘-‘号需要忽略;
  3. 小数点只能出现一次,且不能出现在e后面;
  4. 指数符号e只能出现一次,而且它的前面和后面都要有数字;
  5. 除了前导的‘+‘和‘-‘外,还可以在符号e后面出现‘+‘和‘-‘,但只能在e后面。

代码如下:

 1 public class Solution {
 2     public boolean isNumber(String s) {
 3         if(s == null || s.length() == 0)
 4             return false;
 5
 6         s = s.trim();
 7         if(s.length() == 0)
 8             return false;
 9
10         int i = 0;
11         if(s.charAt(0) == ‘+‘ || s.charAt(0) == ‘-‘)
12             i++;
13
14         boolean hasDot = false;
15         boolean hasExp = false;
16         boolean num = false;
17
18         while(i<s.length()){
19             char now = s.charAt(i);
20             if(now >= ‘0‘ && now <= ‘9‘)
21             {
22                 num = true;
23             }
24             else if(now == ‘.‘){
25                 if(hasDot || hasExp)
26                     return false;
27                 hasDot = true;
28             }
29             else if(now == ‘e‘){
30                 if(hasExp || num == false)
31                     return false;
32                 hasExp = true;
33                 num = false;
34             }
35             else if(now == ‘+‘ || now == ‘-‘){
36                 if(s.charAt(i-1) != ‘e‘)
37                     return false;
38             }
39             else
40                 return false;
41             i++;
42         }
43         return num;
44     }
45 }

【leetcode刷题笔记】Valid Number

时间: 2024-11-15 10:04:10

【leetcode刷题笔记】Valid Number的相关文章

【leetcode刷题笔记】Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

【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