当前位置:网站首页>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>
边栏推荐
- how to prove compiler‘s correctness
- AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
- POJ 1182: food chain (parallel search) [easy to understand]
- Version 2.0 of tapdata, the open source live data platform, has been released
- 50亿,福建又诞生一只母基金
- State mode - Unity (finite state machine)
- Kirin Xin'an joins Ningxia commercial cipher Association
- How much does it cost to develop a small program mall?
- 咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
- 解决远程rviz报错问题
猜你喜欢
2022.07.05
cmd命令进入MySQL时报服务名或者命令错误(傻瓜式教学)
2022.07.02
State mode - Unity (finite state machine)
前首富,沉迷种田
ES6 note 1
6. About JWT
Wechat web debugging 8.0.19 replace the X5 kernel with xweb, so the X5 debugging method can no longer be used. Now there is a solution
Research and practice of super-resolution technology in the field of real-time audio and video
Basic operation of chain binary tree (implemented in C language)
随机推荐
杰理之相同声道的耳机不允许配对【篇】
Tips and tricks of image segmentation summarized from 39 Kabul competitions
炒股如何开户?请问一下手机开户股票开户安全吗?
POJ 2392 Space Elevator
L1-028 judging prime number (Lua)
10 schemes to ensure interface data security
编译原理 实验一:词法分析器的自动实现(Lex词法分析)
cmd命令进入MySQL时报服务名或者命令错误(傻瓜式教学)
RISCV64
解决远程rviz报错问题
Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)
ES6 note 1
Seize Jay Chou
In 2021, the national average salary was released. Have you reached the standard?
Embedded interview questions (algorithm part)
解决rosdep的报错问题
LeetCode 648(C#)
CMD command enters MySQL times service name or command error (fool teaching)
Classification and application of enterprise MES Manufacturing Execution System
LeetCode1051(C#)