自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

Ling的博客

在探索中努力前行

  • 博客(96)
  • 收藏
  • 关注

原创 剑指Offer__20、包含min函数的栈

题目描述定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。思路:本题需要设置三个变量,分别为用于存储数据的主栈、用于存储当前主栈对应最小元素的辅栈以及主栈目前的最小值。之所以需要辅栈是因为当主栈元素数量变化时,其对应的最小元素可能也不同。举个例子,假如主栈目前为空,则辅栈为空,最小值为None;当我们压入第一个元素7,这个时候主栈...

2019-06-26 10:05:54 292

原创 剑指Offer__19、顺时针打印矩阵

题目描述输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.思路:本题的一个核心问题是如何实现顺时针打印,解决了这个问题,这道题就会迎刃而解,我参考了 风中摇曳的叶子 的...

2019-06-26 10:05:45 266

原创 剑指Offer__18、二叉树的镜像

题目描述操作给定的二叉树,将其变换为源二叉树的镜像。思路:这道题用递归做最合适不过,其思路很简单,既然是递归那函数的开始一定有跳出递归的条件,条件就是当遍历到树节点为空时,就跳出递归。剩下的就是交换根节点左右子节点了,在交换完毕之后要进入到子节点的子树中对子树再进行左右节点的交换,如此循环下去,代码如下:Solution:Python# -*- coding:utf-8 ...

2019-06-26 10:05:34 261

原创 剑指Offer__17、树的子结构

题目描述输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)思路:其实这道题有了图之后思路就会更清晰:图片来自于博客园 凌风1205,具体博客参见reference。看图之后就会首先捕捉到一点:如果要判断B是A的子结构,那么必须从B的根节点值来与A中节点进行比对。当B的根节点与A树中某个节点相同时,再判断A的节点的左右子树与B的根节点左...

2019-06-26 10:05:26 135

原创 剑指Offer__16、合并两个排序的链表

题目描述输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。思路:这道题的思路有点类似于归并排序,即依次比较两个链表的头结点,将较小的头结点取出来,被取头结点所在的链表更新头结点之后再与另外的链表进行头结点大小比较,直到其中一个链表节点被取完为空,然后将剩下的不为空的链表接在排序好的链表后面。Solution:Python# -*-...

2019-06-26 10:05:17 117

原创 剑指Offer__15、反转链表

题目描述输入一个链表,反转链表后,输出新链表的表头。思路:首先需要判断是否有特殊情况,这里是链表为空的情况,如果链表为空,直接返回None;之后可以采用三个指针对链表进行反转,其思路就是从头结点开始,依次断开链表节点,将节点的指针方向反转。相当于将链表一分为二,第一部分存放着当前的反转链表,另外一部分存放着未反转的链表,依次将未反转链表的头结点断开,然后将断开的头结点指向已经反转的链表...

2019-06-25 15:56:05 188

原创 剑指Offer__14、链表中倒数第K个节点

题目描述输入一个链表,输出该链表中倒数第k个结点。思路:本题其实有很多思路,例如:1、先将链表翻转,再取第K个元素;2、将链表元素压入栈中,弹出第K个;3、先计算链表总体长度,再取第n-k个;而这里用了第四种方法,也是链表的一种常用操作:快慢指针法,即快指针先走K-1步,记住是K-1步,然后慢指针开始走,快指针走到链表末尾的时候,慢指针正好走在链表的倒数第K个元素上。快慢指针法相比于前...

2019-06-25 15:55:57 88

原创 剑指Offer__13、调整数组顺序使奇数位于偶数前面

题目描述输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。思路:这道题的思路也很简单,创建两个列表,分别存储奇数和偶数,然后遍历整个数组,将奇数/偶数分别存储在对应的数组中,最后返回奇数数组和偶数数组的合并即可Solution:Python# -*- co...

2019-06-25 15:55:48 110

原创 剑指Offer__12、数值的整数次方

题目描述给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。思路:有两种方法来解决这个问题:1、第一种是暴力方法,就是用for循环,一个循环base乘一遍,这样就会得到结果,需要判断exponent是否为负数,如果为负数,那么求得的结果需要去倒数。2、第二种方法是对第一种方法进行了优化,其思路是数学上的一个定理,可以简化...

2019-06-25 15:55:41 104

原创 剑指Offer__11、二进制中1的个数

题目描述输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。思路:先来看一下wangdongli_1993博客中总结的原码、反码、补码的知识:数字在计算机中都是二进制来存在,以字节为单位,一个字节是8位,这个题目是int类型就是32位原码:最高位是符号位,剩下的表示机器数的值+1:0000 0001-1:1000 0001反码:对于整数,反码同原码,对于...

2019-06-25 15:55:33 147

原创 剑指Offer__10、矩形覆盖

题目描述我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?思路:乍一看,这道题貌似无从下手,但其实将图画出来之后你就会豁然开朗,下图是本题的解题思路图片,摘自-Billy的博客,原文链接见reference,所以看到这个图之后就发现,这其实又是斐波那契数列的一种变形,n=1时,只有一种解法,n=2时,有两种解...

2019-06-25 11:08:17 144

原创 剑指Offer__9、变态跳台阶

题目描述一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。思路:由于青蛙可以跳任意一阶台阶,所以青蛙可以从n级台阶之前的任意一级台阶跳上第n级台阶,所以其跳上n级台阶的跳法应该是n级台阶之前所有跳法的总和,再加上其直接跳上n级台阶这一种跳法。Solution:Python# -*- coding:utf-8 -*...

2019-06-25 11:08:07 130

原创 剑指Offer__8、跳台阶

题目描述一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。思路:青蛙跳台阶是斐波那契数列的抽象,只是初始条件有所不同。当青蛙跳上n级台阶时,由于它只能跳一级台阶和二级台阶,其跳上n级台阶只有两种方式,从n-1级台阶跳上来或者从n-2级台阶跳上来,那么跳上n级台阶的跳法就是跳上n-1级台阶跳法和跳上n-2级台阶跳法的和,...

2019-06-25 11:07:53 120

原创 剑指Offer__7、斐波那契数列

题目描述大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。n<=39思路:斐波那契数列的第一项和第二项分别为1、1,从第三项开始符合公式:f(n) = f(n-1) + f(n-2)Solution:Python(1)# -*- coding:utf-8 -*-class Solution: def...

2019-06-25 11:07:45 230

原创 剑指Offer__6、旋转数组的最小数字

题目描述把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。思路:整个数组是一个非减排序的数组,那么旋转数组之后最小元素落入的区间就只有三种情况:1、正好是数组的中...

2019-06-25 11:07:27 97

原创 剑指Offer__5、用两个栈实现队列

题目描述用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。思路:队列的特性是先进先出,栈的特性是先进后出,那么用栈来实现队列就需要考虑数据能否实现先进先出的特性。显然,一个栈肯定是无法实现的,当有两个栈时,一个A栈为数据输入,一个B栈作为数据输出;现有两个元素a、b,如果a、b入栈,则将元素依次压入A栈,a先入A,b后入A,当要实现队列先进先出的特性时...

2019-06-20 21:37:08 83

原创 剑指Offer__4、重建二叉树

题目描述输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。思路:根据二叉树的中序遍历和前/后序遍历可以对二叉树进行重构,需要注意的一点是在重构二叉树的时候必须有二叉树的中序遍历,若只有前序和后序遍历是无...

2019-06-20 19:56:44 78

原创 剑指Offer__3、从尾到头打印链表

题目描述输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。思路:1、第一种方法是正常从头到尾打印链表,存储在列表或者栈中,如果存储在列表中就翻转一下列表,存储在栈中就直接pop元素即可(利用栈后进先出特性)2、将链表翻转,然后输出链表元素Solution:Python(1)# -*- coding:utf-8 -*-# class ListNo...

2019-06-19 09:29:41 95

原创 剑指Offer__2、替换空格

题目描述请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。思路:用Python来做的话非常简单,将字符串根据空格分割形成列表,然后再用‘%20’做列表元素的连接,利用join函数连接列表形成字符串。Solution:Python# -*- coding:utf-8 ...

2019-06-18 14:33:31 90

原创 剑指Offer__1、二维数组的查找

题目描述在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。思路:因为矩阵从左到右、从上到下递增,因此二维数组的最后一列元素都是其对应行的最大元素,因此我们可以根据target与每一行的末尾元素进行比较,如果target大于末尾元素,则用下一行末尾...

2019-06-18 14:20:18 109

原创 Leetcode_Dynamic_Programming -- 91. Decode Ways [medium]

A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given anon-emptystring containing only digits, determine th...

2019-04-27 23:03:11 118

原创 Leetcode_Dynamic_Programming -- 5. Longest Palindromic Substring [medium]

Given a strings, find the longest palindromic substring ins. You may assume that the maximum length ofsis 1000.给定一个字符串s,找到s的最长回文子串,假设s的最大长度是1000Example 1:Input: "babad"Output: "bab"Note: ...

2019-04-21 17:03:33 89

原创 Leetcode_Dynamic_Programming -- 300. Longest Increasing Subsequence [medium]

Given an unsorted array of integers, find the length of longest increasing subsequence.给定一个未排序的整数数组,返回该数组的最长递增子序列的长度。Example:Input: [10,9,2,5,3,7,101,18]Output: 4 Explanation: The longest inc...

2019-04-21 11:32:58 118

原创 Leetcode_Dynamic_Programming -- 322. Coin Change [medium]

You are given coins of different denominations and a total amount of moneyamount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money ...

2019-04-19 16:47:09 109

原创 Leetcode_Dynamic_Programming -- 198. House Robber [easy]

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 that adjacent house...

2019-04-15 08:38:15 135

原创 Leetcode_Dynamic_Programming -- 746. Min Cost Climbing Stairs [easy]

On a staircase, thei-th step has some non-negative costcost[i]assigned (0 indexed).Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of ...

2019-04-14 22:18:54 175

原创 Leetcode_Dynamic_Programming -- 70. Climbing Stairs [easy]

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-04-12 15:48:57 157

原创 Leetcode_Dynamic_Programming --121. Best Time to Buy and Sell Stock [easy]

Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock),...

2019-04-12 14:26:37 171

原创 Leetcode_Linked_List --19. Remove Nth Node From End of List [medium]

Given a linked list, remove then-th node from the end of list and return its head.给定一个单向链表,移除从链表的尾节点开始数的第n个节点并返回链表Example:Given linked list: 1-&gt;2-&gt;3-&gt;4-&gt;5, and n = 2.After removi...

2019-03-05 07:56:29 94

原创 Leetcode_Linked_List --24. Swap Nodes in Pairs [medium]

Given alinked list, swap every two adjacent nodes and return its head.You maynotmodify the values in the list's nodes, only nodes itself may be changed.给定一个链表,交换每两个相邻节点,并返回其头部节点你不能改变链表中的值,只能改...

2019-03-04 22:07:19 115

原创 Leetcode_Linked_List --2. Add Two Numbers [medium]

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i...

2019-02-16 17:59:34 164

原创 Leetcode_Array -- 697. Degree of an Array [easy]

Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.Your task is to find the smallest possible length of a ...

2018-11-21 23:18:16 155

原创 Leetcode_Array -- 888. Fair Candy Swap [easy]

Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j] is the size of the j-th bar of candy that Bob has.Since they are friends, they wo...

2018-11-20 20:35:09 132

原创 Leetcode_Array -- 448. Find All Numbers Disappeared in an Array [easy]

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.Coul...

2018-11-20 09:24:59 86

原创 Leetcode_Array -- 724. Find Pivot Index [easy]

Given an array of integers nums, write a method that returns the "pivot" index of this array.We define the pivot index as the index where the sum of the numbers to the left of the index is equal to ...

2018-11-17 15:14:10 125

原创 Leetcode_Array -- 832. Flipping an Image [easy]

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.To flip an image horizontally means that each row of the image is reversed.  For examp...

2018-11-17 13:57:40 129

原创 Leetcode_Array -- 532. K-diff Pairs in an Array [easy]

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in...

2018-11-12 23:20:11 141

原创 Leetcode_Array -- 747. Largest Number At Least Twice of Others [easy]

 In a given integer array nums, there is always exactly one largest element.Find whether the largest element in the array is at least twice as much as every other number in the array.If it is, r...

2018-11-08 21:03:05 104

原创 Leetcode_Array -- 674. Longest Continuous Increasing Subsequence [easy]

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).给定一个未排序的整数数组,找到最长的连续递增子序列,返回其长度Example 1:Input: [1,3,5,4,7]Output: 3Explanation: T...

2018-11-07 21:43:03 173

原创 Leetcode_Array -- 169. Majority Element [easy]

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 that the array is non-empty and the majority element alwa...

2018-11-07 20:43:04 108

空空如也

空空如也

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

TA关注的人

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