[LeetCode C++实现]155. Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

push(x) -- Push element x onto stack.

pop() -- Removes the element on top of the stack.

top() -- Get the top element.

getMin() -- Retrieve the minimum element in the stack.

Example 1:

Input

["MinStack"......

[LeetCode C++实现]1470. Shuffle the Array

Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].

Return the array in the form [x1,y1,x2,y2,...,xn,yn].

Example 1:

Input: nums = [2,5,1,3,4,7], n = 3

Output: [2,3,5,4,1,7]

Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].

......

[LeetCode C++实现]560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

Example 1:

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

Output: 2

Constraints:

The length of the array is in range [1, 20,000].

The range of numbers in the array is [-1000, 1000] and the ran......

[LeetCode C++实现]532. K-diff Pairs in an Array

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.

Example 1:

Input: [3, 1, 4, 1, 5], k = 2

Output: 2

E......

[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: [1,2,3,4]

Output: [24,12,8,6]

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

[LeetCode C++实现]740. Delete and Earn

这道题讲道理是我最近做的收获最大的一道题,最开始我的解法考虑比较片面,没考虑到存在重复元素场景,后面看了讨论区万万没想到这道题居然会和打家劫舍系列问题扯上关系,真的是万万想不到。

最开始的实现代码:

class Solution {

public:

int deleteAndEarn(vector<int>& nums) {

unordered_set<int> hash(nums.begin(),nums.end());

int scores = 0;

helper(hash,scores);

return scores;

}

private:

voi......

[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],

[......