算法相关文档格式模版
2023/2/23小于 1 分钟
算法相关文档格式模版
题目链接
题目描述
刷题思路
代码实现
/*
* @Description:第一个只出现一次的字符
* @Version: Beta1.0
* @Author: 微信公众号:储凡
* @Date: 2021-04-28 22:23:51
* @LastEditors: 微信公众号:储凡
* @LastEditTime: 2021-04-28 22:24:20
*/
/**
* 利用indexOf和lastIndexOf角标不一致
*/
function firstNotRepeatingCharOne(str) {
const arr = str.split('')
for (let index = 0; index < arr.length; index++) {
if (arr.indexOf(arr[index]) === arr.lastIndexOf(arr[index])) {
return index
}
}
return -1
}
/**
* 数组按字母查找
*/
function firstNotRepeatingCharTwo(str) {
const len = str.length
for (let index = 0; index < len - 1; index++) {
const s = str.slice(index, index + 1)
const remainStr = `${str.slice(0, index)}${str.slice(index + 1)}`
if (!remainStr.includes(s)) {
return index
}
}
return -1
}
/**
* 使用Map结构计数
*/
function firstNotRepeatingCharThree(str) {
const resMap = new Map()
const resArr = str.split('')
// 计数操作
resArr.forEach((r) => {
if (resMap.has(r)) {
resMap.set(r, resMap.get(r) + 1)
}
else {
resMap.set(r, 1)
}
})
for (const [key, value] of resMap) {
if (value === 1) {
return str.indexOf(key)
}
}
return -1
}
console.log(firstNotRepeatingCharOne('google'))
console.log(firstNotRepeatingCharTwo('google'))
console.log(firstNotRepeatingCharThree('google'))一些建议
更新日志
2024/7/29 23:43
查看所有更新日志
5a2b2-于c0f2d-于6ff0a-于06596-于9b9e4-于b0275-于5f1e1-于02ab1-于afe76-于a23ce-于bc074-于74aa9-于
