ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

贪心2

2020-08-28 23:02:29  阅读:226  来源: 互联网

标签:map int denominator numerator points key 贪心


两数之和,三数之和,最接近的三数之和(注意记录最接近的值并更新),四数之和(注意去除重复)。
整体思路:排序加双指针。

leetcode 49.字母异位词分组
new ArrayList<>()括号内参数是集合对象Collection,set,list都行。

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, ArrayList<String>> map = new HashMap<>();

        for (String str: strs) {
            char[] charArray = str.toCharArray();
            Arrays.sort(charArray);
            String strSorted = String.valueOf(charArray);
            if (map.containsKey(strSorted)) {
                map.get(strSorted).add(str);
            } else {
                map.put(strSorted, new ArrayList<String>(Arrays.asList(str)));
            }
        }
        List<List<String>> res = new ArrayList<>();
        return new ArrayList(map.values());

    }
}

leetcode149. 直线上最多的点数
注意点:

长度小于3
重复点
记录斜率,固定一个点,这样曲线就固定了,不用考虑截距

class Solution {
    public int maxPoints(int[][] points) {
        if (points.length <= 2) {
            return points.length;
        }
        HashMap<String, Integer> map = new HashMap<>();
        int res = 0;
        for (int i = 0; i < points.length; i++) {
            int maxLine = 0;
            int numSame = 1;
            map.put("", 0);
            for (int j = i+1; j < points.length; j++) {
                String key = "";
                int numerator = points[i][1] - points[j][1];
                int denominator = points[i][0] - points[j][0];

                if (numerator == 0 && denominator == 0) {
                    numSame ++;
                } else if (numerator == 0) {
                    key = "0_1";
                } else if (denominator == 0){
                    key = "1_0";
                } else {
                    int positive = (double)numerator/denominator > 0 ? 1:-1;
                    int factor = gcd(Math.abs(numerator), Math.abs(denominator));

                    numerator = Math.abs(numerator) / factor;
                    denominator = Math.abs(denominator) / factor;
                    key = String.valueOf(positive*numerator)+"_"+String.valueOf(denominator); 
                }
                if(key != "") {
                    map.put(key, map.getOrDefault(key, 0)+1);
                    if (map.get(key) > maxLine) {
                        maxLine = map.get(key);
                    }
                }
                if (maxLine + numSame > res) {
                    res = maxLine + numSame;
                }
            }
            map.clear();
        }
        return res;
    }

    public int gcd(int a, int b) {
        if (a == 0)
            return b;
        if (a > b) {
            return gcd(b, a);
        } else {
            return gcd(b%a, a);
        }
    }
}

标签:map,int,denominator,numerator,points,key,贪心
来源: https://www.cnblogs.com/zuotongbin/p/13580191.html

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

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

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

ICode9版权所有