自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

转载 leetcode分类

利用堆栈: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心) http://oj.leetcode.com/problems/valid-parentheses/

2015-04-12 10:36:35 636

原创 #Android##bug#ConcurrentModificationException

今天出现ConcurrentModificationException 原因如下  App有个功能是添加项目到list里 如果已存在就数量加1 不存在就新建一个对象 类似购物车  if(laundryItemList.size()==0){ Log.v("lisa", "空篮没加过"); LaundryItem

2016-01-15 21:52:04 446

原创 https://leetcode.com/problems/search-in-rotated-sorted-array/

https://leetcode.com/problems/search-in-rotated-sorted-array/ 1.寻找初始的index 也就是rotate的情况  2. 在顺序的列表里 二分查找  如果没有 返回-1 如果有 再减去startindex 注意 减去startindex可能会出现负数的情况 在加一个len(list)再取len(list)余 class Sol

2015-05-23 13:32:04 568

原创 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/

https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ 和上一题没有区别 把值改成TF class Solution: # @param {integer[]} nums # @param {integer} target # @return {boolean} def search(se

2015-05-23 13:31:29 596

原创 https://leetcode.com/problems/search-insert-position/

https://leetcode.com/problems/search-insert-position/ 依然二分法 如果能找到直接返回index 如果找不到最后会逼近到s 那么target就应该插入到e所在的位置  class Solution: # @param {integer[]} nums # @param {integer} target # @ret

2015-05-23 13:10:28 490

原创 https://leetcode.com/problems/sqrtx/

https://leetcode.com/problems/sqrtx/ 这道题 由于数字可能很大 先找大约的值 以10倍数递增 确定范围  然后二分法  如果刚好是完全平方根 就在while里 return (s+e)/2 否则 最后会确定到s,e 其中e=s+1 而真正的平方根是在s和e之间的float  所以s**2 这种情况下 直接返回s  class Solution:

2015-05-23 13:05:28 477

原创 https://leetcode.com/problems/search-for-a-range/

https://leetcode.com/problems/search-for-a-range/ 二分就能做  图方便用了list.index(target)  list.count(target) 如果index没有找到会抛出异常 用了try class Solution: # @param {integer[]} nums # @param {integer} tar

2015-05-23 13:01:28 398

原创 https://leetcode.com/problems/search-a-2d-matrix/

https://leetcode.com/problems/search-a-2d-matrix/ 先看是不是超出matrix范围  再看是哪行  最后行内for循环查找 (因为是排序好的 二分法也可以) class Solution: # @param {integer[][]} matrix # @param {integer} target # @return

2015-05-23 12:57:58 644

原创 https://leetcode.com/problems/gas-station/

https://leetcode.com/problems/gas-station/ 这道题暴力的解释可以的 但是超时  def canCompleteCircuit(gas, cost): length=len(gas) for i in range(length): sgas=0 scos=0 for j in range(l

2015-05-23 11:12:37 478

原创 https://leetcode.com/problems/candy/

https://leetcode.com/problems/candy/ 这道题的想法是 1.左右数字都大于等于它的 算作是凹型的 包括连续相同的几个数组中间的那几个      其中最左最右也可以这样算 不过因为只有一端有数 按一端算就行      将这些凹点赋值为1 其他为0 2. 从这样的凹点开始 到顶点 (大于等于两端且为0的点)都从凹点向上(左右)加一 3.到顶点时 要取max

2015-05-23 10:56:16 595

原创 https://leetcode.com/problems/single-number-ii/

https://leetcode.com/problems/single-number-ii/ 和https://leetcode.com/problems/single-number/相同  range间隔改成3就行了 class Solution: # @param {integer[]} nums # @return {integer} def sing

2015-05-14 11:49:05 627

原创 https://leetcode.com/problems/single-number/

https://leetcode.com/problems/single-number/ 很简单 排序之后 每隔一个range 如果这个值和下一个不相等 就是single  因为single可能是最后一个 给list append一个字符 最后一个也可以那样比较了 class Solution: # @param {integer[]} nums # @return

2015-05-14 11:46:40 628

原创 ?????????https://leetcode.com/problems/largest-rectangle-in-histogram/

https://leetcode.com/problems/largest-rectangle-in-histogram/ 递归:(然而会超时 而且height很长时迭代深度太高) 1.找最低点 计算最低点*总长的V 2.分为左右两个list 分别再找最低  3.把所有的V放在list里 找最大的得出答案  def a(height,list): if height==[]:

2015-05-13 23:39:20 369

原创 https://leetcode.com/problems/longest-valid-parentheses/

https://leetcode.com/problems/longest-valid-parentheses/ 最开始是每次配对成功(大雾 就讲这一对赋值为0 最后看0串最长的一组的长度  发现超时  然后 换了方法 放了三个list alist最为栈  temp存放入栈元素的index indexlist存放配对成功的元素的index 一旦遇到配对成功的右括号 就把右括号的in

2015-05-13 23:24:36 620

原创 https://leetcode.com/problems/trapping-rain-water/

https://leetcode.com/problems/trapping-rain-water/ 找最高点A 然后 分左右 向左找次高B 中间点和min(A,B)的差就是V 然后最高变B 再向左  右边也一样  第一次的方法很蠢 写了三个函数 分别找最高点 计算V 而且还把每次的高点存在list里 结果超时 后来改了一下 一边找一边算一边加 旧的 def vol(height,

2015-05-13 23:19:20 417

原创 https://leetcode.com/problems/valid-parentheses/

https://leetcode.com/problems/valid-parentheses/ 也是堆栈  左括号全部入栈 如果遇到任何一种右括号  弹出一个元素 如果匹配 就继续 如果不匹配 就直接return False 当结束时 栈应该是空的  如果不为空 说明多很多做括号 也要False  class Solution: # @param {string} s

2015-05-13 23:10:17 512

原创 https://leetcode.com/problems/evaluate-reverse-polish-notation/

https://leetcode.com/problems/evaluate-reverse-polish-notation/ 堆栈 class Solution: # @param {string[]} tokens # @return {integer} def evalRPN(self, tokens): list=[] for i

2015-05-13 23:06:35 595

原创 https://leetcode.com/problems/valid-palindrome/

https://leetcode.com/problems/valid-palindrome/ 去掉非字母数字 统一小写  然后比较它和它的逆序是否相同 我的方法没问题 但是超时  看discuss里面用了filter 我还不太会  class Solution: # @param {string} s # @return {boolean} def isPali

2015-05-13 22:30:46 580

原创 https://leetcode.com/problems/length-of-last-word/

https://leetcode.com/problems/length-of-last-word/ 简单 用string.split(' ') 有可能有连续空格 因此从后往前找第一个非空值计算长度 class Solution: # @param {string} s # @return {integer} def lengthOfLastWord(self, s)

2015-05-13 22:28:11 535

原创 https://leetcode.com/problems/longest-palindromic-substring/

https://leetcode.com/problems/longest-palindromic-substring/ 这道题我分了两类 单字符中心和双字符中心  单字符的 遍历每一个字符 找以它为中心左右两个是否相等  把左边的起点的index和总长度记住  双字符 寻找两个连续的字符作为中心 方法相同  其中单中心的 length=min(i,len(s)-i-1) 就是看中心离哪

2015-05-13 22:24:14 672

原创 https://leetcode.com/problems/zigzag-conversion/

https://leetcode.com/problems/zigzag-conversion/ 要锯齿状的输出  首先分组  如果nRows为m 那么纵列 m 个斜 m-2个 因此 每2m-2个字符是一组 每组占据m-1列 并根据这样算出要初始化的数组list的大小 1       A     2     8 B     3

2015-05-13 22:21:39 419

原创 https://leetcode.com/problems/simplify-path/

https://leetcode.com/problems/simplify-path/ 还是比较简单的 1.以/为分隔符 将字符串分开  2.所有的 . 都变为‘’空串 3.所有的 .. 都变为‘’空串   并且把..前第一个不为空的串设置为空    因为..表示上一级  4.删除所有空串  5.用/再把串连起来 (如果只有空串 那么意味着是根目录 ) class Soluti

2015-05-07 22:55:04 452

原创 https://leetcode.com/problems/multiply-strings/

https://leetcode.com/problems/multiply-strings/ 因为数字很大 不能进行普通乘法  想到用分解乘法的步骤  长串和单位数字的乘法 就是 按位做乘法  积+进位%10=得数  积+进位/10=进位 最后在单独把最后一次的进位放在最前面 singlemul()就做了这件事 —————————————————— 然后得到每一次的

2015-05-07 22:40:03 417

原创 https://leetcode.com/problems/text-justification/

https://leetcode.com/problems/text-justification/ 思路是这样的: 1.先分行 2.将这一行输出  分行的方法是 前i个字符串和i-1个空格的长度小于Max  i自加 如果除了这i个字符串没有别的了 就用last()方法输出(输出最后一行的方法) 如果还有后续字符 就用 output()方法输出 判断是否为最后一行用了needle变量

2015-05-07 22:19:51 406

原创 https://leetcode.com/problems/implement-strstr/

https://leetcode.com/problems/implement-strstr/ 很简单 先找needle串的头和haystack相同的位置  如果连头都不一样就肯定不是  相同再比较后面的每一位  有一些特殊情况  长度needle或者haystack可能是空串 单独处理  class Solution: # @param {string} haystack

2015-05-07 22:15:02 412

原创 第一次深深感到进步

https://leetcode.com/problems/count-and-say/ 就是这道题 这是第一次深深感到进步  三个月前第一次打开leetcode的时候 就是这道题  感觉自己根本不会 然后由于畏难心理就没再看  一个月前开始刷leetcode 然后一道一道过来  虽然才刷了30道 做到这道题时候发现  这道题原来很简单啊!  以前想了很久的题看一眼就知道怎么做了 

2015-05-04 22:44:55 378

原创 https://leetcode.com/problems/count-and-say/

https://leetcode.com/problems/count-and-say/ 只分两种情况  1.连续数字 ni  2.单独数字 1i 将每次的数字处理之后放在数组里  处理第n个就把第n-1个拿来处理  在结尾时 可能出现 倒数两个是相同的数字 比如(1211)11相同  或者不同数字  两种分别处理  class Solution: # @param {i

2015-05-04 22:42:51 494

原创 https://leetcode.com/problems/container-with-most-water/

https://leetcode.com/problems/container-with-most-water/ 这道题做错了太多次  第一次是理解错题目 以为是两块及其中间的模板围成的体积 要找两个之间的最小高度  后来知道只要两块的min即可 第二次是算法不对  两个指针一个最左 一个最右 固定a b里面走 找到比b初始值大的值 再固定b 找a 这种做法有错 可能会出现a,b之间

2015-05-03 19:53:50 417

原创 https://leetcode.com/problems/max-points-on-a-line/

https://leetcode.com/problems/max-points-on-a-line/ 最开始的方法是两个点 然后计算出直线的系数 然后看哪些点在直线上 ax+b=y  发现会超时  后来 如果确定1点 那么只要剩余的点和1组成的斜率相同 它们和1点就在一条直线上  斜率有两种 普通的 或者直线和x轴垂直  为了把点1下所有斜率存在一个list方便统计 x轴垂直的斜率设为

2015-05-03 16:24:42 372

原创 https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 新建了一个list  这个list存的是数组A的一些下标 这些下标的所存的值都有共同的特点  就是A[i]的值和A[i+2]的值相同  list种存储的是i 这些下标存的值被赋值为'a' 因此可以保证 连续n个相同的数字  前n-2个都是'a'

2015-05-03 15:16:22 510

原创 https://leetcode.com/problems/remove-duplicates-from-sorted-array/

https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 很简单 因为已经排好序 i 和i下一个值相等就把下一个复制为字符串 最后再排序取数字即可 class Solution: # @param a list of integers # @return an integer def remov

2015-05-03 15:11:54 498

原创 https://leetcode.com/problems/valid-sudoku/

https://leetcode.com/problems/valid-sudoku/ 数独的要求 1除了1-9不允许有其他字符 (此题里可以有" . ") 2. 每个数字当前行,列,方格只出现一次  class Solution: # @param board, a 9x9 2D array # @return a boolean def inthecube(sel

2015-05-03 12:17:42 394

原创 https://leetcode.com/problems/set-matrix-zeroes/

https://leetcode.com/problems/set-matrix-zeroes/ 本来不想用两个数组 一开始的想法是找到0就把当前行列置为0 然后不再遍历这行这列  发现这样会把这两行里原来就是0的忽略掉  最后分配了两个数组 分别和行,列数一致分别代表 行,列 初始化值为1 遍历matrix 把值为0的行,列 对应的两个一维数组的值为0  然后遍历两个数组 如果为0 m

2015-05-03 11:35:31 519

原创 ☆☆☆https://leetcode.com/problems/3sum/

https://leetcode.com/problems/3sum/ 这道题由于之前写过two sum 可以先固定一个 另外两个用类似twosum的方法  但是niddle就不需要了 因为这道题允许有多个解 我没有写另一个函数 直接把代码都写在一起 不过效率很低  class Solution: # @return a list of lists of length 3, [[v

2015-05-03 11:26:10 933

原创 https://leetcode.com/problems/two-sum/

https://leetcode.com/problems/two-sum/ 寻找两个数的和是target 其实这道题可以遍历全部list 但是效率特别低 很容易超时  所以我的方法是将数组排序固定其中一个 另一个二分查找 有一个niddle 是为了判定 当找到刚好合适的值的时候 niddle=0  由于假设了只有一组解  就不必继续执行while了  要注意的是二分查找用j=(x+l

2015-05-03 11:15:03 816

原创 https://leetcode.com/problems/rotate-image/

https://leetcode.com/problems/rotate-image/ 讲数组右转90度  将数组分成四个区 只执行一个区的右转  然后把一区值拿出来 1->temp 4->1 3->4 2->3 temp->2  如果偶数行刚好能分成四个区 奇数行就多带一行 那么这个时候 中间的四条就会向右转两遍 也就是转了180度 还要再向左转一次归位 class Solution

2015-04-27 11:46:33 393

原创 https://leetcode.com/problems/spiral-matrix-ii/

https://leetcode.com/problems/spiral-matrix-ii/ 先把n2个数字都放在list里 输出了就删除  有一个记录层数的变量 每输出一层就+1 这样才能确定将数字 从哪里开始 输出在具体哪个位置 还有一个记录还剩多少层的变量 还剩2层和1层的时候特殊处理  注意初始化一个n*n的数组的方法 solution = [([0] * n) for i

2015-04-27 11:35:23 437

原创 https://leetcode.com/problems/spiral-matrix/

https://leetcode.com/problems/spiral-matrix/ 单独写了个剥离最外层的函数  如果是一般情况就顺时针输出数字 如果只有两层或者一层 单独处理  每剥离完一圈 长 宽均减一 很简单 注意边界 class Solution: # @param matrix, a list of lists of integers # @return a

2015-04-27 11:22:20 443

原创 https://leetcode.com/problems/first-missing-positive/

https://leetcode.com/problems/first-missing-positive/ 题目很好懂  我的做法是 1.删除所有非正数  2.去重 3.排序 4.看下标和值不相差1 如果都相同 那就少list里的下一个 一定有更好的方法  class Solution: # @param {integer[]} nums # @return {in

2015-04-26 21:58:41 415

原创 https://leetcode.com/problems/merge-sorted-array/

https://leetcode.com/problems/merge-sorted-array/ 这道题有一个很奇怪的问题 看了discuss之后明白了  A B数组的长度其实是不确定的 所以len(A)是不能用的 这也是为什么会给出m,n作为长度  大家的解释是 A的实际长度长于m 这是为了最后mergeB留的空间  但是有一个testcase  input [],[1]out

2015-04-26 21:55:19 632

空空如也

空空如也

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

TA关注的人

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