- 浏览: 266125 次
- 性别:
文章分类
最新评论
LeetCode-Valid Number - 有限状态机
class Solution { public: bool isNumber(const char *s) { enum InputType { INVALID, // 0 SPACE, // 1 SIGN, // 2 DIGIT, // 3 DOT, // 4 EXPONENT, // 5 NUM_INPUTS // 6 }; int transitionTable[state][inputType] = { INVALID,SPACE,SIGN,DIGIT,DOT,EXPONENT -1, 0, 3, 1, 2, -1, // state0 当输入空格时,状态仍为0, 输入为符号时,状态转为3,3的转换和0是一样的,除了不能再接受符号,故在0的状态的基础上,把接受符号置为-1; 当输入为数字时,状态转为1, 状态1的转换在于无法再接受符号,可以接受空格,数字,点,指数;状态1为合法的结束状态; 当输入为点时,状态转为2,状态2必须再接受数字,接受其他均为非法; 当输入为指数时,非法; -1, 8, -1, 1, 4, 5, // state1 接受数字时仍转为状态1, 接受点时,转为状态4,可以接受空格,数字,指数,状态4为合法的结束状态, 接受指数时,转为状态5,可以接受符号,数字,不能再接受点,因为指数必须为整数,而且必须再接受数字; 【合法的结束状态】 -1, -1, -1, 4, -1, -1, // state2 接受数字转为状态4 -1, -1, -1, 1, 2, -1, // state3 和0一样,只是不能接受符号 -1, 8, -1, 4, -1, 5, // state4 接受空格,合法接受; 接受数字,仍为状态4; 接受指数,转为状态5 【合法的结束状态】 -1, -1, 6, 7, -1, -1, // state5 接受符号,转为状态6,状态6和状态5一样,只是不能再接受符号, 接受数字,转为状态7,状态7只能接受空格或数字;状态7为合法的结束状态; -1, -1, -1, 7, -1, -1, // state6 只能接受数字,转为状态7 -1, 8, -1, 7, -1, -1, // state7 接受空格,转为状态8,状态7为【合法的结束状态】 接受数字,仍为状态7; -1, 8, -1, -1, -1, -1, // state8 接受空格,转为状态8,状态8为【合法的结束状态】 }; int state = 0; while (*s != '\0') { InputType inputType = INVALID; if (isspace(*s)) inputType = SPACE; else if (*s == '+' || *s == '-') inputType = SIGN; else if (isdigit(*s)) inputType = DIGIT; else if (*s == '.') inputType = DOT; else if (*s == 'e' || *s == 'E') inputType = EXPONENT; state = transitionTable[state][inputType]; if (state == -1) return false; else ++s; } return state == 1 || state == 4 || state == 7 || state == 8; } };
发表评论
-
Job Sequencing with Deadline
2018-09-18 15:47 670Algorithm: Job-Sequencing-Wit ... -
Strassen’s Matrix Multiplication
2018-09-18 15:45 747Algorithm: Matrix-Multiplicat ... -
Binary Search
2018-09-18 15:43 502Algorithm: Binary-Search(numb ... -
Merge Sort
2018-09-18 14:54 476Algorithm: Merge-Sort (number ... -
Max-Min Problem
2018-09-18 13:00 634Algorithm: Max - Min(x, y) ... -
Shortest Paths
2018-09-17 19:16 593Algorithm: Dijkstra’s-Algorit ... -
Fractional-Knapsack
2018-09-17 18:52 510Algorithm: Greedy-Fractional- ... -
语法描述符号
2018-08-03 11:29 0* 0次或以上 + 1次或以上 ? 0次或1次 {A ... -
语法描述符号
2018-08-03 11:27 26* 0次或以上 + 1次或以上 ? 0次或1次 {A ... -
语法描述符号
2018-08-03 11:26 1* 0次或以上 + 1次或以上 ? 0次或1次 {A ... -
语法描述符号
2018-08-03 11:26 0* 0次或以上 + 1次或以上 ? 0次或1次 {A ... -
语法描述符号
2018-08-03 11:25 0* 0次或以上 + 1次或以上 ? 0次或1次 {A ... -
语法描述符号
2018-08-03 11:25 0* 0次或以上 + 1次或以上 ? 0次或1次 {A ... -
5分钟后取消订单
2018-06-01 16:44 465分钟后取消订单功能设计。 项目提到,但暂时还没做。 这里我想 ... -
N进制与10进制相互转换
2018-05-23 14:47 35public class ShareCodeUtil { ... -
RC4实现收集二例(Complete)
2018-05-22 03:51 36import java.io.UnsupportedEnc ... -
RC4实现收集二例(Simple)
2018-05-22 03:49 26public class RC4 { pub ...
相关推荐
自己编写的 非常简单和基本的 希望能够帮助到朋友们
leetcode最难LeetCode_ValidNumber 因为我疯了,所以我去了 LeetCode,并按 Hardest:LeastSolved 排序。 没有汗! 语言:C# 我学到的是 这个很疯狂,因为它在定义实际可以算作数字方面确实做得很差。 如果这是面对面...
ISVALID NUMBER(1), SYSTEMNAME VARCHAR2(50) ); -- Create/Recreate primary, unique and foreign key constraints alter table APPMENU add constraint PK_APPMENU primary key (APPMENUID);
7. `number`: "Please enter a valid number." 8. `digits`: "Please enter only digits" 9. `creditcard`: "Please enter a valid credit card number." 10. `equalTo`: "Please enter the same value again." 11. ...
Valid Number Integer to Roman Roman to Integer Count and Say Anagrams Valid Anagram Simplify Path Length of Last Word Isomorphic Strings Word Pattern 栈和队列 栈 Min Stack Valid Parentheses Longest ...
The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...
Calculating the Number of Weekdays Between Two Dates Recipe 3.6. Looking up Holidays Automatically Recipe 3.7. Fuzzy Parsing of Dates Recipe 3.8. Checking Whether Daylight Saving Time Is ...
该对象具有以下结构: { start: 0, // page number (valid number) limit: 10, // number of items to fetch, when combined with start, this is the mechanism for pagination. sort: 'name' | 'date' | 'rating'...
number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to integer ,easy , 模拟 count and say , easy , 模拟 Anagrams 字符串处理,map Simplify Path,字符串处理,stack Length of Last Word,字符串...
主要介绍了Shell中判断字符串是否为数字的6种方法分享,本文直接给出实现代码,需要的朋友可以参考下
无形的以打字稿编写的数据驱动的React形式库处理表单的方式取决于您对数据的看法。 该库采用高级typescript类型来自动...formless/core"import { validNumber } from "@react-formless/utils"export type User = { name
力码解决方案 Leetcode是一个网站,人们——主要是软件工程师——练习他们的编码技能。 有 800 多个问题(并且还在不断增加),每个问题都有多个解决方案。 问题按难度等级排列:简单、中等和...├── Valid Number │
valid number leetcode 自动机 Noah学习之旅 编程基础 Task1(8.3-8.5) 1. 数组 实现一个支持动态扩容的数组,支持增删改操作 实现两个有序数组合并为一个有序数组 学习哈希表思想,并完成leetcode上的两数之和(1)...
valid number leetcode 自动机 All Tasks Task 1:数组和链表(2天) 时间:2019-08-03 21:00 - 2019-08-05 21:00 讨论&CR时间: 2019-08-05 21:00 - 2019-08-05 22:30 ** **【数组】 实现一个支持动态扩容的数组,...
valid number leetcode 自动机 --- All Tasks Task 1:数组和链表(2天)【当前任务】 时间:2019-08-03 21:00 - 2019-08-05 21:00 讨论&CR时间: 2019-08-05 21:00 - 2019-08-05 22:30 【数组】 实现一个支持动态扩...
leetcode双人赛 Given a BT, return its mirror Implement a thread-safe ...它的value可以是一个element也可以是另外一个 ...实现一个iterator来遍历这个map...number Implement List Interface. (LC) Search a 2D sorted a
valid number leetcode 自动机 Learning With Sakura 这是什么 学习的日常进度记录 目前进度 Week1-5:学习C++并编写简单的STL 主项目 传送门: 总结 主要配合侯捷视频以及SGI STL编写。STL分为6个组件:allocator,...
valid number leetcode 自动机 前言 JiA同学的代码人生,梦想成长为一名优秀的Architect(a7t)。成长一名优秀的a7t之前,需要不断提升自己的技术广度及深度。成长过程都记录在此,方便自己归纳知识点与复习。 目录 ...
valid number leetcode 自动机 老衲的 LeetCode Easy(2021-01-02) 1. 题目地址: 描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。 题解: 2. 题目地址: 描述: 判断一个整数是否是...
Source Insight 3.5 序列号 serial number