zoukankan      html  css  js  c++  java
  • 2017/11/20 Leetcode 日记

    2017/11/14 Leetcode 日记

    442. Find All Duplicates in an Array

    Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

    Find all the elements that appear twice in this array.

    Could you do it without extra space and in O(n) runtime?

    class Solution {
    public:
        vector<int> findDuplicates(vector<int>& nums) {
            int sz = nums.size();
            int once[sz+2];
            vector<int> num;
            for(int i = 0; i < sz+2; i++){
                once[i] = 0;
            }
            for(int i = 0; i < sz; i++){
                if(once[nums[i]] == 1){
                    num.push_back(nums[i]);
                }else{
                    once[nums[i]] ++;
                }
            }
            return num;
        }
    };
    c++
    class Solution:
        def findDuplicates(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            num = []
            for x in nums:
                if nums[abs(x)-1] < 0:
                    num.append(abs(x))
                else:
                    nums[abs(x)-1] *= -1
            return num
    Python3

    406. Queue Reconstruction by Height

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue.

    (先以第一序列升序,第二序列降序排列,再插入表中。)

    vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
        auto comp = [](const pair<int, int>& p1, const pair<int, int>& p2)
                        { return p1.first > p2.first || (p1.first == p2.first && p1.second < p2.second); };
        sort(people.begin(), people.end(), comp);
        vector<pair<int, int>> res;
        for (auto& p : people) 
            res.insert(res.begin() + p.second, p);
        return res;
    }
    C++
  • 相关阅读:
    iOS使用自签名证书实现HTTPS请求
    DB操作-用批处理执行Sql语句
    SSL通信-忽略证书认证错误
    oracle 19c awr 丢失 i/o信息
    this.$route.query刷新后类型改变
    wx.navigateTo在app.js中偶发性失效
    微信小程序new Date()转换日期格式时iphonex为NaN
    下载cnpm成功,cnpm -v却不识别
    element-ui的表单验证如何清除校验提示语
    5. 最长回文子串(动态规划算法)
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/7833062.html
Copyright © 2011-2022 走看看