刷题
文章平均质量分 52
Zhongyi_Li
这个作者很懒,什么都没留下…
展开
-
算法中常用的技术
一、recursion 递归技术leetcode上对recursion的解释Principle of RecursionReverse String 最经典的例子就是把一个数组(或者一个字符串)倒序链表相关题目 还有个经典的例子就是反转一个链表,递归和迭代都可以做,都很好理解Fibonacci Number 二、Two-Pointer Technique 双指针技术...原创 2019-12-20 16:48:09 · 626 阅读 · 0 评论 -
农行笔试题之多少年后两国人均GDP相等
具体题目记得不准,只能大概描述一下,有两个国家A和B,分别给出A国的GDP(21.43)、GDP增长率(0.023)、人口数(3.28)、人口增长率(0.015),B国的GDP(14.36)、GDP增长率(0.061)、人口数(14)、人口增长率(0.0033),假设每年的GDP增长率和人口增长率保持不变的情况下,多少年后两国的人均GDP相等。这道题考察的是我高中学过的幂、对数运算操作和java Math运算类的掌握情况。我们先感性的对这个题分析一下,A国的GDP比B国的高,人口数却比B国的少,就目前来原创 2022-01-19 11:56:47 · 577 阅读 · 0 评论 -
二分查找
704. Binary Searchclass Solution { public int search(int[] nums, int target) { if (nums == null || nums.length == 0) return -1; int left = 0; int right = num...原创 2020-01-17 16:08:02 · 300 阅读 · 0 评论 -
二叉树相关题目
在二叉树中常用的技术(1)递归(2)深度优先遍历(3)广度优先遍历1、对二叉树进行先序、中序或者后序遍历采用递归很简单,一般面试都让用非递归实现,采用深度优先遍历,熟悉了其实也不难说明一下:深度优先也有很多策略,比如先序遍历,是按层深度的,具体:根节点入栈,然后是右节点、左节点入栈以此类推。中序遍历,是左子树不断入栈,直到没有左子树,重复这个过程。二叉树的深度遍历...原创 2019-12-31 11:21:03 · 98 阅读 · 0 评论 -
接雨水问题(Trapping Rain Water)
先说一个整体思路:无论是用什么算法,其实整体思路是一样的,只是实现有差别。对于每个元素(画图中是柱子)它上面能不能放雨水,能放多少雨水,是由这个元素左右两边的最大元素决定的,min(left_max, right_max)- 当前元素的高度。进一步简化一下,只要发现左右两边最大元素的小的那个元素就可以了,也就是说,我们遍历的方向上,在远端有元素大于当前值,当前元素的可能蓄水值就是由这个方向的最...原创 2019-12-31 11:19:24 · 690 阅读 · 0 评论 -
二叉树的右视图
199. Binary Tree Right Side ViewDFS/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val =...原创 2019-12-26 18:22:29 · 174 阅读 · 0 评论 -
双指针技术(two-pointer technique)
要想用好双指针技术还是挺难的,尤其是快慢指针,所以单独开一篇文章一、数组中使用双指针(1)一个升序数字型的一维数组,给定一个目标值,找出数组中两个位置,这两个位置元素的和等于目标值。Two Sum II - Input array is sorted思路:采用从两端都中间移动的双指针技术。两指针元素的和大于目标值,右指针向前移,如果两指针元素的和小于目标值,左指针后移,直到相等。...原创 2019-12-24 17:06:08 · 658 阅读 · 0 评论 -
链表相关题目
最基本的就是反转单链表,递归和迭代都可以实现Reverse Linked List递归解法:从后往前反转,这是不断向新链表的尾加节点,直到结束。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * List...原创 2019-12-24 11:16:25 · 111 阅读 · 0 评论 -
一维数组相关题目
数字类型的一维数组常用解题思路(1)两次循环遍历(2)一次循环遍历即可(3)从后往前遍历(4)排序后遍历(5)双指针技术 (分为两种:左右指针和快慢指针)具体题目(1)从一个数字型一维数组中,找到一个元素,这个元素左右两边元素的和相同,如果找到返回这个元素的位置,没有找到返回-1。Find Pivot Index思路:采用两次循环遍历,第一次遍历获取数组的和,第...原创 2019-12-24 11:17:12 · 390 阅读 · 0 评论 -
leetcode 聪明题,需要背的
Valid ParenthesesArray Partition I原创 2019-12-20 17:21:42 · 235 阅读 · 0 评论 -
二维数组相关题目
对角线遍历矩阵class Solution { public int[] findDiagonalOrder(int[][] matrix) { if (matrix == null || matrix.length == 0) return new int[0]; int M = matrix.length; int N = matr...原创 2019-12-20 16:55:06 · 261 阅读 · 0 评论 -
多字符串相关题目
一般围绕两个字符串的算法题,都有如下思路:(1)两个字符串同时遍历(2)嵌套遍历两个二进制字符串相加class Solution { public String addBinary(String a, String b) { StringBuilder result = new StringBuilder(); int i = a.leng...原创 2019-12-20 16:45:25 · 113 阅读 · 0 评论 -
Climbing Stairs (LeetCode)
You are climbing a stair case. It takesnsteps 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?Note:Givennwill be a positive...原创 2019-10-21 10:29:07 · 153 阅读 · 0 评论 -
五大算法之一——动态规划(Dynamic Programming),Memoization
动态规划是解决多阶段决策过程最优化的一种有效的数学方法。Memoization 一种计算优化技术。大白话就是不要重复计算,用空间换时间,把需要重复计算的中间值存起来。在动态规划中需要用到此技术。个人理解,动态规划和分治的区别,动态规划可以按照步来求解,一步一步的求解,最后就得到解决,而分治把大问题不断分成小问题,小问题解决了,最终解决大问题。还有就是分治算法中,不存在重复计算中间值的问题。...原创 2019-10-19 12:06:30 · 510 阅读 · 0 评论 -
Fibonacci Number (leetcode)---动态规划和memoization
TheFibonacci numbers, commonly denotedF(n)form a sequence, called theFibonacci sequence, such that each number is the sum of the two preceding ones, starting from0and1. That is,F(0) = 0, F...原创 2019-10-19 11:44:58 · 284 阅读 · 0 评论 -
五大常用算法之一——分治算法
分治算法(Divide and Conquer)分治算法,简单理解就是把一个大规模的问题分成相同或者相似的子问题,直到最后子问题能很容易的求解,原问题的解通过子问题的解合并得到。根据这个描述,我们知道分治算法是需要用到递归的,二叉树的好多操作天然是采用分治算法的。分治算法需要用到递归,但是用递归的不一定都是分治算法,也可能是动态规划。分治算法和动态规划是一种思想,递归只是实现方案,...原创 2019-10-19 12:03:13 · 811 阅读 · 0 评论 -
Pascal's Triangle (LeetCode) 帕斯卡三角或者叫杨辉三角----动态规划和memoization
Given a non-negative integernumRows, generate the firstnumRowsof Pascal's triangle.In Pascal's triangle, each number is the sum of the two numbers directly above it.Example:Input: 5Output:...原创 2019-10-18 20:11:23 · 350 阅读 · 0 评论 -
镜像二叉树相关问题(通过此问题说明recursive的本质)
目前遇到两类题目:(1)判断一个二叉树是否是镜像的,leetcode有相关题目。可以用递归实现,也可以用非递归实现,思路都比较清晰递归方法判断一个二叉树是否是镜像的,就是判断二叉树的左右子树是否是镜像的。递归中有两个问题需要回答:base case(退出递归的条件):可以直接计算出结果,而不用进一步递归的case。两个节点都是null,return true。或者一个节点是...原创 2019-10-18 14:30:30 · 137 阅读 · 0 评论 -
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.Example 1:Input:[[0,0,0], [0,1,0], [0,0,0]]Output:[[0...原创 2019-10-12 13:45:39 · 243 阅读 · 0 评论 -
Keys and Rooms (LeetCode)
There areNrooms and you start in room0. Each room has a distinct number in0, 1, 2, ..., N-1, and each room may havesome keys to access the next room.Formally, each roomihas a list of keysr...原创 2019-10-12 11:49:24 · 208 阅读 · 0 评论 -
Walls and Gates (LeetCode)-这是一道经典题
You are given am x n2D grid initialized with these three possible values.-1- A wall or an obstacle. 0- A gate. INF- Infinity means an empty room. We use the value231- 1 = 2147483647to repr...原创 2019-10-08 21:09:58 · 316 阅读 · 0 评论 -
二叉树求深度
一般来说有两种方案,一种是采用递归的方式,另一种是采用循环迭代的方式模拟递归的栈。递归方式:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int...原创 2019-09-30 15:02:53 · 188 阅读 · 0 评论 -
用递归解决树的问题(Solve Tree Problems Recursively)
Recursion is one of the most powerful and frequently used techniques for solving tree problems.解决树的问题,递归是最有效也是使用最多的技术。Typically, we can solve a tree problem recursively using a top-down approach o...原创 2019-09-30 09:34:19 · 425 阅读 · 0 评论 -
二叉树水平顺序遍历level-order traversal
有个很有名的算法叫广度优先搜索(Breadth-First Search)BFS,这个算法被用于遍历或者搜索数据结构,比如树或者图。BFS使用到树上就是level-order traversal.进行广度优先搜索算法时,需要用到java的Queue,Queue是继承自Collection的,所以Collection中的方法都是可以用的。Queue常用方法:offer、poll、size...原创 2019-09-30 08:12:45 · 4264 阅读 · 0 评论 -
Number of Islands (LeetCode)
Given a 2d grid map of'1's (land) and'0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume...原创 2019-10-08 21:33:59 · 166 阅读 · 0 评论 -
BFS能解决什么问题?
BFS解决问题的算法流程,伪代码实现:/** * Return the length of the shortest path between root and target node. */int BFS(Node root, Node target) { Queue<Node> queue; // store all nodes which are waiti...原创 2019-10-08 23:56:02 · 951 阅读 · 0 评论 -
Open the Lock (LeetCode)
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn...原创 2019-10-09 14:10:46 · 193 阅读 · 0 评论 -
Daily Temperatures (LeetCode)
Given a list of daily temperaturesT, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which...原创 2019-10-10 11:19:24 · 89 阅读 · 0 评论 -
Flood Fill(LeetCode)
Animageis represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).Given a coordinate(sr, sc)representing the starting pixel (row and colu...原创 2019-10-11 15:10:00 · 172 阅读 · 0 评论 -
Decode String (LeetCode)
Given an encoded string, return its decoded string.The encoding rule is:k[encoded_string], where theencoded_stringinside the square brackets is being repeated exactlyktimes. Note thatkis guar...原创 2019-10-11 19:58:06 · 193 阅读 · 0 评论 -
二叉树的深度遍历
人的大脑真是很强大的器官,可以帮我们潜意识的处理很多事情,让我们会觉得好多“运行”都很简单。就比如二叉树的遍历,我们以先序遍历(Preorder Traversal)举例:如果我们人脑先序遍历上面这个二叉树,我们会很容易的说出:FBADCEGIE,这样正确的结果,人脑帮我们“存储”了很多细节,所以对于我们人脑很容易。但是,对于计算机就没那么容易,计算机得明确的存储细节。我们人脑遍历...原创 2019-09-29 19:31:22 · 340 阅读 · 0 评论