当前位置:网站首页>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>
边栏推荐
- First time in China! The language AI strength of this Chinese enterprise is recognized as No.2 in the world! Second only to Google
- [information security laws and regulations] review
- [tpm2.0 principle and Application guide] Chapter 16, 17 and 18
- AI writes a poem
- Reuse of data validation framework Apache bval
- Jerry's headphones with the same channel are not allowed to pair [article]
- Uvalive – 4621 CAV greed + analysis "suggestions collection"
- How many are there (Lua)
- "Decryption" Huawei machine vision Corps: Huawei is moving up and the industry is moving forward
- 博睿数据入选《2022爱分析 · IT运维厂商全景报告》
猜你喜欢
Borui data was selected in the 2022 love analysis - Panoramic report of it operation and maintenance manufacturers
杰理之相同声道的耳机不允许配对【篇】
Responsibility chain model - unity
PV static creation and dynamic creation
杰理之手动配对方式【篇】
杰理之关于 TWS 声道配置【篇】
[tpm2.0 principle and Application guide] Chapter 16, 17 and 18
Seize Jay Chou
Policy mode - unity
Numpy——2. Shape of array
随机推荐
初识缓存以及ehcache初体验「建议收藏」
虚拟数字人里的生意经
Numpy——axis
Business experience in virtual digital human
The moveposition function of rigidbody2d of unity2d solves the problem of people or screen jitter when moving
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
Number - number (Lua)
杰理之按键发起配对【篇】
The top of slashdata developer tool is up to you!!!
Kirin Xin'an joins Ningxia commercial cipher Association
How many are there (Lua)
Seize Jay Chou
Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
In 2021, the national average salary was released. Have you reached the standard?
Version 2.0 of tapdata, the open source live data platform, has been released
[Base64 notes] [suggestions collection]
Redis master-slave and sentinel master-slave switchover are built step by step
Numpy——2.数组的形状
Hongmeng smart home [1.0]
Reuse of data validation framework Apache bval