-
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. 题意:一个数组,由0,1,2组成,现在要求升序排列 思路:无脑做法就是计数排序,扫两遍,时间复杂 …
Read More -
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not …
Read More -
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. 思路: 排序,然后two pointer,复杂度 O(n^2) /* *********************************************** Author :111qqz Created …
Read More -
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain duplicate quadruplets. 思路: O(n^2)枚举两个元素,变成2-sum问题,总体复杂度O(n^3) hash的解法以后补 /* …
Read More -
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. 思路:排序O(nlgn),然后枚举一个元素O(n),对于每个元素,在剩下的区间中 two pointer O(n) 整体复杂度 O(n^2)。 hash的解法以后补。 /* …
Read More -
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. 题意:1..9个数,从中选择k个,和为n,要求输出所有满足题意的集合。 思路:枚举子集,根据sum和集合元素个数剪枝即可。 /* *********************************************** Author :111qqz Created Time …
Read More -
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 思路:就是枚举子集,根据集合的大小剪枝。。。最后只要集合大小为k的集合 /* *********************************************** Author :111qqz Created Time :2017年04月13日 星期四 15时25分37秒 File Name :77.cpp ************************************************ */ class Solution { …
Read More -
The set [1,2,3,…,_n_] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): 1. `"123"` 2. `"132"` 3. `"213"` 4. `"231"` 5. `"312"` 6. `"321"` Given n and k, return …
Read More -
Given a collection of numbers that might contain duplicates, return all possible unique permutations.__ 思路:和leet code 46 类似,最后用set去个重即可。。 /* *********************************************** Author :111qqz Created Time :2017年04月13日 星期四 15时00分48秒 File Name :47.cpp ************************************************ */ class …
Read More -
Given a collection of distinct numbers, return all possible permutations. 思路:调用n-1次 leetcode 31 解题报告 中提到的算法即可。。。 /* *********************************************** Author :111qqz Created Time :2017年04月13日 星期四 14时49分34秒 File Name :46.cpp ************************************************ */ class Solution { public: void …
Read More