零 题目:算法(leetode,附思维导图 + 全副解法)300题之(8)字符串转换整数 (atoi)
一 题目形容
二 解法总览(思维导图)
.png)
三 全副解法
1 计划1
1)代码:
// 计划1 var myAtoi = function(s) { const l = s.length, numStrArr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; let index = 0, // 正、负 状况 sign = undefined, // 后果字符串 resStr = ''; // 1)一直去掉后面的 空格字符 while (index < l && s[index] === ' ') { index++; } // 2)去完后面的空格字符后,前面的第一个字符必须是 "+"、"-" 或 数值字符 // 不是的话间接返回 0 if (index < l) { if (s[index] === '-' || s[index] === '+' ) { sign = s[index]; resStr += sign; } else { if (numStrArr.includes(s[index])) { resStr += s[index]; } else { return 0; } } } // 3)+、- 号确定后,一直往后读取数值字符(若是遇到 非数值字符 就没必要往下读了) 并 一直存入resStr index += 1; while (index < l && numStrArr.includes(s[index])) { resStr += s[index]; index++; } let resValue = parseInt(resStr); // 边界1:"+-12" (外围:只有 +、- 字符等,此时 parseInt(resStr) 为 NaN,即Not A Number) resValue = Number.isNaN(resValue) ? 0 : resValue; // 边界2:范畴的上下界解决 resValue = resValue < Math.pow(-2, 31) ? Math.pow(-2, 31) : resValue; resValue = resValue > Math.pow(2, 31) - 1 ? Math.pow(2, 31) - 1 : resValue; // 4)返回最终的后果 return resValue;}
2 计划2
1)代码:
// 计划2 计划1的”优化版“,其实没必要进行 去后面空格字符 等操作,间接应用 JS自带的 parseInt()var myAtoi = function(str) { // 1)间接应用 parseInt() ,其帮咱们少了不少“前置解决工作” let resValue = parseInt(str); // 2)边界解决 // 边界1:"+-12" (外围:只有 +、- 字符等,此时 parseInt(resStr) 为 NaN,即Not A Number) if (isNaN(resValue)) { return 0; } else { // 边界2:范畴的上下界解决 resValue = resValue < Math.pow(-2, 31) ? Math.pow(-2, 31) : resValue; resValue = resValue > Math.pow(2, 31) - 1 ? Math.pow(2, 31) - 1 : resValue; } // 3)返回最终的后果 return resValue;};
3 计划3
1)代码:
// 计划3 状态机var myAtoi = function(str) { // 依据以后 字符char,获取要变更为哪个 状态state const getStateIndex = char => { const numStrArr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; // 初始化流转值为 'end' 状态,其实也能够有别的写法 let resStateIndex = 3; if (char === ' ') { resStateIndex = 0; } else if (char === '+' || char === '-') { resStateIndex = 1; } else if (numStrArr.includes(char)){ resStateIndex = 2; } return resStateIndex; }; // 1)初始化各种值等,特地是 tableMap 的定义!! const l = str.length, // 状态机的表格模式 tableMap = { 'start': ['start', 'signed', 'in_number', 'end'], 'signed': ['end', 'end', 'in_number', 'end'], 'in_number': ['end', 'end', 'in_number', 'end'], 'end': ['end', 'end', 'end', 'end'], }; // state:以后的状态state值 let state = 'start', sign = 1, index = 0, resValue = 0; // 2)一直向后遍历 字符串str ,依据以后遍历到的字符char去不断更新 state、resValue、sign 等值, // 当 index >=l || state === 'end' 时,退出遍历 while (index < l) { const char = str[index]; state = tableMap[state][getStateIndex(char)]; if (state == 'in_number') { resValue = resValue * 10 + parseInt(char); } else if (state === 'signed') { // 因为 sign 初始化为 1,所以为 '-' 时才有必要解决 if (char === '-') { sign = -1; } } index++; // 优化:以后state为'end',就能够退出、不必再遍历 if (state === 'end') { break; } } // 3)复原符号 resValue *= sign; // 4)边界解决 // 边界1:范畴的上下界解决 resValue = resValue < Math.pow(-2, 31) ? Math.pow(-2, 31) : resValue; resValue = resValue > Math.pow(2, 31) - 1 ? Math.pow(2, 31) - 1 : resValue; // 5)返回后果 return resValue;}
四 更多
1 刷题进度
1)LeetCode:307 / 2390 。2)《剑指offer》:66 / 66 。3)相干学习材料与笔记汇总: https://github.com/CYBYOB/algorithm-leetcode/tree/master/材料%26笔记 。4)注:所有题目均有 2-5种 左右的解法,后续还将不断更新题目 & 题解。敬请期待~也欢送大家进群一起 学习、交换、刷题&拿高薪~
2 GitHub - LeetCode我的项目仓库
0)本我的项目地址: https://github.com/CYBYOB/algorithm-leetcode 。指标、愿景:让每个人都能领有肯定的算法能力、以应答面试中(会触类旁通的同学还能够将其融入本人的肌肉和血液,甚至可能赋能于公司的业务和技术)的算法。自己每周仍在一直的更新 —— 保障每周都有新的题目、题解计划刺激着您的神经 和 刷题欲望。欢送对算法感兴趣的同学退出咱们的社群。QQ群: 933919972 ;作者QQ: 1520112971 ;作者VX: c13227839870(可拉您进群、一起学习与交换~) 。
3 作者标签
1)“BAT里1名小小的伪全栈工程师,主攻前端,偶然写点后端”。2)2019年的微信小程序利用开发赛 - 全国三等奖;2019CODA较量 - 前 17/211 强 且 荣获“优良团队”名称 等。3)“半自媒体人”,在校期间、集体公众号(IT三少。新自媒体(公众号)号: 码农三少 )在半年内实现了0到5.8K+的粉丝增长等。