资讯专栏INFORMATION COLUMN

[LeetCode] 65. Valid Number

SoapEye / 2865人阅读

Problem

Validate if a given string can be interpreted as a decimal number.

Some examples:

</>复制代码

  1. "0" => true
  2. " 0.1 " => true
  3. "abc" => false
  4. "1 a" => false
  5. "2e10" => true
  6. " -90e3 " => true
  7. " 1e" => false
  8. "e3" => false
  9. " 6e-1" => true
  10. " 99e2.5 " => false
  11. "53.5e93" => true
  12. " --6 " => false
  13. "-+3" => false
  14. "95a54e53" => false

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:

</>复制代码

  1. Numbers 0-9
  2. Exponent - "e"
  3. Positive/negative sign - "+"/"-"
  4. Decimal point - "."

Of course, the context of these characters also matters in the input.

Solution

</>复制代码

  1. class Solution {
  2. public boolean isNumber(String s) {
  3. if (s == null || s.length() == 0) return false;
  4. s = s.trim();
  5. int len = s.length();
  6. if (len == 0) return false;
  7. int signCount = 0;
  8. boolean hasE = false;
  9. boolean hasNum = false;
  10. boolean hasPoint = false;
  11. for (int i = 0; i < len; i++) {
  12. char ch = s.charAt(i);
  13. if (ch >= "0" && ch <= "9") {
  14. hasNum = true;
  15. } else if (ch == "e" || ch == "E") {
  16. if (hasE || !hasNum || i == len-1) return false;
  17. hasE = true;
  18. } else if (ch == ".") {
  19. if (hasPoint || hasE) return false;
  20. //0. is true
  21. if (i == len-1 && !hasNum) return false;
  22. hasPoint = true;
  23. } else if (ch == "+" || ch == "-") {
  24. //two signs at most; should not appear in the end
  25. if (signCount == 2 || i == len-1) return false;
  26. //sign appears in the middle but no E/e
  27. if (i > 0 && !hasE) return false;
  28. signCount++;
  29. } else return false;
  30. }
  31. return true;
  32. }
  33. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72364.html

相关文章

  • leetcode65 valid number 正则表达式的运用

    摘要:完整的正则表达式为。代码如下翻译如果我们看到数字,就将设为如果看到小数点,则判断是否已有小数点或是,因为后只能有整数只能遇到一次,如果第一次遇到但是没有遇到数字,则返回错误。 题目要求 Validate if a given string is numeric. Some examples: 0 => true 0.1 => true abc => false 1 a => fa...

    DobbyKim 评论0 收藏0
  • LeetCode65. Valid Number -- 判断合法数字

    摘要:描述分析该题的说明比较模糊,所以需要慢慢进行尝试来弄清楚哪些是合法的数字。代码去除前后的空格小数点前面不能出现和小数点前面不能出现,并且需要有数字保证后面也有数字符号只能再位和后面一位 描述 Validate if a given string is numeric. Some examples:0 => true 0.1 => trueabc => false1 a => fals...

    jindong 评论0 收藏0
  • leetcode部分题目答案之JavaScript版

    摘要:自己没事刷的一些的题目,若有更好的解法,希望能够一起探讨项目地址 自己没事刷的一些LeetCode的题目,若有更好的解法,希望能够一起探讨 Number Problem Solution Difficulty 204 Count Primes JavaScript Easy 202 Happy Number JavaScript Easy 190 Reverse Bi...

    alphahans 评论0 收藏0
  • [LeetCode] 611. Valid Triangle Number

    Problem Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.Examp...

    anonymoussf 评论0 收藏0
  • LeetCode 306. Additive Number

    摘要:描述累加数是一个字符串,组成它的数字可以形成累加序列。一个有效的累加序列必须至少包含个数。说明累加序列里的数不会以开头,所以不会出现或者的情况。示例输入输出解释累加序列为。 LeetCode 306. Additive Number Description Additive number is a string whose digits can form additive sequen...

    GeekQiaQia 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<