zoukankan      html  css  js  c++  java
  • 7-4日刷题

    Subarray Sum Closest

    1. 保存<sum[i + 1], i>,然后对sum[i + 1]排序,然后找出相邻值之间的最小差。

    2. TreeMap:ceilingEntry返回一个键-值映射关系,它与大于等于给定键的最小键关联Value。floorEntry:返回小于等于给定键的最大键关联Value。

        public ArrayList<Integer> subarraySumClosest(int[] nums) {
            // write your code here
            ArrayList<Integer> res = new ArrayList<Integer>();
            if (nums == null || nums.length == 0) {
                return res;
            }
            TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
            int sum = 0;
            int minDif = Integer.MAX_VALUE;
            res.add(0);
            res.add(0);
            for (int i = 0; i < nums.length; ++i) {
                sum += nums[i];
                Map.Entry floor = map.floorEntry(sum);
                Map.Entry ceiling = map.ceilingEntry(sum);
                if (floor != null || ceiling != null) {
                    if (floor == null || (ceiling != null && Math.abs((int) floor.getKey() - sum) > Math.abs((int) ceiling.getKey() - sum))) {
                        if (Math.abs((int) ceiling.getKey() - sum) < minDif) {
                            res.set(0, (int) ceiling.getValue() + 1);
                            res.set(1, i);
                            minDif = Math.abs((int) ceiling.getKey() - sum);
                        }
                    } else {
                        if (Math.abs((int) floor.getKey() - sum) < minDif) {
                            res.set(0, (int) floor.getValue() + 1);
                            res.set(1, i);
                            minDif = Math.abs((int) floor.getKey() - sum);
                        }
                    }
                }
                map.put(sum, i);
            }
            return res;
        }

    4 Sum

    难题是如何去重。3sum里面的判断nums[i] == nums[i - 1],已经行不通了。

        public ArrayList<ArrayList<Integer>> fourSum(int[] nums, int target) {     
            //write your code here
            ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
            Arrays.sort(nums);
            ArrayList<String> mp = new ArrayList<>();
            for (int i = 0; i < nums.length; ++i) {
                for (int j = i + 1; j < nums.length; ++j) {
                    int p1 = j + 1, p2 = nums.length - 1;
                    while (p1 < p2) {
                        int d = nums[i] + nums[j] + nums[p1] + nums[p2] - target;
                        if (d < 0) {
                            ++p1;
                        } else if (d > 0) {
                            --p2;
                        } else {
                            String tag = nums[i] + "#" + nums[j] + "#" + nums[p1] + "#" + nums[p2];
                            if (!mp.contains(tag)) {
                                ArrayList<Integer> ar = new ArrayList<>();
                                ar.add(nums[i]);
                                ar.add(nums[j]);
                                ar.add(nums[p1]);
                                ar.add(nums[p2]);
                                ret.add(ar);
                                mp.add(tag);
                            }
                            ++p1;
                            --p2;
                        }
                    }
                }
            }
            return ret;
        }
  • 相关阅读:
    UIPickerView-一.01-
    闭包-01-Swift
    Swift 入门-01-概述
    git使用命令行-01-自己操作的
    FetchedResultsController-03-CoreData相关
    SQLite-05-增删改查
    Sqlite函数总结-04
    List<string>转xml
    比较两个List<T>是否相同
    获取DataTable前几条数据
  • 原文地址:https://www.cnblogs.com/fripside/p/4621597.html
Copyright © 2011-2022 走看看