- 博客(22)
- 收藏
- 关注
原创 Leetcode freedom trail
题目In the video game Fallout 4, the quest “Road to Freedom” requires players to reach a metal dial called the “Freedom Trail Ring”, and use the dial to spell a specific keyword in order to open the door
2018-01-12 11:55:19
298
原创 Leetcode Jump Game
题目Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine i
2018-01-11 21:28:46
162
原创 Leetcode evaluate Division
题目Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer
2018-01-11 21:13:10
252
原创 Leetcode 01 Matrix
题目给出一个矩阵,寻找最近的零题目解析找出最近的零,相当于寻找一个最短路径的问题,有两种想法思想一暴力求解: 1. 先把0的位置存起来 2. 遇到1时,将每个1和0的欧式距离求出,取最小值 3. 返回最新的矩阵 这种思想马上会超时。class Solution {public: vector<vector<int> > updateMatrix(vector<vector<int
2018-01-07 11:49:52
180
原创 Leetcode minimum height trees
题目For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called min
2018-01-07 10:50:23
171
原创 强连通分量的简单概括
简介对于强连通分量,是只懂得原理,但不知道如何用算法实现求解。今天就对强连通分量的算法进行一个整理,也对强连通分量的实例进行分析。强连通分量强连通分量出现在有向有环图中,每一个连通的分量都可以被当作是强连通分量。如左下图每一个虚线框内都是一个强连通分量,右下图则将强连通分量当作一个结点后构成的一个DAG 在《算法概论》中采用的pre,post时间序列的线性算法中,强连通分量有以下的特点: 1.
2018-01-06 12:25:22
2490
原创 Leetcode 分治法
分治法分治法的思想是将问题分而治之: 1. 将问题分成小问题 2. 分治完毕后,触发递归中的结束判定语句 3. 将分治的结果 合并一起求majority题目Given an array of size n, find the majority element. The majority element is the element that appears more th
2018-01-05 10:01:41
356
原创 Leetcode Merge k sorted lists
题目Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.题目分析将k个已经排好序的链表合并成一个sorted的链表。可以采用归并排序的思想: 1. 读出k个链表的链表头,并比较它们的大小 2. 取出最小的那个链表头,并将它连接到新链表的尾部 3. 重复
2018-01-03 20:36:57
182
原创 《算法概论》习题8.8
题目在精确的4SAT(EXACT 4SAT)问题中,输入为一组自居,每个子句都是恰好4个文字的析取,且每个变量最多在每个自居中出现依次。目标是求它的满足赋值——如果该赋值存在。证明精确的4SAT是NP-完全问题。题目分析证明是NP-完全问题,可以用一个已知的NP-完全问题A,用它归约到4SAT问题,即可得证。首先4SAT是SAT的一种特例,它是属于NP的。已知3SAT是NP-完全问题,给定一个3SA
2018-01-03 13:09:15
205
原创 Leetcode Ugly numbers
题目Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ug
2017-12-31 23:30:27
141
原创 Leetcode Unique BST
题目给定正数n,计算1到n组成不同的BST的个数。题目分析BST的特点是左子树小于根, 右子树大于根,这是一个递归的定义。因此我们也可以采用递归的反方向,自下而上的递推出不同BST的个数。首先计算1个结点的BST个数,接下来计算2个,3个,……直到计算完n个结点。建立两个数组,cnt[i]和dp[i][j],cnt[i]表示i个结点能够组成不同的BST的个数,dp[i][j]表示给出i个结点,以j为
2017-12-19 15:35:13
203
原创 Leetcode Longest Increasing Subsequences
题目描述Given an unsorted array of integers, find the length of longest increasing subsequence.For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101],
2017-12-19 14:20:58
158
原创 Leetcode 股票交易
股票交易系列共有三道题目,难度递增。1题Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell
2017-12-19 14:17:34
547
原创 Leetcode 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.解题思路
2017-11-26 15:25:51
287
原创 Leetcode 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.A zero-indexed array A consisting of N nu
2017-11-08 15:41:06
164
原创 Leetcode Minimum path sum
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.题目解析: 这道题第一眼看到minimum path马上就可以联想到最小生成树,可以将这一个
2017-11-05 17:52:44
202
原创 Leetcode House Robber
House Robber I题目描述You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is th
2017-10-26 14:10:49
231
原创 判断BST是否合法
题目介绍:二叉搜索树(BST)的结构是左子树的值小于根值,右子树的值小于根值。解题思路二叉搜索树的定义本身就是递归的,因此,在这种情况下,用递归的方法去判断每一个子树是否合法最恰当。 从上图可以看出:左子树的最深右子树必须小于根值,右子树的最深左子树必须大于根值。同时在递归的时候判断左子树要小于父节点的值,右子树要大于父节点的值即可。源代码/** * Definition for a bin
2017-10-16 08:48:24
1233
原创 leetcode easy题page1小结
Intro之前一直埋头于leetcode的medium和hard难度的题目,今天想变化一下,将第一页的easy题刷完,并小结一下。Q7 reverse integerDescription: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reverse
2017-09-28 14:26:16
254
原创 ZigZag Conversion
题目意思是将字符串变成Z型字符串这道题一开始我是想着通过计算下一步取的字符所在的位置来构成zigzag字符串。解题思路: 1. 首先进行特殊情况处理,如果len(字符串长度)小于行数,或者行数等于1输出它本身 2. 接下来分成s.length() / numRows个小片,每片长度为2*(numRows-1) 3. 由于在切片过程中容易忽略掉s.length()==sliceLen
2017-09-20 20:21:56
174
原创 Leetcode Median of two sorted arrays解题
Q4 Median of two sorted array首先奉上题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity sh
2017-09-14 15:55:28
216
原创 Leetcode 初探
Add Two Numbers第一次在leetcode上面做题,选了两道最简单的题目来试试手,回忆一下cpp的写法。写了一个暑假的python之后,在cpp语法上稍微有点不太习惯,还好,做完前两道题就上手了。这里主要讲一讲Question2. 题目原意: You are given two non-empty linked lists representing two non-n
2017-09-09 18:12:07
190
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人