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+];
vector<int> num;
for(int i = ; i < sz+; i++){
once[i] = ;
}
for(int i = ; i < sz; i++){
if(once[nums[i]] == ){
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)-] < :
num.append(abs(x))
else:
nums[abs(x)-] *= -
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++

2017/11/20 Leetcode 日记的更多相关文章

  1. 2017/11/22 Leetcode 日记

    2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...

  2. 2017/11/21 Leetcode 日记

    2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...

  3. 2017/11/13 Leetcode 日记

    2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...

  4. 2017/11/9 Leetcode 日记

    2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...

  5. 2017/11/7 Leetcode 日记

    2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...

  6. 2017/11/6 Leetcode 日记

    2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...

  7. 2017/11/5 Leetcode 日记

    2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...

  8. 2017/11/3 Leetcode 日记

    2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...

  9. 2017.11.20 基于JSP+Servlet+JavaBean实现复数运算(一)

    (7)在Servlet中使用JavaBean Servlet和JavaBean都是类,在Servlet中使用JavaBean有两种方式: 1.在一个Servlet中单独使用JavaBean 一般完成的 ...

随机推荐

  1. 分块+二分,统计对数 CDOJ

    http://acm.uestc.edu.cn/#/problem/show/1157 数列(seq) Time Limit: 3000/1000MS (Java/Others)     Memory ...

  2. 数学&动态规划:期望DP

    BZOJ3036 给定一张有向无环图,起点为1,终点为N,每个点i有ki条出边,从每个点走其中一条出边的概率是1/ki,求从1到N的期望步数 我们注意到一点,走每条边都是等概率的,那么就相当于 给定一 ...

  3. HDU6130 签到题 打表

    LINK 题意:给出一个描述自身的数列,求出第n项 思路:看了很久题目才看懂..每个值其实是描述一个分组中的个数,把两个数列对照一下就可以了,那么一个指针扫,同时向尾部加数,构造个数组就行了.其实很水 ...

  4. K-means的缺点(优化不仅仅是最小化误差)

    K-means的缺点(优化不仅仅是最小化误差) #转载时,请注明英文原作David Robinson,译者Ding Chao.# 我最近遇到一个交叉验证的问题,我认为这个给我提供了一个很好的机会去用“ ...

  5. C/C++程序员必备的15个编辑器和集成开发环境

    我们有许多的编程语言,像 Java,NET,PHP,Ruby,Perl 和 Python 等,但今天我们要讨论的是两个最古老和流行的语言的C和C++.它们都有其特殊的地方,更有效的功能和支持的工具,这 ...

  6. Kali设置代理

    原文:Kali-linux设置ProxyChains ProxyChains是Linux和其他Unices下的代理工具.它可以使任何程序通过代理上网,允许TCP和DNS通过代理隧道,支持HTTP.SO ...

  7. Double类型的数向上取整和向下取整

  8. python基础===对字符串进行左右中对齐

    例如,有一个字典如下: >>> dic = { "name": "botoo", "url": "http:// ...

  9. 64_p6

    polkit-kde-5.10.1-1.fc26.x86_64.rpm 12-Jun-2017 13:45 84854 polkit-libs-0.113-8.fc26.i686.rpm 13-Apr ...

  10. 【BubbleCup X】D. Exploration plan

    这个题首先一眼能看出二分答案…… 毕竟连可爱的边界都给你了. 下面就是怎么check 首先预处理跑一遍floyed,预处理出最短路. 用网络流判断能否达到即可. #include<bits/st ...