[LeetCode C++实现]78.Subsets

加班回来同事微信发来一道题目说这是整个leetcode中最棒的一道题目获益良多,本着我坚决不信的态度,尝试了两种平平无奇的解法后正打算开喷,在讨论区看到一种位运算的方法,好吧,我承认这是最棒的一道题。

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]

Output:

[

[3],

[1],

[......

[LeetCode C++实现]347. Top K Frequent Elements

Given a non-empty array of integers, return the k most frequent elements.

Example 1:

Input: nums = [1,1,1,2,2,3], k = 2

Output:

Example 2:

Input: nums = [1], k = 1

Output:

Note:

You may assume k is always valid, 1 ≤ k ≤ number of unique elements.

Your algorithm's time complexity......

[LeetCode C++实现]22.Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

[

"((()))",

"(()())",

"(())()",

"()(())",

"()()()"

]

首先这种类型的题目一眼看上去肯定是可以用递归的形式,递归的写法往往是一看就懂,......

[LeetCode C++实现]406. Queue Reconstruction by Height

在leetcode.com上随机选择到这个题目,讲道理,第一遍没看懂题目啥意思,有很多人也在吐槽题目读不懂I don't understand the question. Reconstruct the queue to what?直到我打开讨论区看了一眼别人的python代码,如果你也看不懂这个题目描述,我结合中文版的描述加以完善:

现在有一个队列,每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数。但现在输入的序列是随机打乱的,编写一个算法来重建这个队列。

输入队列:

[[7,0], [4,4], [7,1], [5,0], ......

[LeetCode C++实现]46.Permutations

46.Permutations

一口气刷了3道简单题目,正当得意忘形的时候遇到了一道求全排列的题,竟然一时间不知道如何下手。

由于恰好看algorithm中有实现next_permutation,因此直接使用stl中的现有实现:

方法一:

next_permutation

class Solution {

public:

vector<vector<int> > permute(vector<int> &num) {

vector<vector<int> > ans;

sort(num.begin(), num......

[LeetCode C++实现]86.Partition List

86.Partition List

解法与思路:

将小于某一值x的结点加到pless所指向的结点,将大于某一直x的结点加到pbigger所指向的结点。需要注意内存泄露与将pbigger最后一个结点next置为NULL。

/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* ListNode *next;

* ListNode(int x) : val(x), next(NULL) {}

* };

*/

class Solution {

public:

ListNode......

[LeetCode C++实现]189.Rotate Array

189.Rotate Array

解法一,空间复杂度O(n)

class Solution {

public:

void rotate(vector<int>& nums, int k) {

int length = nums.size();

if(k <= 0 || length <= 1)

return;

//copy vector nums

vector<int> copy_nums;

vector<int>::iterator it = copy_nums.begin();

copy_nums.insert(it,n......

[LeetCode C++实现]13. Roman to Integer

13. Roman to Integer

罗马数字转整数,写了一堆if else之后打算改成switch,等等,我刷leetcode不是想学c++吗,那用c++应该会简单不少。

解法如下:

class Solution {

public:

int romanToInt(string s)

{

map<char, int> T = { { 'I' , 1 },

{ 'V' , 5 },

{ 'X' , 10 },

{ 'L' , 50 },

{ 'C' , 100 },

{ 'D' , 500 },

{ 'M' , 1000 } };

int sum = T[s.bac......

[LeetCode C++实现]1030. Matrix Cells in Distance Order

leetcode 1030

学习c++ sort的基本用法之后,这道题还是比较easy.

Beginners guide to the std::sort() function

解法一:

class Solution {

public:

vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {

vector<vector<int>> res(R*C,vector<int>(3));

int num=0;

for(int i=0;i<......

[LeetCode C实现]28. Implement strStr()

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"

Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"

Output: -1......