[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++实现]200. Number of Islands

染色问题,这里使用的方法修改了传入的参数grid,这个题目也可以使用并查集方法解决。

class Solution {

private:

int m;

int n;

void dfs(vector<vector<char>>& grid,int i,int j) {

if(i < 0 || j < 0 || i >= m || j >= n || grid[i][j] != '1')

return;

grid[i][j] = '0';

dfs(grid,i-1,j);

dfs(grid,i+1,j);

dfs(grid,i,j......

[LeetCode C++实现]1669. Merge In Between Linked Lists

首先无脑爆破,使用最直观的方法AC

/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* ListNode *next;

* ListNode() : val(0), next(nullptr) {}

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

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

* };

*/

class Solution {

p......

[LeetCode C++实现]152. Maximum Product Subarray

这道题目加深了我对于DP的理解,这里需要记录最大值和最小值,最大值来源于正数相乘或负数相乘,因此初始时最终结果res cur_min cur_max均为nums[0],然后计算cur_min和cur_max乘以nums[i],最终计算三者当中最小值 最大值,更新到cur_min 和cur_max在下一个循环里继续处理,最终结果即res.

class Solution {

public:

int maxProduct(vector<int>& nums) {

int size = nums.size();

int res = nums[0], cur_min = num......

[LeetCode C++实现]62. Unique Paths

看到题目知道需要用到DFS,自己写了一版存在重复计算的bug,代码如下:

class Solution {

public:

int uniquePaths(int m, int n) {

int res = 0;

help(0,0,m,n,res);

return res;

}

void help(int start_m,int start_n,int m,int n,int &res)

{

if(start_m >= m-1 && start_n >= n-1)

return;

if(start_m < m -1)

{

res++;

hel......

[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......