当前位置:网站首页>记一些笔试题
记一些笔试题
2022-06-29 08:26:00 【swag_特约男演员】
已知一个由ASCLL字符组成的字符串,查找第一个重复的字符
思路:每一个元素去与后面的元素去比较,只要相等就退出,说明已经找到第一个重复的字符了。
function getFirstSameStr (string) {
var i = 0
for (i; i < string.length; i++) {
for (var j = i + 1; j < string.length; j++) {
if (string[i] == string[j]) {
console.log('第一个重复的字符是', string[i])
return
} else {
continue
}
}
}
console.log('该字符串没有重复字符')
}
给定一个数组,数组元素只能为数字,null,undefined,NaN。求数组不重复最大子数组长度。
例如:输入[1,4,3,4,null,undefined,NaN,null] 输出 6
思路:将每一个元素全部转成字符串,避免了null和undefined的影响。null == undefined 为 true,null === undefined 为false,每个元素跟后面的元素去比较,看有没有重复的,找到重复的把后面的删除。
function maxSonArray (array) {
var i = 0
for (i; i < array.length; i++) {
// 全部转字符串便于比较(null == undefined为true,null === undefined为false)
array[i] = '' + array[i]
}
// console.log('处理后的数组', array)
var sonArray = array
i = 0
for (i; i < sonArray.length; i++) {
for (var j = i + 1; j < sonArray.length; j++) {
if (sonArray[i] === sonArray[j]) {
// 找到重复的就把后面重复的去掉
sonArray.splice(j, 1)
} else {
continue
}
}
}
console.log('最大子数组长度', sonArray.length, '最大子数组为', sonArray)
}
将函数补充完整 transform(url){ } 实现url地址参数的动态替换
例如 let href=‘https://www.baidu.com/s?user=teml2&&wd=teml’
console.log(transform(href)({user:‘123’,wd:‘aa’,qq:‘321’}))
输出:https://www.baidu.com/s?user=123&&wd=aa
思路:先找到?,?后面跟的就是参数,参数用&&链接,我们就用&&分割成数组,=左右两边就是键值对,那直接用=将键值对分割成数组,将新的参数值替换原来旧的参数值,注意题目不要新的参数就要原来的参数。所以遍历原来的参数的key去接收新的参数值,新的字段就不要了。最后重新拼接字符串。
function transform (url, param) {
// 找到?后面就是参数串
var paramsindex = url.indexOf('?')
// 截取?前面的字符串
var urlHead = url.substr(0, paramsindex + 1)
// 截取?后面的字符串
var paramsStr = url.substr(paramsindex + 1)
// 将参数串按照'&&'分割
var paramsArray = paramsStr.split('&&')
// 将参数数组变成键值对放进对象
var keyValObject = {
}
paramsArray.forEach((element) => {
let temp = element.split('=')
keyValObject[temp[0]] = temp[1]
});
// 合并参数对象
let tempKeyArray = Object.keys(keyValObject)
tempKeyArray.forEach((element) => {
keyValObject[element] = param[element]
})
// 想要把新对象的全部参数加上去可以用下一行代码
// Object.assign(keyValObject, param)
// 重新组合成url
let target = ''
let temparray = Object.entries(keyValObject)
for (let i = 0; i < temparray.length; i++) {
temparray[i] = temparray[i].join('=')
}
let parambody = temparray.join('&&')
target = urlHead + parambody
console.log(target)
}
边栏推荐
- 闭关修炼(二十)如何做好单元测试
- MT-yolov6训练及测试
- La finale de la zone de compétition Hefei de la sixième saison 2022 a été couronnée de succès.
- CDGA|交通行业做好数字化转型的核心是什么?
- mysql insert 时出现Deadlock死锁场景分析
- uniapp引入组件不生效解决方法
- mongoDB 持久化
- 打印服务IP设置方案
- 闭关修炼(二十一)Servlet生命周期、service方法源码分析、线程安全问题
- Is it really safe to open a stock account online? Find the answer
猜你喜欢

CDGA|交通行业做好数字化转型的核心是什么?

MT-yolov6训练及测试
Development tips - Image Resource Management

Memoirs of actual combat: breaking the border from webshell
![[most complete] download and installation of various versions of PS and tutorial of small test ox knife (Photoshop CS3 ~ ~ Photoshop 2022)](/img/6d/4d8d90dd221de697f4c2ab5dcc7f96.png)
[most complete] download and installation of various versions of PS and tutorial of small test ox knife (Photoshop CS3 ~ ~ Photoshop 2022)

51 MCU interrupt and timer counter, based on Puzhong technology hc6800-esv2.0

Heavyweight released "FISCO bcos application landing guide"

H5软键盘问题

2022第六季完美童模 清远赛区 海选赛圆满落幕

laravel 8 实现 订单表按月份水平分表
随机推荐
Some "non-technical" Thoughts on distributed digital identity
2022第六季完美童模 合肥賽區 决賽圓滿落幕
工厂模式
Compare homekit, MI family, and zhiting family cloud edition for what scene based experiences
Verilog reduction operator
Multiplier design (pipeline) Verilog code
P4769-[noi2018] bubble sort [combinatorics, tree array]
编程语言
Transformer details
Let you know today that the passing rate of the PMP Exam is 97%, is it believable
2022年7月系统集成项目管理工程师认证招生简章
来个小总结吧
The final of the sixth season of 2022 perfect children's model Hefei division came to a successful conclusion
Leetcode(142)——环形链表 II
P6772 [noi2020] gourmet (matrix fast power)
Open3D 最远点采样(FPS)
Unity C# 网络学习(十二)——Protobuf生成协议
积分商城运营要如何做才能获取到利润
通过ELO机制衡量各类对弈活动水平
First electric shock, so you are such a dragon lizard community | dragon lizard developer said that issue 8