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)
阅读更多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和集合元素个数剪枝即可。
阅读更多Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
思路:就是枚举子集,根据集合的大小剪枝。。。最后只要集合大小为k的集合
1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月13日 星期四 15时25分37秒 4File Name :77.cpp 5************************************************ */ 6class …
阅读更多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 …
阅读更多Given a collection of numbers that might contain duplicates, return all possible unique permutations.__
思路:和leet code 46 类似,最后用set去个重即可。。
1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月13日 星期四 15时00分48秒 4File Name :47.cpp 5************************************************ */ 6 …
阅读更多Given a collection of distinct numbers, return all possible permutations.
思路:调用n-1次 leetcode 31 解题报告 中提到的算法即可。。。
1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月13日 星期四 14时49分34秒 4File Name :46.cpp 5 ************************************************ */ 6class Solution { …
阅读更多Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some …
阅读更多Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e.,
0 1 2 4 5 6 7
might become4 5 6 7 0 1 2
).You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
思路:找规律。。。二分。。。
阅读更多Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return
[-1, -1]
.For example, Given
[5, 7, 7, 8, 8, 10]
and target value 8, return[3, 4]
.思 …
阅读更多Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
* All numbers (including target) will be positive integers. * The solution set …
阅读更多