当前位置:网站首页>剑指offer基础版 ---- 第26天
剑指offer基础版 ---- 第26天
2022-07-31 05:09:00 【米兰的小红黑】
class Solution {
public int strToInt(String str) {
char[] arr = str.trim().toCharArray();
if(arr.length == 0){
return 0;
}
int i = 1;
int sign = 1;
long res = 0;
int sum = Integer.MAX_VALUE;
if(arr[0] == '-'){
sign = -1;
}else if(arr[0] != '+'){
i = 0;
}
for(int j = i; j < arr.length; j++){
if(arr[j] < '0' || arr[j] > '9'){
break;
}
res = res * 10 + (arr[j] -'0');
if(sign == 1){
if(res >= sum){
return sum;
}
}else if(sign == -1){
if(res > sum){
return Integer.MIN_VALUE;
}
}
}
return (int)(sign * res);
}
}
class Solution {
public boolean isNumber(String s) {
Map<State, Map<CharType, State>> transfer = new HashMap<State, Map<CharType, State>>();
Map<CharType, State> initialMap = new HashMap<CharType, State>() {
{
put(CharType.CHAR_SPACE, State.STATE_INITIAL);
put(CharType.CHAR_NUMBER, State.STATE_INTEGER);
put(CharType.CHAR_POINT, State.STATE_POINT_WITHOUT_INT);
put(CharType.CHAR_SIGN, State.STATE_INT_SIGN);
}};
transfer.put(State.STATE_INITIAL, initialMap);
Map<CharType, State> intSignMap = new HashMap<CharType, State>() {
{
put(CharType.CHAR_NUMBER, State.STATE_INTEGER);
put(CharType.CHAR_POINT, State.STATE_POINT_WITHOUT_INT);
}};
transfer.put(State.STATE_INT_SIGN, intSignMap);
Map<CharType, State> integerMap = new HashMap<CharType, State>() {
{
put(CharType.CHAR_NUMBER, State.STATE_INTEGER);
put(CharType.CHAR_EXP, State.STATE_EXP);
put(CharType.CHAR_POINT, State.STATE_POINT);
put(CharType.CHAR_SPACE, State.STATE_END);
}};
transfer.put(State.STATE_INTEGER, integerMap);
Map<CharType, State> pointMap = new HashMap<CharType, State>() {
{
put(CharType.CHAR_NUMBER, State.STATE_FRACTION);
put(CharType.CHAR_EXP, State.STATE_EXP);
put(CharType.CHAR_SPACE, State.STATE_END);
}};
transfer.put(State.STATE_POINT, pointMap);
Map<CharType, State> pointWithoutIntMap = new HashMap<CharType, State>() {
{
put(CharType.CHAR_NUMBER, State.STATE_FRACTION);
}};
transfer.put(State.STATE_POINT_WITHOUT_INT, pointWithoutIntMap);
Map<CharType, State> fractionMap = new HashMap<CharType, State>() {
{
put(CharType.CHAR_NUMBER, State.STATE_FRACTION);
put(CharType.CHAR_EXP, State.STATE_EXP);
put(CharType.CHAR_SPACE, State.STATE_END);
}};
transfer.put(State.STATE_FRACTION, fractionMap);
Map<CharType, State> expMap = new HashMap<CharType, State>() {
{
put(CharType.CHAR_NUMBER, State.STATE_EXP_NUMBER);
put(CharType.CHAR_SIGN, State.STATE_EXP_SIGN);
}};
transfer.put(State.STATE_EXP, expMap);
Map<CharType, State> expSignMap = new HashMap<CharType, State>() {
{
put(CharType.CHAR_NUMBER, State.STATE_EXP_NUMBER);
}};
transfer.put(State.STATE_EXP_SIGN, expSignMap);
Map<CharType, State> expNumberMap = new HashMap<CharType, State>() {
{
put(CharType.CHAR_NUMBER, State.STATE_EXP_NUMBER);
put(CharType.CHAR_SPACE, State.STATE_END);
}};
transfer.put(State.STATE_EXP_NUMBER, expNumberMap);
Map<CharType, State> endMap = new HashMap<CharType, State>() {
{
put(CharType.CHAR_SPACE, State.STATE_END);
}};
transfer.put(State.STATE_END, endMap);
int length = s.length();
State state = State.STATE_INITIAL;
for (int i = 0; i < length; i++) {
CharType type = toCharType(s.charAt(i));
if (!transfer.get(state).containsKey(type)) {
return false;
} else {
state = transfer.get(state).get(type);
}
}
return state == State.STATE_INTEGER || state == State.STATE_POINT || state == State.STATE_FRACTION || state == State.STATE_EXP_NUMBER || state == State.STATE_END;
}
public CharType toCharType(char ch) {
if (ch >= '0' && ch <= '9') {
return CharType.CHAR_NUMBER;
} else if (ch == 'e' || ch == 'E') {
return CharType.CHAR_EXP;
} else if (ch == '.') {
return CharType.CHAR_POINT;
} else if (ch == '+' || ch == '-') {
return CharType.CHAR_SIGN;
} else if (ch == ' ') {
return CharType.CHAR_SPACE;
} else {
return CharType.CHAR_ILLEGAL;
}
}
enum State {
STATE_INITIAL,
STATE_INT_SIGN,
STATE_INTEGER,
STATE_POINT,
STATE_POINT_WITHOUT_INT,
STATE_FRACTION,
STATE_EXP,
STATE_EXP_SIGN,
STATE_EXP_NUMBER,
STATE_END
}
enum CharType {
CHAR_NUMBER,
CHAR_EXP,
CHAR_POINT,
CHAR_SIGN,
CHAR_SPACE,
CHAR_ILLEGAL
}
}
边栏推荐
- 关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
- CentOS7 安装MySQL 图文详细教程
- MySQL optimization slow log query
- .NET-6.WinForm2.NanUI learning and summary
- SQL row-column conversion
- SQL行列转换
- Unity Framework Design Series: How Unity Designs Network Frameworks
- 关于LocalDateTime的全局返回时间带“T“的时间格式处理
- Temporal介绍
- sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
猜你喜欢
mysql stored procedure
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
DVWA shooting range environment construction
再见了繁琐的Excel,掌握数据分析处理技术就靠它了
Linux系统安装mysql(rpm方式安装)
Mysql application cannot find my.ini file after installation
CentOS7 —— yum安装mysql
剑指offer专项突击版 --- 第 3 天
MySQL优化:从十几秒优化到三百毫秒
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
随机推荐
Temporal线上部署
MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
MySQL优化:从十几秒优化到三百毫秒
Workflow番外篇
The interviewer asked me TCP three handshake and four wave, I really
关于superset集成到自己的项目中
CentOS7 安装MySQL 图文详细教程
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
Reference code series_1. Hello World in various languages
Minesweeper game (written in c language)
C语言指针详解
剑指offer专项突击版 ---- 第2天
MySQL optimization slow log query
Mysql应用安装后找不到my.ini文件
质量小议12 -- 以测代评
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
Temporal介绍
MySQL事务隔离级别详解
What are the advantages and disadvantages of Unity shader forge and the built-in shader graph?