成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 548. Split Array with Equal Sum

frank_fun / 2031人閱讀

Problem

Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions:

0 < i, i + 1 < j, j + 1 < k < n - 1
Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be equal.
where we define that subarray (L, R) represents a slice of the original array starting from the element indexed L to the element indexed R.
Example:
Input: [1,2,1,2,1,2,1]
Output: True
Explanation:
i = 1, j = 3, k = 5.
sum(0, i - 1) = sum(0, 0) = 1
sum(i + 1, j - 1) = sum(2, 2) = 1
sum(j + 1, k - 1) = sum(4, 4) = 1
sum(k + 1, n - 1) = sum(6, 6) = 1
Note:
1 <= n <= 2000.
Elements in the given array will be in range [-1,000,000, 1,000,000].

Solution
class Solution {
    public boolean splitArray(int[] nums) {
        if (nums == null || nums.length < 7) return false;
        int len = nums.length;
        int[] sum = new int[len];
        sum[0] = nums[0];
        for (int i = 1; i < len; i++) {
            sum[i] = sum[i-1]+nums[i];
        }
        // 0 ~ i-1  |  i+1 ~ mid-1  |  mid+1 ~ k-1  |  k+1 ~ len-1
        for (int mid = 3; mid < len-3; mid++) {
            Set set = new HashSet<>();
            for (int i = 1; i <= mid-2; i++) {
                //save quarter sum into hashset
                if (sum[i-1] == sum[mid-1]-sum[i]) set.add(sum[i-1]);
            }
            for (int k = mid+2; k <= len-2; k++) {
                if (sum[len-1]-sum[k] == sum[k-1]-sum[mid]) {
                    int quarterSum = sum[len-1]-sum[k];
                    if (set.contains(quarterSum)) return true;
                }
            }
        }
        return false;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72407.html

相關(guān)文章

  • 【譯】45種Javascript技巧大全

    摘要:對進(jìn)行序列化和反序列化避免使用和構(gòu)造函數(shù)使用和構(gòu)造函數(shù)是非常昂貴的操作,因?yàn)槊看嗡麄兌紩?huì)調(diào)用腳本引擎將源代碼轉(zhuǎn)換成可執(zhí)行代碼。 原文:45 Useful JavaScript Tips, Tricks and Best Practices 譯文:45個(gè)有用的JavaScript技巧,竅門和最佳實(shí)踐 譯者:dwqs 在這篇文章中,我將分享一些JavaScript常用的技巧,竅門和最...

    hufeng 評論0 收藏0
  • 45 個(gè)實(shí)用的 JavaScript 技巧、竅門和最佳實(shí)踐

    摘要:使用閉包實(shí)現(xiàn)私有變量譯者添加未在構(gòu)造函數(shù)中初始化的屬性在語句結(jié)尾處使用分號(hào)在語句結(jié)尾處使用分號(hào)是一個(gè)很好的實(shí)踐。總結(jié)我知道還有很多其他的技巧,竅門和最佳實(shí)踐,所以如果你有其他想要添加或者對我分享的這些有反饋或者糾正,請?jiān)谠u論中指出。 showImg(http://segmentfault.com/img/bVbJnR); 如你所知,JavaScript是世界上第一的編程語言(編者注:2...

    魏憲會(huì) 評論0 收藏0
  • JavaScript編程注意事項(xiàng)、技巧大全

    摘要:數(shù)組元素刪除應(yīng)使用。用來序列化與反序列化結(jié)果為的值與對象相同不要使用或者函數(shù)構(gòu)造器和函數(shù)構(gòu)造器的開銷較大,每次調(diào)用,引擎都要將源代碼轉(zhuǎn)換為可執(zhí)行的代碼。 收藏自 JavaScript奇技淫巧45招 JavaScript是一個(gè)絕冠全球的編程語言,可用于Web開發(fā)、移動(dòng)應(yīng)用開發(fā)(PhoneGap、Appcelerator)、服務(wù)器端開發(fā)(Node.js和Wakanda)等等。JavaSc...

    Shimmer 評論0 收藏0
  • [LeetCode] 663. Equal Tree Partition

    Problem Given a binary tree with n nodes, your task is to check if its possible to partition the tree to two trees which have the equal sum of values after removing exactly one edge on the original tr...

    coordinate35 評論0 收藏0
  • [LeetCode] 698. Partition to K Equal Sum Subsets

    Problem Given an array of integers nums and a positive integer k, find whether its possible to divide this array into k non-empty subsets whose sums are all equal. Example 1:Input: nums = [4, 3, 2, 3,...

    kuangcaibao 評論0 收藏0

發(fā)表評論

0條評論

最新活動(dòng)
閱讀需要支付1元查看
<