
Leetcode
佛系城
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
不相交的握手
【题目描述】偶数 个人站成一个圆,总人数为 num_people 。每个人与除自己外的一个人握手,所以总共会有 num_people / 2 次握手。将握手的人之间连线,请你返回连线不会相交的握手方案数。由于结果可能会很大,请你返回答案 模 10^9+7 后的结果。【示例】输入:num_people = 2输出:1输入:num_people = 4输出:2解释:总共有两种方案,第一种方案是 [(1,2),(3,4)] ,第二种方案是 [(2,3),(4,1)] 。输入:num原创 2020-05-30 13:56:18 · 427 阅读 · 0 评论 -
无重复数字的数组全排列
【题目描述】给定一个 没有重复 数字的序列,返回其所有可能的全排列。【示例】输入: [1,2,3]输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]【代码】class Solution {public: void perm(vector<vector<int> > &res, vector<int>& nums, int start, int end)原创 2020-05-29 16:36:25 · 1488 阅读 · 0 评论 -
数组的子集(全排列)
【题目描述】给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。说明:解集不能包含重复的子集。【示例】输入: nums = [1,2,3]输出:[[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[]]【解题思路】这道题利用递归,每次固定一个数,然后保存,再继续遍历下去【代码】class Solution {public: void DFS(vector<vector<int>>原创 2020-05-29 16:04:07 · 653 阅读 · 0 评论 -
组成最大数
【题目描述】给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。【示例】输入: [10,2]输出: 210输入: [3,30,34,5,9]输出: 9534330【解题思路】自定义一种排序比较方法,就是判断ab>ba注意如果数组全是0,输出为“0”【代码】class Solution {public: static bool comp(const string a, const string b) { string sum1 =原创 2020-05-28 20:34:09 · 2910 阅读 · 3 评论 -
数组的度
【题目描述】给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值。你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度。【示例】输入: [1, 2, 2, 3, 1]输出: 2解释:输入数组的度是2,因为元素1和2的出现频数最大,均为2.连续子数组里面拥有相同度的有如下所示:[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]原创 2020-05-28 19:09:15 · 691 阅读 · 0 评论 -
四数之和为target(2)
【题目描述】给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -228 到 228 - 1 之间,最终结果不会超过 231 - 1 。【示例】输入:A = [ 1, 2]B = [-2,-1]C = [-1, 2]D = [ 0, 2]输出:2解释原创 2020-05-28 18:23:39 · 265 阅读 · 0 评论 -
四数之和为target
【题目描述】给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。注意:答案中不可以包含重复的四元组。【示例】给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。满足要求的四元组集合为:[[-1, 0, 0, 1],[-2, -1, 1, 2],[-2, 0, 0, 2]原创 2020-05-28 16:46:52 · 262 阅读 · 0 评论 -
三数之和为0
【题目描述】给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。注意:答案中不可以包含重复的三元组。【示例】给定数组 nums = [-1, 0, 1, 2, -1, -4],满足要求的三元组集合为:[[-1, 0, 1],[-1, -1, 2]]【解题思路】1.暴力解法,就是我们用三个循环嵌套,然后找出和为0的三个数,但是这种情况下很耗时,时间复杂度为O(n^3)原创 2020-05-28 16:29:12 · 1407 阅读 · 1 评论 -
Next Greater Element II
Next Greater Element II原创 2017-03-05 14:48:03 · 262 阅读 · 0 评论 -
Intersection of Two Arrays I
Given two arrays, write a function to compute their intersection.原创 2017-03-20 14:45:48 · 192 阅读 · 0 评论 -
Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.原创 2017-03-20 20:17:59 · 176 阅读 · 0 评论 -
485. Max Consecutive Ones
题目来源【Leetcode】 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last thr原创 2017-07-11 13:12:27 · 259 阅读 · 0 评论 -
566. Reshape the Matrix
题目来源【Leetcode】 In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data. You’re given a matrix repres原创 2017-07-11 13:02:41 · 164 阅读 · 0 评论 -
561. Array Partition I
题目来源【Leetcode】 Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as原创 2017-07-11 12:48:59 · 231 阅读 · 0 评论 -
Minesweeper
Minesweeper原创 2017-06-23 22:13:07 · 227 阅读 · 0 评论 -
Friend Circles
Friend Circles原创 2017-06-23 21:15:53 · 376 阅读 · 0 评论 -
Minimum Depth of Binary Tree
Minimum Depth of Binary Tree原创 2017-06-23 14:03:22 · 165 阅读 · 0 评论 -
Binary Tree Paths
Binary Tree Paths原创 2017-06-23 13:19:10 · 209 阅读 · 0 评论 -
Symmetric Tree
Symmetric Tree原创 2017-06-21 23:00:03 · 163 阅读 · 0 评论 -
Search a 2D Matrix II
Search a 2D Matrix II原创 2017-06-20 20:04:48 · 271 阅读 · 0 评论 -
Maximum Subarray
Maximum Subarray原创 2017-06-20 18:39:57 · 168 阅读 · 0 评论 -
Same Tree
LeetCode原创 2017-04-26 16:43:34 · 229 阅读 · 0 评论 -
Maximum Depth of Binary Tree
LeetCode原创 2017-04-26 16:25:38 · 258 阅读 · 0 评论 -
Next Greater Element I个人解决方法
Next Greater Element I解题代码原创 2017-02-26 16:16:21 · 341 阅读 · 0 评论 -
Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.原创 2017-03-20 20:45:57 · 211 阅读 · 0 评论 -
Kth Largest Element in an Array
LeetCode题目来源 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k原创 2017-03-20 20:50:46 · 240 阅读 · 0 评论 -
Find Bottom Left Tree Value
DFS原创 2017-04-26 12:54:15 · 295 阅读 · 0 评论 -
Find Largest Value in Each Tree Row
DFS原创 2017-04-26 14:02:26 · 192 阅读 · 0 评论 -
1001. 会议安排
1001. 会议安排原创 2017-06-19 21:51:28 · 215 阅读 · 0 评论 -
1002. 等价二叉树
1002. 等价二叉树原创 2017-06-19 22:01:30 · 183 阅读 · 0 评论 -
1003. 相连的1
1003. 相连的1原创 2017-06-20 13:13:41 · 245 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array
题目来源【Leetcode】 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear原创 2017-07-11 13:26:55 · 266 阅读 · 0 评论 -
283. Move Zeroes
题目来源【Leetcode】 Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], a原创 2017-07-11 13:40:43 · 167 阅读 · 0 评论 -
167. Two Sum II - Input array is sorted
题目来源【Leetcode】 Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices原创 2017-07-11 13:52:47 · 175 阅读 · 0 评论 -
122. Best Time to Buy and Sell Stock II
题目来源【Leetcode】 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as yo原创 2017-07-11 14:29:38 · 171 阅读 · 0 评论 -
628. Maximum Product of Three Numbers
题目来源【Leetcode】 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4]原创 2017-07-11 14:42:16 · 167 阅读 · 0 评论 -
217. Contains Duplicate
题目来源【Leetcode】 Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false原创 2017-07-11 14:48:43 · 154 阅读 · 0 评论 -
268. Missing Number
题目来源【Leetcode】 Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array. For example, Given nums = [0, 1, 3] return 2. Note:原创 2017-07-11 15:05:53 · 154 阅读 · 0 评论 -
27. Remove Element
题目来源【Leetcode】 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplic原创 2017-07-11 15:17:02 · 204 阅读 · 0 评论 -
66. Plus One
题目来源【Leetcode】 Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. You may assume the integer do not contain any leading zero, except the number原创 2017-07-12 11:05:41 · 178 阅读 · 0 评论