自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(46)
  • 收藏
  • 关注

原创 leetcode第23题(longest-consecutive-sequence)

题目:Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is

2018-01-19 16:59:15 207

原创 leetcode第22题(sum-root-to-leaf-numbers)

题目:Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. An example is the root-to-leaf path1->2->3which represents the number123. Find the

2018-01-19 15:27:36 173

原创 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

2018-01-19 15:03:17 218

原创 leetcode第20题(palindrome-partitioning)

题目: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  [

2018-01-17 20:53:25 190

原创 leetcode第19题(palindrome-partitioning-ii)

题目:Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s ="aab",Return1since

2018-01-17 20:25:49 220

原创 leetcode第18题(clone-graph)

题目:Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors. OJ's undirected graph serialization:Nodes are labeled uniquely. We use#as a separator fo

2018-01-15 15:00:15 160

原创 leetcode第17题(gas_station)

题目:There are N gas stations along a circular route, where the amount of gas at station i isgas[i]. You have a car with an unlimited gas tank and it costscost[i]of gas to travel from station i 

2018-01-15 13:58:05 196

原创 leetcode第16题(candy)

题目:There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at

2018-01-15 13:22:35 214

原创 leetcode第15题(single-number)

题目:Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without

2018-01-14 20:28:35 164

原创 leetcode第14题(single-number-ii)

题目:Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it wi

2018-01-14 20:21:17 201

原创 leetcode第13题(copy-list-with-random-pointer)

题目:A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:跟剑指offer25道题一样代码:

2018-01-14 19:34:54 111

原创 leetcode第12题(word-break)

题目:Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens ="leetcode",dict =[

2018-01-14 18:52:37 175

原创 leetcode第11题(word-break-ii)

题目:Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, giv

2018-01-14 18:24:41 209

原创 leetcode第10题(linked-list-cycle)

题目:Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space?思路:跟上面题一样,只是不用求相遇点,第一次相遇就可以判断有环。代码:/** * Definition for singly-

2018-01-14 14:56:47 149

原创 leetcode第9题(linked-list-cycle-ii)

题目:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up:Can you solve it without using extra space?思路:跟之前剑指offer的题一样。二次相遇的点就是环的入口代

2018-01-14 14:50:33 128

原创 leetcode第8题(reorder-list)

题目:Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You must do this in-place without altering the nodes' values. For example,Given{1,2,3

2018-01-14 14:27:08 283

原创 leetcode第7题(binary-tree-preorder-traversal)

题目:Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree{1,#,2,3},  1 \ 2 / 3return[1,2,3]. Note: Recursiv

2018-01-12 20:40:56 131

原创 leetcode第6题(binary-tree-postorder-traversal)

题目:Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree{1,#,2,3},  1 \ 2 / 3return[3,2,1]. Note: Recursi

2018-01-12 20:27:03 214

原创 leetcode第5题(insertion-sort-list)

题目:Sort a linked list using insertion sort.思路:新建一个新链表,每遍历一个原始链表,就排序之后插在新链表的相应位置。代码:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; *

2018-01-12 19:53:46 142

原创 leetcode第4题(sort-list)

题目:Sort a linked list in O(n log n) time using constant space complexity. 思路:题目要求复杂度为O(nlogn),故可以考虑归并排序的思想。 归并排序的一般步骤为: 1)将待排序数组(链表)取中点并一分为二; 2)递归地对左半部分进行归并排序; 3)递归地对右半部分进行

2018-01-12 18:38:07 287

原创 leetcode第3题(max-points-on-a-line)

题目:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.思路:需要两重循坏。第一重是循环遍历初始结点,第二重是循环遍历剩余结点。对于每个结点啊,如果b重合a,则以a为初始的节点的所有直线的点数加1,如果两个节点不重合,则确定一条直线,则a与b确

2018-01-12 17:43:54 180

原创 leetcode第2题(evaluate-reverse-polish-notation)

題目:Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are+,-,*,/. Each operand may be an integer or another expression. Some examples:  ["2", "1"

2018-01-12 15:39:22 166

原创 leetcode第1题(minimum-depth-of-binary-tree)

题目:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.思路:采用递归的思想,需要注意的是最小深度与最大深度的区别:对于最大深

2018-01-12 15:01:27 155

原创 剑指offer67题(机器人的运动范围)

题目:地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?思路:在上一题的基础上加个判断即可代码

2018-01-11 18:57:12 153

原创 剑指offer66题(矩阵中的路径)

题目:请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩

2018-01-11 17:17:12 162

原创 剑指offer65题(滑动窗口的最大值)

题目:给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5

2018-01-11 14:52:58 162

原创 剑指offer64题(数据流中的中位数)

题目:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。思路:定义一个列表,插入数之后,排序。然后从中找出中位数即可。(本题最好用大小堆做)代码:import java.util.ArrayList;import java.util.Arrays

2018-01-11 13:49:23 122

原创 剑指offer63题(而叉搜索树第k个结点)

题目:给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。思路:中序遍历遍历二叉搜索树就是排序的,第k个结点就是第k大的。代码:/*public class TreeNode { int val = 0; TreeNode left = null; TreeNode

2018-01-10 20:40:16 158

原创 剑指offer62题(序列化二叉树)

题目:请实现两个函数,分别用来序列化和反序列化二叉树思路:用前序遍历使其序列化,将前序遍历按照根左右转化成二叉树的形式(反序列化)代码:/*public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int va

2018-01-10 20:00:40 149

原创 剑指offer61题(把二叉树打印成多行)

题目:从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。思路:用队列,每次打印一层。(需要两个变量:一个用于存当前没有打印的节点数,另一个用于存下层的节点数)代码:import java.util.ArrayList;import java.util.*;/*public class TreeNode { int val = 0; TreeN

2018-01-10 19:15:46 153

原创 剑指offer60题(按之字形顺序打印二叉树)

题目:请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。思路:用两个栈实现,一个栈存放父节点,一个栈存放子节点,注意偶数层先保存右节点,再保存左结点,奇数层先保存左结点,再保存右节点。代码:import java.util.ArrayList;import java.util.Stack

2018-01-10 16:51:42 136

原创 剑指offer59题(对称的二叉树)

题目:请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。思路:根据前序遍历(根、左、右)和对称的前序遍历(根、右、左)即可判断。只要左子树的左结点和右子树的右结点,左子树的右节点和右子树的左结点相等,就是对称的。代码:/*public class TreeNode { int val = 0; TreeN

2018-01-10 14:42:32 220

原创 剑指offer58题(二叉树的下一个结点)

题目:给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。思路:该结点有右子树,那么它的下一结点就是右子树的最左子节点。若该节点没有右子树,但是该节点是父节点的左子节点,则下一节点是父节点,若该节点是父节点的右子结点,则需要沿着父节点的指针一直向上遍历,直到找到他父节点的左子节点为止。代码:/*pu

2018-01-10 14:03:45 161

原创 剑指offer57道题(删除链表中的重复结点)

题目:在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5思路:分两种情况讨论:第一当前结点是重复结点,则需要删除重复结点才能调用deleteDuplication函数。第二当前结点不是重复结点,则直接调用deleteDuplication函数,并返回头指针。代码:

2018-01-09 21:47:16 123

原创 剑指offer56题(链表中环的入口节点)

题目:一个链表中包含环,请找出该链表的环的入口结点。思路:第一步,找环中相汇点。分别用p1,p2指向链表头部,p1每次走一步,p2每次走二步,直到p1==p2找到在环中的相汇点。第二步,找环的入口。接上步,当p1==p2时,p2所经过节点数为2x,p1所经过节点数为x,设环中有n个节点,p2比p1多走一圈有2x=n+x; n=x;可以看出p1实际走了一个环的步数,再让p2指向链表头部,p1

2018-01-09 21:18:34 150

原创 剑指offer55题(字符流中第一个不重复的字符)

题目:请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。思路:用一个hash表或者数组,其值或下标对应字符的ASCII码值,值为出现的次数。最后判断第一次出现一次的,即为本题的返回值代码:public class Solu

2018-01-09 20:17:01 194

原创 剑指offer54题(表示数值的字符串)

题目:请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是思路:将String转化为double类型,如果成功转换即该字符串是数值,否则不是。代码:public class Solution

2018-01-09 16:42:42 126

原创 剑指offer53题(正则表达式匹配)

题目:请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配思路:当模式中的第二个字符不是“*”时:1、如果字符串第一个字符和模式中的

2018-01-09 13:56:19 163

原创 剑指offer52题(构建乘积数组)

题目:给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。思路:B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]将B[i]看成两部分相乘,从i那里断开。对于前面的我们用正三角计算,后面部分用倒三角计算。代

2018-01-08 19:24:34 175

原创 剑指offer51题(数组中重复的数字)

题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。思路:数组中的数字都在0到n-1范围内,如果这个数组中没有重复数字,那么当数组排序后刚好为下标。由于数组中有重复数字,有些位

2018-01-08 15:33:22 474

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除