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. 训练营 |【AIRIOT大学计划暑期训练营】第三期即将开营,报名从速!

    培养新生力量,聚焦产业融合.为了促进物联网产业的纵深发展和创新,推进教育链.产业链与创新链的有机衔接,提高学生理论.实践和创新能力,为行业培养更多优秀人才,航天科技控股集团股份有限公司将于2023年7 ...

  2. 利用Django实现文件上传

    一.form表单的形式上传文件 1.路由 urlpatterns = [ path("upload/", views.UploadView.as_view(),) ] 2.视图 f ...

  3. FreeRTOS例程开发

    环境配置 下载官方源码 https://www.freertos.org/ 找到这个,他就是visual studio示例demo,我们主要在这个的基础上修改 下载visio studio https ...

  4. NOIP模拟98(多校30)

    T1 构造字符串 解题思路 不算特别难的题,但是有一点细节... 首先需要并茶几缩一下点,然后判断一下是否合法,由于我们需要字典序最小的,因此我们应当保证一个联通块中标号较小的点为根节点. 那么对于所 ...

  5. flutter 打包web应用指定上下文

    使用flutter build web命令打包的应用不包含上下文,只能部署在根目录.如何指定上下文,部署在子目录下呢? 有两种办法: 1.修改web/index.html文件 修改 <base ...

  6. 填IP那个就算是接口式开发,这回随便填

    ///////////////////////////////////////////////////////////using namespace std; #include<stdlib.h ...

  7. 怎么实现鼠标移入第i个li则对应显示第i个div,默认显示第一个LI

    html 部分 <ul> <li>菜单1</li> <li>菜单2</li> <li>菜单3</li> <li ...

  8. 燕千云 YQCloud 数智化业务服务管理平台发布1.11版本

    2022年3月25日,燕千云 YQCloud 数智化业务服务管理平台发布1.11版本.新增客户服务管理模块.优化IT服务管理功能.增强燕千云与其他平台的集成能力.支持更多的业务服务场景.全面提升企业数 ...

  9. 微信支付普通商户与AppID账号关联管理-授权

    微信支付普通商户与AppID账号关联管理 二.名词解释 名词 释义 微信支付普通商户 公司企业.政府机关.事业单位.社会组织.个体工商户.个人卖家.小微商户.(微信支付商户接入指引) AppID 已通 ...

  10. Java验证集合空或验证对象空的方法

    import java.util.ArrayList; import java.util.List; import java.util.Objects; import org.springframew ...