您好,登錄后才能下訂單哦!
這篇文章主要介紹C++實(shí)現(xiàn)LeetCode之最短子數(shù)組求和的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
Example:
Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
Follow up:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
這道題給定了我們一個(gè)數(shù)字,讓求子數(shù)組之和大于等于給定值的最小長(zhǎng)度,注意這里是大于等于,不是等于。跟之前那道 Maximum Subarray 有些類(lèi)似,并且題目中要求實(shí)現(xiàn) O(n) 和 O(nlgn) 兩種解法,那么先來(lái)看 O(n) 的解法,需要定義兩個(gè)指針 left 和 right,分別記錄子數(shù)組的左右的邊界位置,然后讓 right 向右移,直到子數(shù)組和大于等于給定值或者 right 達(dá)到數(shù)組末尾,此時(shí)更新最短距離,并且將 left 像右移一位,然后再 sum 中減去移去的值,然后重復(fù)上面的步驟,直到 right 到達(dá)末尾,且 left 到達(dá)臨界位置,即要么到達(dá)邊界,要么再往右移動(dòng),和就會(huì)小于給定值。代碼如下:
解法一:
// O(n) class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { if (nums.empty()) return 0; int left = 0, right = 0, sum = 0, len = nums.size(), res = len + 1; while (right < len) { while (sum < s && right < len) { sum += nums[right++]; } while (sum >= s) { res = min(res, right - left); sum -= nums[left++]; } } return res == len + 1 ? 0 : res; } };
同樣的思路,我們也可以換一種寫(xiě)法,參考代碼如下:
解法二:
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int res = INT_MAX, left = 0, sum = 0; for (int i = 0; i < nums.size(); ++i) { sum += nums[i]; while (left <= i && sum >= s) { res = min(res, i - left + 1); sum -= nums[left++]; } } return res == INT_MAX ? 0 : res; } };
下面再來(lái)看看 O(nlgn) 的解法,這個(gè)解法要用到二分查找法,思路是,建立一個(gè)比原數(shù)組長(zhǎng)一位的 sums 數(shù)組,其中 sums[i] 表示 nums 數(shù)組中 [0, i - 1] 的和,然后對(duì)于 sums 中每一個(gè)值 sums[i],用二分查找法找到子數(shù)組的右邊界位置,使該子數(shù)組之和大于 sums[i] + s,然后更新最短長(zhǎng)度的距離即可。代碼如下:
解法三:
// O(nlgn) class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int len = nums.size(), sums[len + 1] = {0}, res = len + 1; for (int i = 1; i < len + 1; ++i) sums[i] = sums[i - 1] + nums[i - 1]; for (int i = 0; i < len + 1; ++i) { int right = searchRight(i + 1, len, sums[i] + s, sums); if (right == len + 1) break; if (res > right - i) res = right - i; } return res == len + 1 ? 0 : res; } int searchRight(int left, int right, int key, int sums[]) { while (left <= right) { int mid = (left + right) / 2; if (sums[mid] >= key) right = mid - 1; else left = mid + 1; } return left; } };
我們也可以不用為二分查找法專(zhuān)門(mén)寫(xiě)一個(gè)函數(shù),直接嵌套在 for 循環(huán)中即可,參加代碼如下:
解法四:
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int res = INT_MAX, n = nums.size(); vector<int> sums(n + 1, 0); for (int i = 1; i < n + 1; ++i) sums[i] = sums[i - 1] + nums[i - 1]; for (int i = 0; i < n; ++i) { int left = i + 1, right = n, t = sums[i] + s; while (left <= right) { int mid = left + (right - left) / 2; if (sums[mid] < t) left = mid + 1; else right = mid - 1; } if (left == n + 1) break; res = min(res, left - i); } return res == INT_MAX ? 0 : res; } };
以上是“C++實(shí)現(xiàn)LeetCode之最短子數(shù)組求和的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。