当前位置:网站首页>LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
2022-07-07 17:15:00 【MyDreamingCode】
1. 请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。
函数 myAtoi(string s) 的算法如下:
读入字符串并丢弃无用的前导空格
检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。
读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。
将前面步骤读入的这些数字转换为整数(即,"123" -> 123, "0032" -> 32)。如果没有读入数字,则整数为 0 。必要时更改符号(从步骤 2 开始)。
如果整数数超过 32 位有符号整数范围 [−231, 231 − 1] ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 −231 的整数应该被固定为 −231 ,大于 231 − 1 的整数应该被固定为 231 − 1 。
返回整数作为最终结果。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var s = "-91283472332";
var myAtoi = function(s) {
s = s.trim();
var sign = 1;
var index = 0;
if(s.length<0) return 0 ;
if(s[index]==='+' || s[index]==='-'){
if(s[index]==='-')
sign = -1;
index++;
}
var res = 0;
while(index<s.length){
if(s.charCodeAt(index)-'0'.charCodeAt(0)<0||s.charCodeAt(index)-'0'.charCodeAt(0)>9)
break;
else{
res = Number(res * 10) + Number(s[index]);
}
index++;
}
if((res>(Math.pow(2,31) - 1))&&sign>0)
return Math.pow(2,31) - 1;
if((res>Math.pow(2,31))&&sign<0)
return -Math.pow(2,31);
return res * sign;
};
console.log(myAtoi(s)); //-2147483648
</script>
</body>
</html>
2. 给定一个正整数 n ,输出外观数列的第 n 项。
「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。
你可以将其视作是由递归公式定义的数字字符串序列:
1. 1
2. 11
3. 21
4. 1211
5. 111221
6. 312211
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var countAndSay = function(n) {
if(n==1) return '1';
var s = countAndSay(n-1);
var num = 0;
var str = '';
var count = 0;
var flag = 0;
for(var i=0;i<s.length;i++){
if(s[i]==s[num]){
count++;
flag = 0;
}
else{
flag = 1;
str += count + s[num];
num = i;
i = num - 1;
count = 0;
}
}
if(!flag)
str+= count + s[num];
return str;
}
console.log(countAndSay(6)); //312211
</script>
</body>
</html>
3. 编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串
""
。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
strs = ["flower","flow","flight"];
var longestCommonPrefix = function(strs) {
var pre = strs[0];
var i = 1;
while(i<strs.length) {
while(strs[i].indexOf(pre)!=0)
pre = pre.slice(0,pre.length-1);
i++;
}
return pre; //fl
};
</script>
</body>
</html>
边栏推荐
- 杰理之相同声道的耳机不允许配对【篇】
- “本真”是什么意思
- 2022-07-04 matlab reads video frames and saves them
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- 99% of people don't know that privatized deployment is also a permanently free instant messaging software!
- Hutool - lightweight DB operation solution
- LeetCode 535(C#)
- Hongmeng smart home [1.0]
- Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
- 如何给“不卖笔”的晨光估值?
猜你喜欢
Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
Borui data was selected in the 2022 love analysis - Panoramic report of it operation and maintenance manufacturers
博睿数据入选《2022爱分析 · IT运维厂商全景报告》
Responsibility chain model - unity
Numpy——2.数组的形状
Jürgen Schmidhuber回顾LSTM论文等发表25周年:Long Short-Term Memory. All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversarial RL. Soccer learn
Command mode - unity
多个kubernetes集群如何实现共享同一个存储
ES6 note 1
Experiment 1 of Compilation Principle: automatic implementation of lexical analyzer (Lex lexical analysis)
随机推荐
Flipping Game(枚举)
ES6 note 1
手把手教姐姐写消息队列
Numpy——2. Shape of array
L1-028 judging prime number (Lua)
抢占周杰伦
[Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
Solve the problem of remote rviz error reporting
2022.07.02
How to implement safety practice in software development stage
how to prove compiler‘s correctness
杰理之发起对耳配对、回连、开启可发现、可连接的轮循函数【篇】
LeetCode 535(C#)
如何给“不卖笔”的晨光估值?
反爬虫的重点:识别爬虫
648. 单词替换
Desci: is decentralized science the new trend of Web3.0?
2022.07.04
Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica