leetCode 65.Valid Number (有效数字)
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.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
思路:此题是真的有些难度的,并且最重要的是我根本就不清楚一个有效数字的规则。。
。
一个连规则都不知道的人怎么能玩好游戏呢,所以果断的挂了10次8次的。最后还是没有全然答对。
临时把别人的代码拿出来放在以下。有时间再好好研究一下:
- public class Solution {
- public boolean isNumber(String s) {
- // Start typing your Java solution below
- // DO NOT write main() function
- /*
- "0" => true
- " 0.1 " => true
- "abc" => false
- "1 a" => false
- "+-2e10" => true
- //*/
- //check input.
- if(s==null || s.length()==0) return false;
- int sz = s.length();
- int i=0;
- while(i<sz && s.charAt(i)==' ') ++i;
- boolean space = false;
- boolean exp = false;
- boolean dot = false;
- boolean number = false;
- boolean neg = false;
- for(; i<sz; i++) {
- char c = s.charAt(i);
- if(c==' ') {
- space = true;
- } else if(space==true) {
- return false;
- } else if( (c=='e' || c=='E') && exp==false && number==true) {
- exp = true;
- number = false;
- dot = true;
- neg = false;
- } else if( c=='.' && dot==false) {
- dot = true;
- neg = true;
- // number = false;
- } else if( c>='0' && c<='9') {
- number = true;
- } else if((c=='+' || c=='-') && neg==false && number==false ) {
- neg = true;
- } else {
- return false;
- }
- }
- return number;
- }
- }
leetCode 65.Valid Number (有效数字)的更多相关文章
- [leetcode]65. Valid Number 有效数值
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- [LeetCode] 65. Valid Number 验证数字
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- LeetCode 65 Valid Number
(在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...
- Leetcode 65 Valid Number 字符串处理
由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...
- [LeetCode] 65. Valid Number(多个标志位)
[思路]该题题干不是很明确,只能根据用例来理解什么样的字符串才是符合题意的,本题关键在于几个标志位的设立,将字符串分为几个部分,代码如下: class Solution { public: strin ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- 【一天一道LeetCode】#65. Valid Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...
- LeetCode Valid Number 有效数字(有限自动机)
题意:判断一个字符串是否是一个合法的数字,包括正负浮点数和整形. 思路:有限自动机可以做,画个图再写程序就可以解决啦,只是实现起来代码的长短而已. 下面取巧来解决,分情况讨论: (1)整数 (2)浮点 ...
随机推荐
- Vue.js—单元测试
Vue.js--测试 这里采用的是Vue官方工具(Vue-CLI)搭建出来的项目,在这个搭建工具中推荐的两种测试分别是 端到端的测试 E2E 单元测试 Unit Test 端到端的测试(E2E) E2 ...
- js 类型之间的相互转化
设置元素对象属性 var img = document.querySelector("img") img.setAttribute("src","ht ...
- wordcloud的安装报错 error: Microsoft Visual C++ 10.0 is required. Get it with "Microsoft Windows SDK 7.1"解决办法
cmd中使用pip install wordcloud失败,没看懂报错的原因…… 而在pycharm中添加也报错 解决方法: 1. 下载wordcloud-1.4.1.tar.gz,解压缩 cmd c ...
- keypoint && DMatch
下面单独介绍KEYPOINT 与DMatch的内在联系 std::vector<cv::Point2f> points1, points2; for (std::vector<cv: ...
- LeetCode(96) Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...
- CentOS下配置LVM和RAID
1.CentOS配置LVM http://www.cnblogs.com/mchina/p/linux-centos-logical-volume-manager-lvm.html http://ww ...
- Spring 结构
Spring框架主要由7大模块组成,它们提供了企业级开发需要的所有功能,而且每个模块都可以单独使用,也可以和其它模块组合使用,灵活且方便的部署可以使开发的程序更加简单灵活. 核心模块 Spring C ...
- [转]pickle python数据存储
python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...
- tp 路径表示
本文实例分析了thinkphp常见路径用法.分享给大家供大家参考.具体如下: 这里介绍的标签主要有: __root__ __self__ __action__ __url__ __app__ __pu ...
- Linux cp复制
复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir2.怎样才能将dir1下所有文件复制到dir2下 ...