[LeetCode C++实现]238. Product of Array Except Self

Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Example:

Input:

Output:

Constraint: It's guaranteed that the product of the elements of any prefix or suffix of the array......

[LeetCode C++实现]105. Construct Binary Tree

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:

You may assume that duplicates do not exist in the tree.

For example, given

preorder = [3,9,20,15,7]

inorder = [9,3,15,20,7]

Return the following binary tree:

3

/ \

9 20

/ \

15 7

记得考研的时候学习数据结构,经常会遇到根据某两种序列求另一种序......

[LeetCode C++实现]53. Maximum Subarray

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],

Output: 6

Explanation: [4,-1,2,1] has the largest sum = 6.

刷到这道题的时候我想起来原来在理工大读书的时候有门课叫算法设计与分析,当时有个0 1背包问题,虽然当时不......

[LeetCode C++实现]124. Binary Tree Maximum Path

二叉树的题目虽然标着hard类型,但解起来有点爽,这道题目琢磨透了觉得非常的棒,可以说这种类型题目是一种套路。可以结合文章段错误segment fault分析一起使用更佳。或许屏幕前的朋友会有疑问,这篇讲函数调用栈的文章和leetcode124有啥关系?其实树里面很多递归的逻辑就是函数调用,直到调用到终止条件,然后层层向上返回的过程。

题目:

Given a non-empty binary tree, find the maximum path sum.

For this problem, a path is defined as any sequence of nodes ......

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