diff --git "a/content/posts/algorithm/data structure/\346\225\260\347\273\204-\345\211\215\347\274\200\345\222\214\346\225\260\347\273\204.md" "b/content/posts/algorithm/data structure/\346\225\260\347\273\204-\345\211\215\347\274\200\345\222\214\346\225\260\347\273\204.md" index a2961a3..0bbaac2 100644 --- "a/content/posts/algorithm/data structure/\346\225\260\347\273\204-\345\211\215\347\274\200\345\222\214\346\225\260\347\273\204.md" +++ "b/content/posts/algorithm/data structure/\346\225\260\347\273\204-\345\211\215\347\274\200\345\222\214\346\225\260\347\273\204.md" @@ -25,7 +25,7 @@ preSum[i] = preSum[i-1] + nums[i] preSum[r] - preSum[l-1] // 需要判断 l 为 0 的情况,直接返回 preSum[r],这种的劣势是需要判断边界条件 ``` -### 构造:[303.区域和检索-数组不可变](https://leetcode.cn/problems/range-sum-query-immutable/) +### 构造:[lc.303 区域和检索-数组不可变](https://leetcode.cn/problems/range-sum-query-immutable/) ```JavaScript /** @@ -86,7 +86,7 @@ NumArray.prototype.sumRange = function (left, right) { */ ``` -### 构造:[304. 二维区域和检索-矩阵不可变](https://leetcode.cn/problems/range-sum-query-2d-immutable/) +### 构造:[lc.304 二维区域和检索-矩阵不可变](https://leetcode.cn/problems/range-sum-query-2d-immutable/) ```js /** @@ -128,7 +128,7 @@ NumMatrix.prototype.sumRegion = function (row1, col1, row2, col2) { */ ``` -### 应用:[560.和为 k 的子数组](https://leetcode.cn/problems/subarray-sum-equals-k/) +### 应用:[lc.560 和为 k 的子数组](https://leetcode.cn/problems/subarray-sum-equals-k/) ```JavaScript /** @@ -152,7 +152,32 @@ var subarraySum = function(nums, k) { }; ``` -### 进阶: [按权重随机选择](https://leetcode.cn/problems/random-pick-with-weight/) +这样做能得到正确结果,但是并不能 AC,时间复杂度 O(n^2),不知道谁搞了个恶心的测试用例。。。会超时~ + +看了下题解,可以使用哈希表进行优化 + +```js +var subarraySum = function (nums, k) { + const mp = new Map() + mp.set(0, 1) + let count = 0, + pre = 0 + for (const x of nums) { + pre += x + if (mp.has(pre - k)) { + count += mp.get(pre - k) + } + if (mp.has(pre)) { + mp.set(pre, mp.get(pre) + 1) + } else { + mp.set(pre, 1) + } + } + return count +} +``` + +### 进阶:[lc.528 按权重随机选择](https://leetcode.cn/problems/random-pick-with-weight/) ```JavaScript /** diff --git a/tania.yaml b/tania.yaml deleted file mode 100644 index ab00b76..0000000 --- a/tania.yaml +++ /dev/null @@ -1,101 +0,0 @@ -# tania 主题的配置 -baseurl: 'https://yokiizx.site' -languageCode: 'en-us' -title: 'Yokiizx' -theme: 'hugo-tania' -paginate: 10 -copyright: 'CopyRight © 2022-{year} yokiizx. All rights reserved.' - -disqusShortname: https-hugo-tania-netlify-app - -params: - # Emoji will show before the blog title on site navbar. - # titleEmoji: "🍭" - - # Logo will show before the blog title on site navbar. - titleLogo: favicon.ico - - # Enable float footnotes. - # Default to true - enableFootnotes: true - - siteName: 'yokiizx的小破站' - siteDesc: '前端 react vue typescript JavaScript' - author: 'yokiizx' - - # 小破站统计 - busuanzi: - enable: true - - colorScheme: - # Enable toggle colorScheme - # Default to true - toggle: true - # Default colorScheme - # Default to auto - default: auto - - # Limit how many categories filter show above search input. - # Default to 5 - maxCategoryToShow: 5 - - # Show your socail information with icon on index bio with `_index.md` content. - socialOptions: - dev-to: - github: https://github.com/yokiizx - email: yokiizx@163.com - facebook: - instagram: - linkedin: - medium: - stack-overflow: - steam: - telegram: - twitter: - twitch: - whatsapp: - - # Comments settings - comments: - enabled: true - provider: giscus - - giscus: - repo: yokiizx/yokiizx.github.io - id: R_kgDOIAmvFA - category: - name: comment - id: DIC_kwDOIAmvFM4CRnAW - -menu: - header: - - name: Articles - url: '/articles/' - weight: 1 - - name: Tags - url: '/tags/' - weight: 2 - - name: Algorithm - url: '/algorithm/' - weight: 3 - - name: English - url: '/english/' - weight: 4 - # - name: About - # url: '/about' - # weight: - # footer: - # - name: RSS - # url: "/index.xml" - -markup: - highlight: - noClasses: false - lineNos: true - goldmark: - renderer: - unsafe: true - -outputs: - home: ['HTML', 'JSON'] - # home: ["HTML", "RSS", "JSON"]