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

    3 Sum Closest

        public int threeSumClosest(int[] numbers ,int target) {
            // write your code here
            int minEr = Integer.MAX_VALUE;
            int ret = 0;
            Arrays.sort(numbers);
            for (int i = 1; i < numbers.length - 1; ++i) {
                int p1 = 0, p2 = numbers.length - 1;
                while (p1 < i && p2 > i) {
                    int s1 = numbers[p1] + numbers[p2] + numbers[i] - target;
                    if (Math.abs(s1) < minEr) {
                        minEr = Math.abs(s1);
                        ret = numbers[p1] + numbers[p2] + numbers[i];
                    }
                    if (s1 > 0) {
                        --p2;
                    } else if (s1 < 0) {
                        ++p1;
                    } else {
                        return target;
                    }
                }
            }
            return ret;
        }

     3 Sum

       public ArrayList<ArrayList<Integer>> threeSum(int[] numbers) {
            // write your code here
            ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
            Arrays.sort(numbers);
            ArrayList<String> mp = new ArrayList<>();
            for (int i = 1; i < numbers.length - 1; ++i) {
                int p1 = 0, p2 = numbers.length - 1;
                while (p1 < i && p2 > i) {
                    int sum = numbers[i] + numbers[p1] + numbers[p2];
                    if (sum > 0) {
                        --p2;
                    } else if (sum < 0) {
                        ++p1;
                    } else {
                        ArrayList<Integer> ad = new ArrayList<>();
                        String tag = numbers[p1] + "#" + numbers[i] + "#" + numbers[p2];
                        if (!mp.contains(tag)) {
                            ad.add(numbers[p1]);
                            ad.add(numbers[i]);
                            ad.add(numbers[p2]);
                            ret.add(ad);
                            mp.add(tag);
                        }
                        --p2;
                        ++p1;
                    }
                }
            }
            return ret;
        }

     C++:

        vector<vector<int> > threeSum(vector<int> &nums) {
            // write your code here
            vector<vector<int> > result;
            sort(nums.begin(), nums.end());
            for (int i = 0; i < nums.size(); ++i) {
                if (i > 0 && nums[i] == nums[i - 1]) {
                    continue;
                }
                int left = i + 1;
                int right = nums.size() - 1;
                while (left < right) {
                    int sum = nums[i] + nums[left] + nums[right];
                    if (sum < 0) {
                        ++left;
                    } else if (sum > 0) {
                        --right;
                    } else {
                        vector<int> triple;
                        triple.push_back(nums[i]);
                        triple.push_back(nums[left]);
                        triple.push_back(nums[right]);
                        result.push_back(triple);
                        ++left;
                        --right;
                        while (left < right && nums[left - 1] == nums[left]) {
                            ++left;
                        }
                        while (left < right && nums[right] == nums[right + 1]) {
                            ++right;
                        }
                    }
                }
            }
            return result;
        }

    Partition Array

    这地方写了一年都没写对,得去练练怎么写快排了

        int partitionArray(vector<int> &nums, int k) {
            // write your code here
            int i = 0, j = nums.size() - 1;
            while (i <= j) {
                while (i <= j && nums[i] < k) ++i;
                while (i <= j && nums[j] >= k) --j;
                if (i <= j) {
                    int tmp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = tmp;
                    ++i;
                    --j;
                }
            }
            return i;
        }
  • 相关阅读:
    父进程pid和子进程pid的大小关系
    static 和extern关键字
    linux源码下载
    tar命令
    USB开发——内核USB驱动+libusb开发方法
    microchip PIC芯片使用方法
    android下4G上网卡
    Modem常用概念
    4G上网卡NIDS拨号之Rmnet驱动
    Uboot源码解析
  • 原文地址:https://www.cnblogs.com/fripside/p/4614921.html
Copyright © 2011-2022 走看看