自定义博客皮肤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 565

原创 #Android##bug#ConcurrentModificationException

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

2016-01-15 21:52:04 345

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

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

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

2015-05-23 13:31:29 491

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

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

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

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

2015-05-23 13:01:28 332

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

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

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

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

2015-05-23 10:56:16 511

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

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

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

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

2015-05-13 23:39:20 310

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

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

2015-05-13 23:24:36 507

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

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

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

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

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

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

原创 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  8B  3

2015-05-13 22:21:39 347

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

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

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

2015-05-07 22:40:03 360

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

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

原创 第一次深深感到进步

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

2015-05-04 22:44:55 318

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

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

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

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

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

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

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

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

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

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

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

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

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

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

空空如也

空空如也

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

TA关注的人

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