![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
KID怪盗基德1412
这个作者很懒,什么都没留下…
展开
-
整数对查找
题目描述请设计一个高效算法,找出数组中两数之和为指定值的所有整数对。给定一个int数组A和数组大小n以及需查找的和sum,请返回和为sum的整数对的个数。保证数组大小小于等于3000。测试样例:[1,2,3,4,5],5,6返回:2//用红黑树最大的陷阱就是可能sum可以分为两个相同的数之和class FindPair {public: int count...原创 2019-09-04 23:03:53 · 367 阅读 · 0 评论 -
rotate-list(循环移动链表)
题目描述Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.Solutio...原创 2019-06-20 20:51:11 · 233 阅读 · 0 评论 -
LeetCode--subsets-ii(所有可能的子集合)
题目描述Given a collection of integers that might contain duplicates,S, return all possible subsets.Note:Elements in a subset must be in non-descending order. The solution set must not contain dup...转载 2019-06-04 16:42:16 · 242 阅读 · 0 评论 -
LeetCode--remove-duplicates-from-sorted-list-ii(链表去重)
题目描述Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3->3->4->4->5, return1-&...原创 2019-06-10 20:29:14 · 211 阅读 · 0 评论 -
LeetCode---unique-paths-ii(路径个数)
题目描述A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach ...原创 2019-06-19 21:23:05 · 269 阅读 · 0 评论 -
LeetCode--set-matrix-zeroes(很巧妙的解法)
题目描述Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(...转载 2019-06-15 15:45:06 · 187 阅读 · 0 评论 -
LeetCode---reverse-linked-list-ii(一段区间内的链表逆序)
题目描述Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.No...原创 2019-06-03 20:10:53 · 423 阅读 · 0 评论 -
LeetCode--subsets(数组的所有可能子集合)
题目描述Given a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets.For example,...原创 2019-06-14 21:12:57 · 407 阅读 · 0 评论 -
LeetCode---restore-ip-addresses(IP地址转换)
题目描述Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order d...原创 2019-06-03 18:53:03 · 349 阅读 · 0 评论 -
LeetCode--word-search(矩阵方格中单词的查找bfs应用)
Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically n...原创 2019-06-11 14:56:41 · 279 阅读 · 0 评论 -
LeetCode--combinations(求一个数组指定个数所有组合)
题目描述Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4...原创 2019-06-11 15:18:35 · 991 阅读 · 0 评论 -
N皇后问题(C++)
n皇后问题研究的是如何将 n个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。(即任意两个皇后都不能处于同一行、同一列或同一斜线上).上图为 8 皇后问题的一种解法。给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。示例:输入: 4输出: [...原创 2019-07-13 11:26:28 · 3743 阅读 · 0 评论 -
最长递增子序列
这类算法与 堆箱子 和叠罗汉都是同一类型题目。解法一致叠罗汉I:题目描述叠罗汉是一个著名的游戏,游戏中一个人要站在另一个人的肩膀上。同时我们应该让下面的人比上面的人更高一点。已知参加游戏的每个人的身高,请编写代码计算通过选择参与游戏的人,我们最多能叠多少个人。注意这里的人都是先后到的,意味着参加游戏的人的先后顺序与原序列中的顺序应该一致。给定一个int数组men,代表依次来的每个人...原创 2019-09-01 11:40:20 · 222 阅读 · 0 评论 -
元素查找(移动有序数组)
题目描述有一个排过序的数组,包含n个整数,但是这个数组向左进行了一定长度的移位,例如,原数组为[1,2,3,4,5,6],向左移位5个位置即变成了[6,1,2,3,4,5],现在对于移位后的数组,需要查找某个元素的位置。请设计一个复杂度为log级别的算法完成这个任务。给定一个int数组A,为移位后的数组,同时给定数组大小n和需要查找的元素的值x,请返回x的位置(位置从零开始)。保证数组中元...原创 2019-09-01 10:45:37 · 364 阅读 · 0 评论 -
堆箱子
题目描述有一堆箱子,每个箱子宽为wi,长为di,高为hi,现在需要将箱子都堆起来,而且为了使堆起来的箱子不倒,上面的箱子的宽度和长度必须小于下面的箱子。请实现一个方法,求出能堆出的最高的高度,这里的高度即堆起来的所有箱子的高度之和。给定三个int数组w,l,h,分别表示每个箱子宽、长和高,同时给定箱子的数目n。请返回能堆成的最高的高度。保证n小于等于500。测试样例:[1,1,1...原创 2019-08-31 22:04:23 · 504 阅读 · 0 评论 -
二叉树的中序遍历(非递归)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *///非...原创 2019-08-31 16:46:42 · 173 阅读 · 0 评论 -
课程表(拓扑排序)
现在你总共有 n 门课需要选,记为 0 到 n-1。在选修某些课程之前需要一些先修课程。 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1]给定课程总量以及它们的先决条件,判断是否可能完成所有课程的学习?示例 1:输入: 2, [[1,0]]输出: true解释: 总共有 2 门课程。学习课程 1 之前,你需要完成课程 0。所以这是可能的。...转载 2019-08-26 21:09:35 · 778 阅读 · 0 评论 -
三维形体的表面积
在 N * N 的网格上,我们放置一些 1 * 1 * 1 的立方体。每个值 v = grid[i][j] 表示 v 个正方体叠放在对应单元格 (i, j) 上。请你返回最终形体的表面积。示例 1:输入:[[2]]输出:10示例 2:输入:[[1,2],[3,4]]输出:34示例 3:输入:[[1,0],[0,2]]输出:16示例 4:输入:[[1,...转载 2019-08-16 07:55:19 · 312 阅读 · 0 评论 -
等价多米诺骨牌对的数量
给你一个由一些多米诺骨牌组成的列表dominoes。如果其中某一张多米诺骨牌可以通过旋转 0度或 180 度得到另一张多米诺骨牌,我们就认为这两张牌是等价的。形式上,dominoes[i] = [a, b]和dominoes[j] = [c, d]等价的前提是a==c且b==d,或是a==d 且b==c。在0 <= i < j < dominoe...原创 2019-07-21 15:58:21 · 232 阅读 · 0 评论 -
迷宫bfs应用(华为OJ题目)
'S'是开始位置,'#'表示墙,'.'是通路,'E'是终点,求最短路径长度#include <iostream>#include<vector>#include<string>#include<utility>#include<queue>using namespace std;int offset[4][2]={...原创 2019-07-24 19:59:01 · 299 阅读 · 0 评论 -
LeetCode-maximal-rectangle(最大矩阵面积)
题目描述Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. ...转载 2019-06-09 20:04:59 · 1283 阅读 · 0 评论 -
partition-list(链表根据值分割两部分)
题目描述Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in eac...原创 2019-06-09 09:16:43 · 251 阅读 · 0 评论 -
interleaving-string(交叉字符串)
题目描述Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac", return true.Whens3="aadbbbaccc", return fals...原创 2019-06-03 09:21:58 · 212 阅读 · 0 评论 -
字符串分割为回文串(palindrome-partitioning-ii)
题目描述Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s ="aab",Return [ ["aa",...原创 2019-04-30 10:55:26 · 688 阅读 · 0 评论 -
奇偶位交换
题目描述请编写程序交换一个数的二进制的奇数位和偶数位。(使用越少的指令越好)给定一个intx,请返回交换后的数int。测试样例:10返回:5Solution 1//略微复杂了一丝,不过时间复杂度还是可观的/*思路:设置两个位变量a和tempa,其中tempa=a<<1举个例子:当x=5=0101a从1开始,如果a&x与tempa&am...原创 2019-05-05 11:09:20 · 689 阅读 · 0 评论 -
最近公共祖先
题目描述有一棵无穷大的满二叉树,其结点按根结点一层一层地从左往右依次编号,根结点编号为1。现在有两个结点a,b。请设计一个算法,求出a和b点的最近公共祖先的编号。给定两个inta,b。为给定结点的编号。请返回a和b的最近公共祖先的编号。注意这里结点本身也可认为是其祖先。测试样例:2,3返回:1class LCA {public: int getLCA(in...原创 2019-05-04 17:49:29 · 155 阅读 · 0 评论 -
single-number(寻找数组中单个数字)
题目描述Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using...转载 2019-04-28 18:04:37 · 266 阅读 · 0 评论 -
word-break-ii(字典元素拼接单词)
题目描述Given a stringsand a dictionary of wordsdict, add spaces insto construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens="c...原创 2019-04-27 20:37:37 · 200 阅读 · 0 评论 -
reorder-list
题目描述Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reor...原创 2019-04-26 20:30:29 · 80 阅读 · 0 评论 -
二叉树的前序遍历(非递归)
题目描述Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note:Recursive solution is tr...原创 2019-04-26 15:53:28 · 120 阅读 · 0 评论 -
二叉树的后序遍历(非递归)
题目描述Given a binary tree, return thepostordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].Note:Recursive solution is tr...原创 2019-04-26 09:26:11 · 88 阅读 · 0 评论 -
字符串分割为回文串的最小分割次数(palindrome-partitioning-i)
题目描述Given a strings, partitionssuch that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning ofs.For example, givens="aab",Return...原创 2019-04-30 11:29:53 · 1032 阅读 · 0 评论 -
Leetcode之surrounded-regions(包围区域)
题目描述Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's in that surrounded region .For example,X X X XX O O XX X O X...原创 2019-04-30 16:09:24 · 225 阅读 · 0 评论 -
word-ladder
题目描述Given two words (startandend), and a dictionary, find the length of shortest transformation sequence fromstarttoend, such that:Only one letter can be changed at a time Each intermediate ...原创 2019-05-08 21:22:08 · 152 阅读 · 0 评论 -
minimum-window-substring(最小区间子串)
题目描述Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S="ADOBECODEBANC"T="ABC"Minimum window is"BANC".No...原创 2019-06-12 19:45:40 · 689 阅读 · 0 评论 -
distinct-subsequences(字符串中相同子序列个数)
题目描述Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subsequence of a string is a new string which is formed from the original string by deleting some (can be...转载 2019-05-28 21:52:57 · 971 阅读 · 0 评论 -
单链表转换为平衡搜索树
题目描述Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for singly-linked list. * struct ListNode { * int val; * ...原创 2019-05-28 21:02:52 · 157 阅读 · 0 评论 -
LeetCode(path-sum)
题目描述Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and...原创 2019-05-12 19:33:21 · 154 阅读 · 0 评论 -
triangle
题目描述Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], ...转载 2019-05-09 20:53:23 · 115 阅读 · 0 评论 -
binary-tree-maximum-path-sum(二叉树最大路径和)
题目描述Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Return 6.思路:这一题...原创 2019-05-01 16:03:34 · 136 阅读 · 0 评论