当前位置:网站首页>LC: string conversion integer (ATOI) + appearance sequence + longest common prefix
LC: string conversion integer (ATOI) + appearance sequence + longest common prefix
2022-07-07 19:30:00 【MyDreamingCode】
1. Please come to realize a myAtoi(string s) function , Enable it to convert a string to a 32 Bit signed integer ( similar C/C++ Medium atoi function ).
function myAtoi(string s) The algorithm is as follows :
Read in strings and discard useless leading spaces
Check the next character ( Suppose you haven't reached the end of the character yet ) Positive or negative , Read the character ( If there is ). Determine whether the final result is negative or positive . If neither exists , Suppose the result is positive .
Read in the next character , Until you reach the next non numeric character or the end of the input . The rest of the string will be ignored .
Convert the numbers read in the previous steps into integers ( namely ,"123" -> 123, "0032" -> 32). If you don't read in the numbers , Then the integer is 0 . Change the symbol if necessary ( From step 2 Start ).
If the number of integers exceeds 32 Bit signed integer range [−231, 231 − 1] , You need to truncate this integer , Keep it in this range . say concretely , Less than −231 The integer of should be fixed to −231 , Greater than 231 − 1 The integer of should be fixed to 231 − 1 .
Returns an integer as the final result .
<!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. Given a positive integer n , Output the first... Of the appearance sequence n term .
「 Look-and-say sequence 」 It's a sequence of integers , From numbers 1 Start , Each item in the sequence is a description of the previous one .
You can think of it as a sequence of numeric strings defined by a recursive formula :
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. Write a function to find the longest common prefix in the string array .
If no common prefix exists , Returns an empty string
"".
<!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>
边栏推荐
- POJ 2392 Space Elevator
- R语言dplyr包select函数、group_by函数、filter函数和do函数获取dataframe中指定因子变量中指定水平中特定数值数据列的值第三大的值
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
- 99% of people don't know that privatized deployment is also a permanently free instant messaging software!
- 【RT-Thread env 工具安装】
- SlashData开发者工具榜首等你而定!!!
- 转置卷积理论解释(输入输出大小分析)
- LeetCode 648(C#)
- Classification and application of enterprise MES Manufacturing Execution System
- testing and SQA_动态白盒測试[通俗易懂]
猜你喜欢

小试牛刀之NunJucks模板引擎

关于ssh登录时卡顿30s左右的问题调试处理

Numpy——axis

In 2021, the national average salary was released. Have you reached the standard?

Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog

爬虫实战(七):爬王者英雄图片

2022上半年朋友圈都在传的10本书,找到了
![[tpm2.0 principle and Application guide] Chapter 16, 17 and 18](/img/7a/b16549590e6445d9199c8000d47d36.png)
[tpm2.0 principle and Application guide] Chapter 16, 17 and 18

多个kubernetes集群如何实现共享同一个存储

Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
随机推荐
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
[information security laws and regulations] review
For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
最长公共前缀(leetcode题14)
L1-023 output gplt (Lua)
Seize Jay Chou
【MIME笔记】
Specify the version of OpenCV non-standard installation
脑洞从何而来?加州大学最新研究:有创造力的人神经连接会「抄近道」
what‘s the meaning of inference
2022.07.05
Former richest man, addicted to farming
Business experience in virtual digital human
ES6 note 1
how to prove compiler‘s correctness
POJ 1182 :食物链(并查集)[通俗易懂]
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
How much does it cost to develop a small program mall?
Key points of anti reptile: identifying reptiles
我的创作纪念日