自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

404NotFound

Some people die at 25 and aren't buried until 75.

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

原创 41. 第一个缺失的正数

题目:给一个未排序的数组,找出第一个缺失的正整数。例如,[1,2,0] 返回 3,[3,4,-1,1] 返回 2。你的算法应该在 O(n) 的时间复杂度内完成并且使用常数量的空间。分析:寻找缺失正整数,因此所有1以下数字均无效定义字典用于标记对应数值存在遍历列表,将出现的有效数字加入字典,如果1在列表中,改变相应flag从1开始递增,在字典中查询是否存在,不存在即为缺失正整数代码:class So...

2018-03-31 18:47:29 876 1

原创 leet334递增的三元子序列

题目:分析:选取第一个元素为主元遍历数组,当前元素大于主元,如果当前元素同时大于递增序列栈顶元素,将当前元素压入递增序列栈,如果此时该元素大于前递增序列栈栈顶,则返回真;如果当前元素小于递增序列栈顶元素,清空当前递增序列栈,如果弹出栈的栈顶元素小于前递增序列栈栈顶元素,则替换前递增序列栈当前元素小于主元,替换主元,如果当前递增序列栈长度为2,则添加至前递增序列栈该算法时间复杂度为O(n),占用内存...

2018-03-31 11:15:01 819

原创 leet150. 逆波兰表达式求值

题目:求在 逆波兰表示法 中算术表达式的值。有效的运算符号包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰计数表达。例如: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", &quo

2018-03-31 10:08:53 165

原创 leet202. 快乐数

题目:写一个算法来判断一个数是不是“快乐数”。一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,或是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。案例: 19 是一个快乐数。12 + 92 = 8282 + 22 = 6862 + 82 = 10012 + 02 + 02 = 1分析:分离n的每一位,...

2018-03-31 09:15:53 698

原创 leet169. Majority Element

题目:给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。你可以假设数组是非空的,并且数组中的众数永远存在分析:最简单的方法是使用库函数对数组排序,第⌊ n/2 ⌋个数即为众数,但时间复杂度为O(nlgn)为降低时间复杂度,采用计数方式,并记录最大频次和对应元素,遍历后返回最大频次对应元素,时间复杂度O(n),空间复杂度O(n)代码:class Sol...

2018-03-31 08:50:50 215

原创 leet62. 不同路径

题目:机器人位于一个 m x n 网格的左上角, 在下图中标记为“Start” (开始)。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角,在下图中标记为“Finish”(结束)。问有多少条不同的路径?例如,上图是一个3 x 7网格。有多少可能的路径?注意: m 和 n 的值均不超过 100。分析:动态规划问题对于任意点在右边界和下边界的点移动到目标位置,均有1条路径对于非2中的情况...

2018-03-31 00:36:18 166

原创 leet55. 跳跃游戏

题目:给定一个非负整数数组,您最初位于数组的第一个索引处。数组中的每个元素表示您在该位置的最大跳跃长度。确定是否能够到达最后一个索引。示例:A = [2,3,1,1,4],返回 true。A = [3,2,1,0,4],返回 false。分析:该问题是动态规划问题;如果除尾部元素外的所有元素均为非零元素,则一定可以到达最后一个索引当除尾部元素外,其余元素存在0元素,则要跳过该0元素,在0之前需要有...

2018-03-30 23:48:08 298

原创 leet33. 搜索旋转排序数组

题目:假设按照升序排序的数组在预先未知的某个关键点上旋转。(即 0 1 2 4 5 6 7 将变成 4 5 6 7 0 1 2)。给你一个目标值来搜索,如果数组中存在这个数则返回它的索引,否则返回 -1。你可以假设数组中不存在重复。分析:可以直接用库函数index(),时间复杂度O(n);旋转的后果是列表不再是递增列表,但列表依然有序,可以通过分情况讨论采用二分法,时间复杂度O(lgn)代码:cl...

2018-03-30 22:59:54 390

原创 leet240. 搜索二维矩阵 II

题目:编写一个高效的算法来搜索 m x n 矩阵中的一个目标值。该矩阵具有以下特性:每行的元素从左到右升序排列。每列的元素从上到下升序排列。例如,考虑下面的矩阵:[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30...

2018-03-30 21:14:31 602

原创 leet347. 前K个高频元素

题目:给定一个非空的整数数组,返回其中出现频率前 k 高的元素。例如,给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2]。注意:你可以假设给定的 k 总是合理的,1 ≤ k ≤ 数组中不相同的元素的个数。你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。分析:统计各元素出现频次,时间复杂度O(n)将统计结果按频次排序,内置sort函数,采用归并排序,...

2018-03-30 00:36:59 698

原创 leet75. 分类颜色

题目:给定一个包含红色、白色和蓝色,且含有 n 个元素的数组,对它们进行排序,使得相同颜色的元素相邻,颜色顺序为红色、白色、蓝色。此题中,我们使用整数 0, 1 和 2 分别表示红色,白色和蓝色。注意:不能使用代码库中的排序函数来解决这道题。点击显示进阶问题.进阶:一个相当直观的解决方案是使用计数排序的 two-pass 算法。首先,迭代计算出0,1 和 2 元素的个数,然后重写当前数组。你能想出...

2018-03-30 00:05:12 542

原创 leet17. 电话号码的字母组合

题目:给定一个数字字符串,返回数字所有可能表示的字母组合。下面给出数字到字母的映射(和电话号码一样)。 输入:数字字符串 "23"输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].说明:尽管上面的答案是按字典序排列的,但是你的答案可以是任何顺

2018-03-29 22:21:14 888

原创 leet116. 每个节点的右向指针

题目:给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }填充他的每个 next(下一个)指针,让这个指针指向其下一个右侧节点。如果找不到下一个右节点,则应该将 next(下一个)指针设置为 NULL。初始状态下,所...

2018-03-29 21:27:55 723

原创 leet94. 中序遍历二叉树

题目:给定一个二叉树,返回其中序遍历。例如:给定二叉树 [1,null,2,3], 1 \ 2 / 3返回 [1,3,2].分析:使用递归调用方式中序遍历代码:class Solution(object): def inorderTraversal(self, root): """ :type root: TreeNod...

2018-03-29 20:17:35 157

原创 leet328. Odd Even Linked List

题目:Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in ...

2018-03-29 19:46:39 133

原创 leet2. 两数相加

题目:给定两个非空链表来代表两个非负数,位数按照逆序方式存储,它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。你可以假设除了数字 0 之外,这两个数字都不会以零开头。示例:输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> 8原因:342 + 465 = 807分析:建立新的链表,为返回头指针,需要新建一...

2018-03-29 17:51:32 452

原创 leet198.打家劫舍

题目:你是一个专业的强盗,计划抢劫沿街的房屋。每间房都藏有一定的现金,阻止你抢劫他们的唯一的制约因素就是相邻的房屋有保安系统连接,如果两间相邻的房屋在同一晚上被闯入,它会自动联系警方。给定一个代表每个房屋的金额的非负整数列表,确定你可以在没有提醒警方的情况下抢劫的最高金额。分析:这是一个动态规划问题为满足题目要求,不触碰警报,不能连续两家特例为1)空数组[],输出0; 2) 列表长度为1,输出列表...

2018-03-28 21:32:19 165

原创 leet20. 有效的括号

题目:给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。括号必须以正确的顺序关闭,"()" 和 "()[]{}" 是有效的但是 "(]" 和 "([)]" 不是。分析:如果字符串长度为奇数,则一定存在无效括号;为方便处理,将括号映射为方便处理的数字,使对应括号之和恒定;将给定字符串使用映射表转换为对应数字列表;定义一个栈,遍历2中得到的列表,当栈为空时,...

2018-03-28 19:27:41 1104

原创 leet102. 二叉树的层次遍历

题目:给定一个二叉树,返回其按层次遍历的节点值。 (即zhu'ceng'de,从左到右访问)。例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回其层次遍历结果为:[ [3], [9,20], [15,7]]分析:直接采用BFS方式遍历,记录不为空的节点值代码:class Solut...

2018-03-27 23:02:25 115

原创 leet101. 对称二叉树

题目:给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称)。例如,这个二叉树 [1,2,2,3,4,4,3] 是对称的。 1 / \ 2 2 / \ / \3 4 4 3 但是下面这个 [1,2,2,null,3,null,3] 则不是: 1 / \ 2 2 \ \ 3 3 说明:如果你可以递归地和迭代地解决它就...

2018-03-27 22:36:51 3164

原创 leet98. 验证二叉搜索树

题目:给定一个二叉树,判断其是否是一个有效的二叉搜索树。一个二叉搜索树有如下定义:左子树只包含小于当前节点的数。右子树只包含大于当前节点的数。所有子树自身必须也是二叉搜索树。示例 1: 2 / \ 1 3二叉树[2,1,3], 返回 true.示例 2: 1 / \ 2 3二叉树 [1,2,3], 返回 false.分析:使用DFS方式遍历每次递归时,确...

2018-03-27 20:24:06 161

原创 leet234.回文链表

题目:请检查一个链表是否为回文链表。进阶:你能在 O(n) 的时间和 O(1) 的额外空间中做到吗?分析:该题不够严谨,要满足时间、空间复杂度要求,应该要求链表无环回文链表要求正逆方向对应位置节点值相同不考虑时间、空间复杂度,可以一次遍历链表,比较正反列表对应节点值是否一致,这样时间复杂度满足O(n),但空间复杂度为O(n)为使空间复杂度满足O(1)只可以新增常数级内存空间,而链表为单向,无法逆向...

2018-03-27 19:20:54 443

原创 leet36. 有效的数独

题目:判断一个数独是否有效,根据:Sudoku Puzzles - The Rules。数独部分填了数字,空的部分用 '.' 表示。一个部分填充是有效的数独。说明:一个有效的数独(填了一部分的)不一定是可解的,只要已经填的数字是有效的即可。分析:数独中数字有效是指同一行中、同一列中、所在的3×3格中不能有重复出现的数字;行优先方式遍历,并统计各行中每个数字出现次数使用zip函数生成列优先数组,遍历...

2018-03-26 19:16:59 805

原创 leet791. Custom Sort String

题目:S and T are strings composed of lowercase letters. In S, no letter occurs more than once.S was sorted in some custom order previously. We want to permute the characters of T so that they match the ...

2018-03-25 21:29:03 101

原创 leet806. Number of Lines To Write String

题目:We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units,...

2018-03-25 20:09:17 206

原创 leet235. Lowest Common Ancestor of a Binary Search Tree

题目:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between...

2018-03-25 18:57:18 93

原创 112. 路径总和

题目:给定一棵二叉树和一个总和,确定该树中是否存在根到叶的路径,这条路径的所有值相加等于给定的总和。例如:给定下面的二叉树和 总和 = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 ...

2018-03-25 17:46:02 957

原创 leet654. Maximum Binary Tree

题目:Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:The root is the maximum number in the array. The left subtree is the maximum tree constructed f...

2018-03-25 16:18:25 108

原创 leet807. Max Increase to Keep City Skyline

题目:In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts ...

2018-03-25 14:54:13 364

原创 leet479. Largest Palindrome Product

题目:Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337.Example:Input: 2Output: 987Explanat...

2018-03-25 14:16:05 176

原创 leet189. Rotate Array

题目:Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 分析:将右侧元素旋转k次至左端当k=0时,数组无需旋转当k=n时,数组无需旋转当k>n时,相当于...

2018-03-25 01:00:41 103

原创 leet278. First Bad Version

题目:You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on ...

2018-03-25 00:06:59 189

原创 leet204. Count Primes

题目:Count the number of prime numbers less than a non-negative number, n.分析:对于小于3整数特殊处理定义判断素数函数代码:class Solution(object): def countPrimes(self, n): """ :type n: int :rtype: ...

2018-03-25 00:02:16 113

原创 leet125. Valid Palindrome

题目:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palin...

2018-03-24 22:15:55 98

原创 leet414. Third Maximum Number

题目:Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).分析:通过集合运算获得所有元素唯一的列表,时间复杂度为O...

2018-03-24 21:35:16 99

原创 leet532. K-diff Pairs in an Array

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

2018-03-24 21:02:26 103

原创 leet28. Implement strStr()

题目:Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.分析:尽量不使用库函数特例(1)为两字符串相等(2)目标字符串为空(3)源字符串长度小于目标字符串代码:class Solution(object)...

2018-03-24 19:53:59 147

原创 leet581. Shortest Unsorted Continuous Subarray

题目:Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You need to fi...

2018-03-24 19:17:40 146

原创 leet190. Reverse Bits

题目:Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 0011100101...

2018-03-24 18:14:12 114

原创 leet605. Can Place Flowers

题目:Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.Giv...

2018-03-24 15:12:23 131

空空如也

空空如也

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

TA关注的人

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