- 验证回文字符串 Ⅱ(680)">1. 验证回文字符串 Ⅱ(680)
- 添加与搜索单词 - 数据结构设计(211)">2. 添加与搜索单词 - 数据结构设计(211)
- 字符串转换整数 (atoi)(8)">3. 字符串转换整数 (atoi)(8)
1. 验证回文字符串 Ⅱ(680)
地址:https://leetcode-cn.com/problems/valid-palindrome-ii/
双指针,如果两头的字符相等,则向中间夹,直到不等。看看区间在 [left+1, right] 或 [left, right-1] 的字符串是否回文;
var validPalindrome = function(s) {const len = s.lengthlet i = 0,j=len-1// 当左右指针均满足对称时,一起向中间前进while(i<j && s[i] === s[j]){i++j--}// 尝试判断跳过左指针元素后字符串是否回文if(validate(i+1,j)){return true}// 尝试判断跳过右指针元素后字符串是否回文if(validate(i,j-1)){return true}// 用于判断字符串是否回文function validate(st,end){while(st < end){if(s[st] !== s[end]){return false}st++;end--}return true}return false};
2. 添加与搜索单词 - 数据结构设计(211)
地址:https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/
使用一个对象存储添加的数据,如果搜索的字符串不带“.”则是正常的字符,直接搜索即可,否则正则匹配一下。
var WordDictionary = function() {this.words = {}};/*** @param {string} word* @return {void}*/WordDictionary.prototype.addWord = function(word) {if(this.words[word.length]){this.words[word.length].push(word)}else{this.words[word.length] = [word]}};/*** @param {string} word* @return {boolean}*/WordDictionary.prototype.search = function(word) {if (!this.words[word.length]) {return false}const len = word.lengthif (!word.includes('.')) {return this.words[len].includes(word)}const reg = new RegExp(word)return this.words[len].some((item) => {return reg.test(item)})};
3. 字符串转换整数 (atoi)(8)
地址:https://leetcode-cn.com/problems/string-to-integer-atoi/
不高频可以试着做
