当前位置:网站首页>查漏补缺(九)---程序篇
查漏补缺(九)---程序篇
2022-04-23 05:53:00 【KK要继续努力】
函数
- 题目:输入一个字符串,返回该字符串出现最多的字母
function strMax(str){
let obj={
};
for(let i=0;i<str.length;i++){
let k = str.charAt(i);
if(obj[k]){
obj[k]++;
}else{
obj[k]=1;
}
}
let a = 0;
let temp = null;
for(let j in obj){
if(obj[j]>a){
a=obj[j];
temp=j;
}
}
return temp;
}
strMax('hello world')
- 题目:编写查询函数返回数字字符串中最大的公共前缀字符串
function longStr(str){
const string = str[0];
let result = '';
if(str.length == 0){
return result;
}
for(let i=0;i<string.length;i++){
for(let j=1;j<str.length;j++){
if(f[i] !== str[j][i]){
return result;
}
}
result += f[i];
}
return result;
}
longStr(['lee','les','levis'])
- 题目:给你一个数组,请你将每个元素用它右边最大的元素替换,如果是最后一个元素,用替换。完成所有替换操作后,请你返回这个数组。
function getNewArr(arr){
let result = arr.map((item, index)=>{
if(index === arr.length-1){
return -1;
}else{
let rest = arr.slice(index+1);
let max = Math.max(...rest);
return max;
}
})
return result;
}
- 题目:对象数组去重
const hash = {
};
function arrFilter(array){
const newArr = array.reduce((prev, cur)=>{
hash[cur.fieldName] ? '' : hash[cur.fieldName] = true && prev.push(cur);
return prev;
},[])
}
reduce方法
arr.reduce((preValue, curValue, index, arr)=>{
pre:初始值,计算后的返回值
cur::当前元素
index:当前索引
arr:原数组
...
},init)
reduce方法求数组中的最大值
const value = arr.reduce((pre, cur)=>{
return (pre>cur) ? pre : cur;
})
- 莱布尼兹公式求圆周率π
function cal(n){
let sum = 0;
let item = 1;
for(let i=1;i<=n;i++){
//把每一项计算出来,然后相加
item *= i/(2*i+1);
sum += item;
}
return (sum+1)*2;
}
- 寻找100以内的质数
outer: for(let i=2;i<=100;i++){
//内层循环从2开始,如果能够整除说明不是质数
for(let j=2;j<i;j++){
if(i%j==0){
continue outer;
}
}
console.log(i);
}
- 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。假设数组中无重复元素。
//方法一
function searchValue(arr, target){
let index = 0;
for(let i=0;i<arr.length;i++){
if(arr[i] === target){
return index = i
}
}
if(!index){
arr.push(target)
arr.sort(function(a, b){
return a-b
})
}
//indexOf可以查找数组
index = arr.indexOf(target)
return index
}
//方法二
function getTargetIndex(list,target){
if(!list.includes(target)){
list.push(target)
}
list.sort((a,b) => {
return a - b
})
return list.indexOf(target)
}
版权声明
本文为[KK要继续努力]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45393020/article/details/123075443
边栏推荐
- ARM常用汇编指令
- cv_bridge 与opencv 版本不匹配的解决
- Matching between class template with default template argument and template parameter
- _findnext 报错
- v-for下定时给图片添加动画
- Makefile foundation, common functions and general makefile
- 基于Keras的时装分类案例
- [UDS unified diagnostic service] IV. typical diagnostic service (2) - data transmission function unit
- Palindromic Primes
- FOC电机库 定点PID代码分析
猜你喜欢
[UDS unified diagnostic service] II. Network layer protocol (2) - data transmission rules (single frame and multi frame)
[UDS unified diagnostic service] i. overview of diagnosis (4) - basic concepts and terms
Informatics one book pass - small ball
JS中 t, _ => 的解析
QT add qserialport class to realize serial port operation
汇编 32位无符号加法计算器
Shell脚本 &&和||的使用
基于TensorFlow的线性回归实例
Analysis and setting of dead time
Assembler 32-bit unsigned addition calculator
随机推荐
QT add qserialport class to realize serial port operation
导入文件时候 new FormData()
Interprocess communication - mutex
[learn] HF net training
Error in created hook: “ReferenceError: “Promise”未定义“
HDU-Tunnel Warfare
uniapp 自定义搜索框适配小程序对齐胶囊
[UDS] unified diagnostic service (UDS)
2020 Jiangsu Collegiate Programming Contest-A.Array
死区时间的分析与设置
基于SSD的物体检测案例实现
Detailed explanation and application principle of token
FOC SVPWM函数PWMC_SetPhaseVoltage解析
Initialization of classes and objects (constructors and destructors)
[UDS unified diagnostic service] IV. typical diagnostic service (2) - data transmission function unit
[UDS unified diagnosis service] IV. typical diagnosis service (3) - read fault information function unit (storage data transmission function unit)
Opencv uses genericindex for KNN search
Eigen 学习总结
浮点数双精度,单精度以及半精度知识总结
Notes on advanced points of C language 2