-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[队列 & 栈] #11
Comments
1 - 622. 设计循环队列 |
2 - 有效的括号 🔥 🔥 🔥 🔥 🔥 🔥var isValid = function(s) {
var stack = []
var obj = {
'(': ')',
'{': '}',
'[': ']'
}
for (_s of s) {
if (obj[_s]) { // 如果有匹配
stack.push(obj[_s]) // 将待匹配值放入栈中
} else {
var pop_s = stack.pop() // 将内容从栈顶pop出来
if (pop_s !== _s) return false // 不匹配说明并不相同
}
}
return stack.length === 0
}; |
3 - 1047. 删除字符串中的所有相邻重复项1 - 硬计算 var removeDuplicates = function(S) {
if (S.length === 0 || S.length === 1) return S
var A = S.split('')
var i = 0
while (i < A.length) {
// 最后一个值了, 不能继续下去了, 直接返回
if (i + 1 === A.length) return A.join('')
// 相同
if (A[i] === A[i + 1]) {
A.splice(i, 2)
i = i > 0 ? i - 1 : i
} else {
i += 1 // 继续
}
}
return A.join('')
}; 2 - 🔥 利用栈 /**
* "abbaca"
* 利用栈 stack = []
* - stack = ['a'], 出栈 'a' 和 'b' 比较, 不同, 则 再放进去 stack = ['a', 'b']
* - stack = ['a', 'b'], 出栈 'b' 和 'b' 比较, 相同, 则继续
* return stack.join('')
*/
var removeDuplicates = function(S) {
if (S.length === 0 || S.length === 1) return S
var stack = [S[0]]
for (let i = 1; i < S.length; i++) {
var prev = stack.pop(), curr = S[i]
if (prev !== curr) {
stack.push(prev)
stack.push(curr)
}
}
return stack.join('')
}; |
4 - 剑指 Offer 50. 第一个只出现一次的字符var firstUniqChar = function(s) {
var map = new Map()
for (let _s of s) {
map.set(_s, map.has(_s) ? map.get(_s) + 1: 1)
}
for ([key, val] of map) {
if (val === 1) return key
}
return ' '
}; /**
* @param {string} s
* @return {character}
*/
var firstUniqChar = function(s) {
if (s === '') return ' '
var map = new Map()
for(let i = 0; i < s.length; i++) {
if (map.has(s[i])) {
map.set(s[i], map.get(s[i]) + 1)
} else {
map.set(s[i], 1)
}
}
// [ 'l', 2 ]
for ([key, value] of map) if (value === 1) return key
return ' '
}; |
5 - 380. 常数时间插入、删除和获取随机元素/**
* Initialize your data structure here.
*/
var RandomizedSet = function() {
this.set = new Set()
};
/**
* Inserts a value to the set. Returns true if the set did not already contain the specified element.
* @param {number} val
* @return {boolean}
*/
RandomizedSet.prototype.insert = function(val) {
if (!this.set.has(val)) {
this.set.add(val)
return true
}
return false
};
/**
* Removes a value from the set. Returns true if the set contained the specified element.
* @param {number} val
* @return {boolean}
*/
RandomizedSet.prototype.remove = function(val) {
if (this.set.has(val)) {
this.set.delete(val)
return true
}
return false
};
/**
* Get a random element from the set.
* @return {number}
*/
RandomizedSet.prototype.getRandom = function() {
var index = parseInt(Math.random() * this.set.size)
return Array.from(this.set)[index]
};
/**
* Your RandomizedSet object will be instantiated and called as such:
* var obj = new RandomizedSet()
* var param_1 = obj.insert(val)
* var param_2 = obj.remove(val)
* var param_3 = obj.getRandom()
*/ |
88. 合并两个有序数组/**
* case1
* nums1 = [7,8,9,0,0,0], m = 3
* nums2 = [2,5,6], n = 3
*
* case2
* [0] 0
* [1] 1
*/
var merge = function(nums1, m, nums2, n) {
var i = m - 1, j = n - 1, tail = m + n - 1
while (j >= 0) { // nums2 用尽
if (i < 0) { // 将nums2 值赋值到nums1上去
nums1[tail--] = nums2[j--] // 本身nums1 就是空的情况, 无需理会nums1比较, 直接将nums2值附上去
continue
}
if (nums1[i] < nums2[j]) { // nums2 更大, 插入到 nums1 中
nums1[tail--] = nums2[j--]
} else { // nums1 更大, 就插入到nums1 指针位置
nums1[tail--] = nums1[i--] // 先赋值, 再--
// tail -= 1
// i -= 1
}
}
return nums1
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
增加或删除
截取或拼接
排序 / 字符串 / 值判断
The text was updated successfully, but these errors were encountered: