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. TCP和UDP相关概念

    位于传输层的协议,都是基于IP协议的. TCP是面向连接的.可靠的传输,UDP是无连接的.不可靠的传输.要进行TCp传输时候,需要进行三次握手,建立连接,然后才能发送数据,而且在发送过程中,有数据的确 ...

  2. ① 设计模式的艺术-07.适配器(Adapter)模式

    什么是适配器模式? 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 模式中的角色 目标接口(Target):客户所期待的接口 ...

  3. jQuery 写的简单打字游戏

    var off_x; //横坐标 var count=0; //总分 var speed=5000; //速度,默认是5秒. var keyErro=0; //输入错误次数 var keyRight= ...

  4. angular 开发之proxy

    创建proxy配置文件proxy.conf.json 内容如下  {   "/api/*": { "target": "https://abc.com ...

  5. 数组A - 财务管理

    Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems ...

  6. lintcode 66.67.68 二叉树遍历(前序、中序、后序)

    AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode le ...

  7. django框架<二>

    django框架:   Models 1.基本创建 Django提供了一个抽象层("Model")的构建和管理Web应用程序的数据. Django使用一种新的方式,即:关系对象映射 ...

  8. linux删除第几天日志【原创】

    cat del.sh #!/bin/bash thirty=`date -d '30days ago' +%Y-%m-%d` cd $ #删除输入路径下第30天的日志文件 find . -name & ...

  9. ftp--pureftpd1.0.46

    pureftpd的新版本1.0.46安装过程与之前的相同 但是之后的配置,有些许不同 pureftpd安装过程: # cd /usr/local/src # wget # cd pure-1.0.46 ...

  10. Linux中查看CPU信息 (转)

    cat /proc/cpuinfo中的信息 processor       逻辑处理器的id. physical id    物理封装的处理器的id. core id        每个核心的id. ...