算法题解
文章平均质量分 79
SYSU_BY
修身齐家治国平天下
先想后干,想明白再干,不明白不干
展开
-
LeetCode107. Binary Tree Level Order Traversal II题解
1. 题目描述Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).2. 样例Given binary tree [3,9,20,null,null,15原创 2017-03-20 19:29:53 · 279 阅读 · 0 评论 -
LeetCode435. Non-overlapping Intervals题解
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.原创 2017-04-12 18:44:25 · 606 阅读 · 1 评论 -
LeetCode413. Arithmetic Slices题解
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.原创 2017-04-28 21:35:55 · 340 阅读 · 0 评论 -
LeetCode376. Wiggle Subsequence题解
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative.原创 2017-04-18 21:22:46 · 576 阅读 · 0 评论 -
LeetCode122. Best Time to Buy and Sell Stock II题解
今天讨论一道让我想的过于复杂的题目:一道有关股票买卖的问题。题目描述:Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many原创 2017-04-06 16:18:49 · 492 阅读 · 0 评论 -
LeetCode338. Counting Bits题解
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.原创 2017-05-10 22:19:05 · 319 阅读 · 0 评论 -
LeetCode121. Best Time to Buy and Sell Stock题解
Say you have an array for which the i element is the price of a given stock on day i.If you were only permitted to complete at most one transaction.原创 2017-05-04 00:07:51 · 312 阅读 · 0 评论 -
LeetCode486. Predict the Winner题解
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on.原创 2017-05-24 17:16:27 · 604 阅读 · 0 评论 -
LeetCode70. Climbing Stairs题解
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?原创 2017-05-05 12:41:41 · 418 阅读 · 0 评论 -
LeetCode357. Count Numbers with Unique Digits题解
题目描述Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^(n).原创 2017-05-17 14:18:57 · 364 阅读 · 0 评论 -
Sicily1005. 最大和题解
从数列A[0], A[1], A[2], …, A[N-1]中选若干个数,要求相邻的数不能都选,也就是说如果选了A[i], 就不能选A[i-1]和A[i+1]. 求能选出的最大和. 1 <= N <= 100000, 1 <= A[i] <= 10002.原创 2017-06-18 13:50:49 · 443 阅读 · 0 评论 -
Sicily1210.二叉树题解
题目描述在众多的数据结构中,二叉树是一种特殊而重要的结构,有着广泛的应用。二叉树或者是一个结点,或者有且仅有一个结点为二叉树的根,其余结点被分成两个互不相交的子集,一个作为左子集,另一个作为右子集,每个子集又是一个二叉树。原创 2017-06-07 19:28:16 · 749 阅读 · 0 评论 -
LeetCode48. Rotate Image题解
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). 题目很简单,给了一个n*n的2D方阵,我们需要将其顺时针旋转90°。原创 2017-06-01 16:26:41 · 550 阅读 · 0 评论 -
LeetCode494. Target Sum题解
You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.原创 2017-06-04 16:30:00 · 1881 阅读 · 4 评论 -
Sicily1209. Sequence Sum Possibi题解
题目描述Most positive integers may be written as a sum of a sequence of at least two consecutive positive integers.原创 2017-06-08 20:10:39 · 366 阅读 · 1 评论 -
《算法概论》8.3
STINGY SET is the following problem: given a set of clauses (each a disjunction of literals) and an integer k, find a satisfying assignment in which at most k variables are true原创 2017-06-22 13:36:12 · 425 阅读 · 0 评论 -
LeetCode53. Maximum Subarray题解
1. 题目描述Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1原创 2017-03-02 14:51:40 · 343 阅读 · 0 评论 -
LeetCode485.Max Consecutive Ones题解
本学期算法课需要每周做几道leetcode的算法题,再写成题解贴到博客上面。先从几道简单的开始练练手,毕竟大三之后几乎都在做project,很少做算法题了。1. 题目描述Given a binary array, find the maximum number of consecutive 1s in this array.原创 2017-02-26 14:39:18 · 300 阅读 · 0 评论 -
LeetCode103. Binary Tree Zigzag Level Order Traversal题解
Given a binary tree, return the zigzag level order traversal of its nodes’ values. (IE, from left to right, then right to left for the next level and alternate between).原创 2017-03-22 01:20:24 · 277 阅读 · 0 评论 -
LeetCode406. Queue Reconstruction by Height题解
最近算法课讲到了贪心算法,接下来的几篇题解应该都会与贪心算法相关。1. 题目描述Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k原创 2017-03-29 11:28:52 · 268 阅读 · 0 评论 -
LeetCode452. Minimum Number of Arrows to Burst Balloons题解
有若干个气球,给出了它们的直径两个端点的坐标(X轴),我们的工作是需要用一支箭来戳破气球。例如两个气球的直径分别是(1,6)和(2,8),那么我们就可以在x=6的坐标处发射一支箭从而将它们一起戳破。求解的问题是:给出一系列气球直径的坐标之后,最少能用多少支箭就可以戳破它们。原创 2017-04-10 12:53:35 · 1175 阅读 · 0 评论 -
LeetCode101. Symmetric Tree题解
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).原创 2017-03-25 18:17:47 · 395 阅读 · 0 评论 -
LeetCode455. Assign Cookies题解
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size原创 2017-04-03 21:25:59 · 342 阅读 · 0 评论 -
LeetCode513. Find Bottom Left Tree Value题解
1. 题目描述Given a binary tree, find the leftmost value in the last row of the tree.原创 2017-03-16 20:48:30 · 453 阅读 · 0 评论 -
LeetCode515. Find Largest Value in Each Tree Row题解
1. 题目描述You need to find the largest value in each row of a binary tree.原创 2017-03-15 16:25:00 · 309 阅读 · 0 评论 -
LeetCode112.Path Sum题解
1. 题目描述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.原创 2017-03-13 17:18:47 · 257 阅读 · 0 评论 -
LeetCode110.Balanced Binary Tree题解
1. 题目描述Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never原创 2017-03-09 19:31:02 · 293 阅读 · 0 评论 -
LeetCode392.Is Subsequence题解
1. 题目描述Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000)原创 2017-03-08 20:42:44 · 403 阅读 · 0 评论 -
LeetCode167.Two Sum II - Input array is sorted题解
1. 题目描述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 of the two原创 2017-03-08 13:07:34 · 359 阅读 · 0 评论 -
LeetCode240. Search a 2D Matrix II题解
1. 题目描述Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers原创 2017-03-03 19:30:56 · 333 阅读 · 0 评论 -
LeetCode169. Majority Element题解
1. 题目描述Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume th原创 2017-03-01 21:28:45 · 322 阅读 · 0 评论 -
LeetCode445.Add Two Numbers II题解
1. 题目描述You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and r原创 2017-02-26 20:07:19 · 228 阅读 · 0 评论 -
LeetCode448.Find All Numbers Disappeared in an Array题解
1. 题目描述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 in this array原创 2017-02-26 15:02:56 · 255 阅读 · 0 评论 -
LeetCode542. 01 Matrix题解
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance between two adjacent cells is 1.原创 2017-06-26 13:04:50 · 902 阅读 · 0 评论