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. 自动化发布ansible以及awx相关(持续更新)

    一 本文章只介绍ansible的基础知识以及一些组织架构,如何批量的处理等 首先一套部署发布任务在ansible里面都是以role的形式展现,并在执行命令的时候执行role入口以及主机列表 例如:an ...

  2. 查看浏览器对html5的支持情况

    http://html5test.com/   视频和音频代码检测 function CheckAudio(){ var myAudio=document.createElement("au ...

  3. mit 6.824 lab1 思路贴

    前言 为遵守 mit 的约定,这个帖子不贴太多具体的代码,主要聊聊自己在码代码时的一些想法和遇到的问题. 这个实验需要我们去实现一个 map-reduce 的功能.实质上,这个实验分为两个大的板块,m ...

  4. 一篇文章让你读懂Java异常栈信息

    一. 基本的异常打印 public class Test { public static void main(String[] args) { fun1();//第4行 } public static ...

  5. Android 13 - Media框架(5)- NuPlayerDriver

    关注公众号免费阅读全文,进入音视频开发技术分享群! 前面的章节中我们了解到上层调用setDataSource后,MediaPlayerService::Client(IMediaPlayer)会调用M ...

  6. 【Effective C++】设计与声明——成员变量和成员函数

    将成员变量声明为private 为什么成员变量不该是public? (1)从语法一致性来说,如果成员变量不是public,就需要通过成员函数访问成员变量.public接口内的每样东西都是函数的话,客户 ...

  7. nfs 加 auto 自动挂载/etc/fstab;autofs

    一,用/etc/fstab 1.在/etc/fstab里面添加一条配置文件 vim /etc/fstab #在里面添加一条配置信息 192.168.200.10:/opt/share2 /mnt/sh ...

  8. numpy基础--用于数组的文件输入输出

    以下代码的前提:import numpy as np numpy能够读写磁盘上的文本数据或二进制数据. 1 将数组以二进制格式保存到磁盘 np.save和np.load是读写磁盘数组数据的两个主要函数 ...

  9. MySQL数据库开发(1)

    数据库的概述 1 什么是数据(Data) 描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字.图片,图像.声音.语言等,数据由多种表现形式, 它们都可以经过数字化后存入计算机. 在计 ...

  10. Centos7或Ubuntu 磁盘扩容

    准备 切换到root用户(获取root权限) 安装: [root]# install lvm2 -y 查看当前信息: # 查看根分区大小 $ df -h Filesystem Size Used Av ...