65. 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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- +-号要求处于第一位,则index i = 0,不是它自己等于0
- 单个字符'e'要用单引号扩起来
[二刷]:
- s = s.trim();必须要有等号,否则空格害事
- 出错的情况直接return false, 不要设置变量,否则可能会拧回来
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
上面那张表
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public boolean isNumber(String s) {
//trim first
s = s.trim();
//ini some flags
boolean numberSeen = false;
boolean pointSeen = false;
boolean eSeen = false;
//for loop: num, . , e, +-
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
numberSeen = true;
}else if (s.charAt(i) == '.') {
if (pointSeen == true || eSeen == true) {
return false;
}
pointSeen = true;
}else if (s.charAt(i) == 'e') {
if (pointSeen == true || eSeen == true || !(i >= 1 && s.charAt(i - 1) >= '0' && s.charAt(i - 1) <= '9')) {
return false;
}
eSeen = true;
numberSeen = false;
}else if (s.charAt(i) == '+' || s.charAt(i) == '-') {
if (i != 0 && s.charAt(i - 1) != 'e') return false;
numberSeen = false;
}else {
//directly return
return false;
}
}
return numberSeen;
}
}
65. Valid Number 判断字符串是不是数字的更多相关文章
- valid number 判断字符串是否为有效数字
RT,面试题,给定一个字符串判断是否为科学计数法的有效数字.此题各种繁琐考虑.今天终于学会了用有限状态机来处理.理解之后简洁易懂.在此分享我的理解推导思路,大有收获啊. 网上有解法说先记录每个状态代表 ...
- [leetcode]65. Valid Number 有效数值
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- Leetcode 65 Valid Number 字符串处理
由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...
- js 判断是不是数字||判断字符串是不是数字(正则表达式)
js使用正则表达式判断对象是不是数字,或者字符串是不是数字,或者是不是数字类型 //判断是不是一个数字 或者 一个字符串里全是数字 isNumber (value) { if (value === u ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- 【一天一道LeetCode】#65. Valid Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...
- leetCode 65.Valid Number (有效数字)
Valid Number Validate if a given string is numeric. Some examples: "0" => true " ...
- [LeetCode] 65. Valid Number 验证数字
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- Valid Number,判断是否为合法数字
问题描述: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " ...
随机推荐
- android ListView 可缩放,支持左右上下手势
public class ZoomListView extends ListView implements SwipeListener { public static enum Action { Le ...
- Restful Service 中 DateTime 在 url 中传递
在C# url 中一旦包特殊字符,请求可能就无法送达.可以使用如下方法,最为便捷. 请求端: beginTime.Value.ToString("yyyyMMddHHmmss") ...
- centos7生产环境下openssh升级
由于生产环境ssh版本太低,导致使用安全软件扫描时提示系统处于异常不安全的状态,主要原因是ssh漏洞.推荐通过升级ssh版本修复漏洞 因为是生产环境,所以有很多问题需要注意.为了保险起见,在生产环境下 ...
- dubbo 基础入门
一.什么是dubbo? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案,说白了就是个远程服务调用的分布式框架. dubbo产生的背景 ① 单一 ...
- 回顾HashMap
一.HashMap的原理简述 HashMap是基于哈希表的非线程安全的Map实现,内部采用数组+链表实现,其内部类Node定义了数据元素类型,它扩展了Map.Entry<K,V>增加了指向 ...
- Linux Capability探索实验
Linux内核从2.1版本开始,就开始支持Capabilities的安全机制.Capabilities安全机制提出的目的在于实现系统特权操作的更加细粒度的访问控制,使用户能够根据实际的安全需求来控制r ...
- The query below helps you to locate tables without a primary key:
SELECT tables.table_schema, tables.table_name, tables.table_rows FROM information_schema.tables LEFT ...
- Azure CosmosDB (1) 概述
<Windows Azure Platform 系列文章目录> Azure CosmosDB是一个全球分布式数据库服务(Global Distributed Database),提供低延迟 ...
- Windows Azure Virtual Network (13) 跨数据中心之间的虚拟网络点对点连接VNet Peering
<Windows Azure Platform 系列文章目录> 今天是大年初二,首先祝大家新年快乐,万事如意. 在笔者之前的文章中:Windows Azure Virtual Networ ...
- springboot 中的commandLineRunners接口
首先看实现了两个接口运行的顺序结果: My1: package com.example.commandlinerunner; import lombok.extern.java.Log;import ...