W: Failed to fetch http://repo.mysql.com/apt/ubuntu/dists/bionic/InRelease The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 467B942D3A79BD29 W: Some index files failed to download. They have been ignored, or old ones used instead.
function maxSubarray(nums) {
if (!nums || nums.length === 0) {
return 0;
}
let maxSum = nums[0];
let curSum = nums[0];
for (let i = 1; i < nums.length; i++) {
curSum = Math.max(nums[i], curSum + nums[i]);//要不要包含前面的
maxSum = Math.max(maxSum, curSum);//要不要包含自己
}
return maxSum;
}
function solution(S) {
var data = S.split("")
var pair = {'[':']','{':'}','(':')'}
var queue = []
for (var i = 0; i < data.length; i++) {
var start = data[i].match(/[\{\(\[]/)
if(start != null){
queue.push(pair[start.input])
}else {
var end = queue.pop()
if(end != data[i]){
return 0
}
}
}
return 1
}
這種簡單的題目真的就是陷阱多,這應該是也要考慮到})]為開頭的狀況,要過濾掉這種CASE
function solution(S) {
//新增檢查邏輯
if(S.length % 2 == 1 || S.match(/^[\}\)\]]/) != null){
return 0
}
var data = S.split("")
var pair = {'[':']','{':'}','(':')'}
var queue = []
for (var i = 0; i < data.length; i++) {
var start = data[i].match(/[\{\(\[]/)
if(start != null){
queue.push(pair[start.input])
}else {
var end = queue.pop()
if(end != data[i]){
return 0
}
}
}
//一定要全部都有成對
if(queue.length > 0){
return 0
}
return 1
}