3. 无重复字符的最长子串

var lengthOfLongestSubstring = function(s) {
let arr = [];
let max = 0
for (let i = 0; i < s.length; i++) {
let index = arr.indexOf(s[i])
if (index !== -1) {
arr.splice(0, index + 1)
}
arr.push(s[i])
max = Math.max(arr.length, max)
}
return max
};

6. Z 字形变换

var convert = function(s, numRows) {
if (numRows === 1) return s // 首先还是,只有一行直接返回
let arr = new Array(numRows).fill('') // 创建一个数组,存储每行
let n = numRows * 2 - 2
for (let i = 0; i < s.length; i++) {
i % n < numRows ? arr[i % n] += s[i] : arr[n - i % n] += s[i]
}
return arr.join('') // 拼接返回
};

8. 字符串转换整数 (atoi)

var myAtoi = function(str) {
let result = str.trim().match(/^[-|+]{0,1}[0-9]+/)
if (result != null) {
if (result[0] > (Math.pow(2, 31) - 1)) {
return Math.pow(2, 31) - 1
}
if (result[0] < Math.pow(-2, 31)) {
return Math.pow(-2, 31)
}
return result[0]
}
return 0
};

49. 字母异位词分组

var groupAnagrams = function(strs) {
const len = strs.length,
ans = new Map()
for (let i = 0; i < len; i++) {
let asc = strs[i].split('').sort().join()
if (ans.has(asc)) {
ans.get(asc).push(strs[i])
} else {
ans.set(asc, [strs[i]])
} }
return Array.from(ans.values())
}

179. 最大数

var largestNumber = function(nums) {
if (Number(nums.join(''))) {
return nums.map(n => String(n)).sort((a, b) => (b + a) - (a + b)).join('')
}
return '0'
};

299. 猜数字游戏

var getHint = function(secret, guess) {
let guessList = guess.split('')
let secretList = secret.split('')
let A = 0
let B = 0
for (let i = 0; i < secretList.length; i++) {
if (secretList[i] == guess[i]) {
A++
guessList[i] = '-'
secretList[i] = '+'
}
}
for (let i = 0; i < secretList.length; i++) {
let index = guessList.indexOf(secretList[i])
if (index > -1) {
B++
guessList[index] = '-'
}
}
return `${A}A${B}B`
};

524. 通过删除字母匹配到字典里最长单词

var findLongestWord = function(s, dictionary) {
let res = ''
for (let i = 0; i < dictionary.length; i++) {
if (isSubsequence(s, dictionary[i])) {
res = !res ? dictionary[i] : getRight(res, dictionary[i])
}
} function getRight(oldVal, newVal) {
if (oldVal.length > newVal.length) {
return oldVal
} else if (oldVal.length < newVal.length) {
return newVal
} else {
return oldVal < newVal ? oldVal : newVal
}
} function isSubsequence(s, t) {
let i = 0
let j = 0
while (i < s.length) {
if (s[i] == t[j]) {
j++
}
if (j == t.length) {
return true
}
i++
}
return false
}
return res
};

539. 最小时间差

var findMinDifference = function(timePoints) {
if (timePoints.length > 1440) return 0 // 将时间转成分钟,24H=1440min,如果参数长度超过1440,说明有重复,直接返回0 let times = []
for (let i = 0; i < timePoints.length; i++) {
let hour = Number(timePoints[i].split(':')[0])
let min = Number(timePoints[i].split(':')[1])
times[i] = hour * 60 + min
} times.sort((a, b) => a - b)
let min = 1440 - times[times.length - 1] + times[0] // 先把头尾差值当做 min
for (let i = 1; i < times.length; i++) {
min = Math.min(times[i] - times[i - 1], min)
}
return min
};

609. 在系统中查找重复文件

var findDuplicate = function(paths) {
let dicMap = {}
for (let i = 0; i < paths.length; i++) {
let dirs = paths[i].split(' ')
let root = dirs[0] + '/'
for (let j = 1; j < dirs.length; j++) {
let index = dirs[j].indexOf('(')
let txt = dirs[j].substring(0, index)
let cont = dirs[j].substring(index + 1, dirs[j].length - 1)
if (!dicMap[cont]) {
dicMap[cont] = []
}
dicMap[cont].push(root + txt)
}
}
let res = []
for (const key in dicMap) {
if (dicMap[key].length > 1) {
res.push(dicMap[key])
}
}
return res
};

648. 单词替换

var replaceWords = function(dictionary, sentence) {
let sentArr = sentence.split(' ')
for (let i = 0; i < sentArr.length; i++) {
for (let j = 0; j < dictionary.length; j++) {
if (sentArr[i].startsWith(dictionary[j])) {
sentArr[i] = dictionary[j]
}
}
}
return sentArr.join(' ')
};

leetcode中等(字符串):[3, 6, 8, 49, 179, 299, 524, 539, 609, 648]的更多相关文章

  1. Leetcode中字符串总结

    本文是个人对LeetCode中字符串类型题目的总结,纯属个人感悟,若有不妥的地方,欢迎指出. 一.有关数字 1.数转换 题Interger to roman和Roman to integer这两题是罗 ...

  2. LeetCode:字符串的排列【567】

    LeetCode:字符串的排列[567] 题目描述 给定两个字符串 s1 和 s2,写一个函数来判断 s2 是否包含 s1 的排列. 换句话说,第一个字符串的排列之一是第二个字符串的子串. 示例1: ...

  3. 前端与算法 leetcode 8. 字符串转换整数 (atoi)

    目录 # 前端与算法 leetcode 8. 字符串转换整数 (atoi) 题目描述 概要 提示 解析 解法一:正则 解法二:api 解法二:手搓一个api 算法 传入测试用例的运行结果 执行结果 G ...

  4. 前端与算法 leetcode 387. 字符串中的第一个唯一字符

    目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...

  5. LeetCode:字符串相加【415】

    LeetCode:字符串相加[415] 题目描述 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100.num1 和num2 都只 ...

  6. LeetCode 43. 字符串相乘(Multiply Strings)

    43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. ...

  7. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  8. LeetCode 中等题解(4)

    40 组合总和 II Question 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...

  9. LeetCode之“字符串”:最短回文子串

    题目链接 题目要求: Given a string S, you are allowed to convert it to a palindrome by adding characters in f ...

  10. LeetCode之“字符串”:最长回文子串

    题目要求: 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串.例如,给出字符串 "abcdzdcab",它的最长回文子串为 & ...

随机推荐

  1. java netty 实现 websocket 服务端和客户端双向通信 实现心跳和断线重连

    java netty 实现 websocket 服务端和客户端双向通信 实现心跳和断线重连 maven依赖 <dependency> <groupId>io.netty< ...

  2. 守护安全|AIRIOT城市天然气综合管理解决方案

      城市使用天然气存在安全风险和隐患,天然气管理的复杂性也比较高,依靠传统人工难以发现安全漏洞,特别是在燃气场站.管网的安全监管等方面,场站面临作业管理.区域管控等问题,管线存在第三方施工发现问题不及 ...

  3. 如何从0-1了解 熟悉 精通gitlab

    加入gitlab团队项目: 打开其他用户极狐邀请邮件: 点击接受紫色邀请按钮"accept invitation": 选择免费试用90天saas服务: 使用邮箱注册进行邮箱验证[验 ...

  4. navicat premium 15 下载和激活

    Navicat Premium 15 下载地址 链接:https://pan.baidu.com/s/1bL-M3-hkEa4M-547giVjYQ?pwd=1107 推荐安装参考地址:https:/ ...

  5. C# Afroge摄像头翻转90

    1.dll和命名空间就不在此列举了,如下只是将转换方法介绍: 第一个函数: public void Rotate90() { // 计算角度,类变量 //dAngle = dAngle + 90; / ...

  6. 使用Docker快速安装Redis

    1.使用docker命令下一个redis的镜像 docker pull redis 2.创建 redis 的 data 目录和 conf 目录 1. cd /home/fengsir/redis 2. ...

  7. 适用于linux的bilibiliB站直播间弹幕爬虫脚本

    适用于linux的bilibiliB站直播间弹幕爬虫脚本,命令行运行之,输入到命令行,部分内容参考自网络,代码底部可见原始代码出处 BUFF:然而,经测试,每次爬只能读取10条弹幕记录,这就使得在(s ...

  8. this,构造器,static,final,单例模式

    this关键字 在java中this是一个引用变量,即指向当前对象地址的引用(指针),→可以把this当作当前对象,便于更好的索引. this() 实际是调用了当前对象的构造器 1. 引用当前对象的属 ...

  9. css3颜色模式 圆角的实现 width的属性值 触发怪异盒模型

    Css颜色模式: rgb(255,0,0) rgba(255,0,0,0.5)(0.5是透明度) hsl(58%,56%)色彩饱和度 hala() border-image   url(路径) 向内偏 ...

  10. 写了一个 SRE 调试工具,类似一个小木马

    远程操作机器有时会比较麻烦,我写了一个工具,主要功能:1.远程执行命令 2.上传下载文件.是一个 Web Server,通过 HTTP 请求来操作机器,类似一个小木马.当然,因为是一个 Web Ser ...