18. 4Sum(35.07%)

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.

A solution set is:
(-1,  0, 0, 1)
(-2, -1, 1, 2)
(-2,  0, 0, 2)

参考链接:http://www.programcreek.com/2013/02/leetcode-4sum-java/

public class Solution {
    public List<List<Integer>> fourSum(int[] num, int target) {
        Arrays.sort(num);

        HashSet<ArrayList<Integer>> hashSet = new HashSet<ArrayList<Integer>>();
        List<List<Integer>> result = new ArrayList<>();

        for (int i = 0; i < num.length; i++) {
            for (int j = i + 1; j < num.length; j++) {
                int k = j + 1;
                int l = num.length - 1;

                while (k < l) {
                    int sum = num[i] + num[j] + num[k] + num[l];

                    if (sum > target) {
                        l--;
                    } else if (sum < target) {
                        k++;
                    } else if (sum == target) {
                        ArrayList<Integer> temp = new ArrayList<Integer>();
                        temp.add(num[i]);
                        temp.add(num[j]);
                        temp.add(num[k]);
                        temp.add(num[l]);

                        if (!hashSet.contains(temp)) {
                            hashSet.add(temp);
                            result.add(temp);
                        }

                        k++;
                        l--;
                    }
                }
            }
        }

        return result;
    }
}
根据提供的数据,我们可以计算安徽省各区域的泰尔系数(Tg)。首先,我们需要计算每个区域的常住人口数和药品检验检测资源拥有量的累计百分比。 以下是每个区域的常住人口数占安徽省总常住人口的百分比(Pi): 皖中地区:合肥市 - 15.49%,滁州市 - 6.52%,六安市 - 7.20%,安庆市 - 6.82% 皖北地区:淮北市 - 3.23%,亳州市 - 8.16%,宿州市 - 8.72%,蚌埠市 - 5.43%,阜阳市 - 13.38%,淮南市 - 4.98% 皖南地区:马鞍山市 - 3.53%,芜湖市 - 6.00%,宣城市 - 4.07%,铜陵市 - 2.14%,池州市 - 2.18%,黄山市 - 2.18% 以下是每个区域的药品检验检测资源拥有量占安徽省总的药品检验检测资源拥有量的百分比(Yi): 皖中地区:合肥市 - 0.17%,滁州市 - 0.12%,六安市 - 0.06%,安庆市 - 0.12% 皖北地区:淮北市 - 0.08%,亳州市 - 0.08%,宿州市 - 0.18%,蚌埠市 - 0.14%,阜阳市 - 0.10%,淮南市 - 0.10% 皖南地区:马鞍山市 - 0.06%,芜湖市 - 0.08%,宣城市 - 0.07%,铜陵市 - 0.12%,池州市 - 0.07%,黄山市 - 0.07% 根据以上数据,我们可以使用以下代码计算每个区域的泰尔系数(Tg): ```python import numpy as np # 示例数据:每个区域的常住人口数占安徽省总常住人口的百分比(Pi) Pi = np.array([15.49, 6.52, 7.20, 6.82, 3.23, 8.16, 8.72, 5.43, 13.38, 4.98, 3.53, 6.00, 4.07, 2.14, 2.18, 2.18]) # 示例数据:每个区域的药品检验检测资源拥有量占安徽省总的药品检验检测资源拥有量的百分比(Yi) Yi = np.array([0.17, 0.12, 0.06, 0.12, 0.08, 0.08, 0.18, 0.14, 0.10, 0.10, 0.06, 0.08, 0.07, 0.12, 0.07, 0.07]) # 计算泰尔系数 Tg = np.sum((Pi / Yi) * np.log(Pi / Yi)) print("安徽省各区域的泰尔系数(Tg):", Tg) ``` 请注意,示例代码中使用的数据仅供参考,你需要根据实际数据进行计算。如果你有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值