ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

从一个无序,不相等的数组中,选取N个数,使其和为M实现算法

2020-09-23 03:32:08  阅读:306  来源: 互联网

标签:nums sum 个数 无序 while high 算法 let low


// 递归分解,最后转换成求2数之和

//  一个方法从 2Sum 秒杀到 100Sum
//  https://leetcode-cn.com/problems/3sum/solution/yi-ge-fang-fa-tuan-mie-by-labuladong/

var nSumTarget = function (nums, n, start, target) {
    let res = []
    if (n < 2 || n > nums.length) {
        return res
    }
    if (n == 2) {
        let low = start;
        let high = nums.length - 1;
        while (low < high) {
            let sum = nums[low] + nums[high];
            let left = nums[low]
            let right = nums[high]
            if (sum < target) {
                while (low < high && nums[low] === left) {
                    low++
                }
            } else if (sum > target) {
                while (low < high && nums[high] === right) {
                    high--
                }
            } else {
                res.push([left, right])
                while (low < high && nums[low] === left) {
                    low++
                }
                while (low < high && nums[high] === right) {
                    high--
                }
            }
        }
    } else {
        for (let i = start; i < nums.length; i++) {
            let sub = nSumTarget(nums, n - 1, i + 1, target - nums[i])
            for (let arr of sub) {
                arr.push(nums[i])
                res.push(arr)
            }
            while (i < nums.length && nums[i] === nums[i + 1]) {
                i++
            }
        }
    }
    return res
}
var findGroup = function (nums, n, sum) {
    nums = nums.sort((a, b) => a - b)
    return nSumTarget(nums, n, 0, sum)
};
// 位运算
// https://blog.csdn.net/weixin_34130269/article/details/91382220
const search = (arr, count, sum) => {
    // 计算某选择情况下有几个 `1`,也就是选择元素的个数
    const n = num => {
        let count = 0
        while (num) {
            num &= (num - 1)
            count++
        }
        return count
    }

    let len = arr.length, bit = 1 << len, res = []

    // 遍历所有的选择情况
    for (let i = 1; i < bit; i++) {
        // 满足选择的元素个数 === count
        if (n(i) === count) {
            let s = 0, temp = []

            // 每一种满足个数为 N 的选择情况下,继续判断是否满足 和为 M
            for (let j = 0; j < len; j++) {
                // 建立映射,找出选择位上的元素
                if ((i & 1 << j) !== 0) {
                    s += arr[j]
                    temp.push(arr[j])

                    // 左移,从右边的index往左边看
                   // s += arr[len -1 - j]
                   // temp.push(arr[len -1 - j])
                }
            }

            // 如果这种选择情况满足和为 M
            if (s === sum) {
                res.push(temp)
            }
        }
    }

    return res
}

标签:nums,sum,个数,无序,while,high,算法,let,low
来源: https://www.cnblogs.com/fazero/p/13715888.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有