• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

随笔分类 -  Leetcode

上一页 1 ··· 19 20 21 22 23 24 25 26 27 ··· 32 下一页
Leetcode: Binary Tree Right Side View

摘要:这道题就是BT的Level Order Traversal,每次要换一层的时候,记录当前节点 注意,每次都是先添加右边child,所以是i==0的时候add 阅读全文
posted @ 2015-04-11 07:06 neverlandly 阅读(789) 评论(0) 推荐(0)
Leetcode: Number of Islands

摘要:DFS的Flood Fill方法, 不使用额外visited数组,但是用‘1’变成‘2’表示visited的方法 复杂度 时间 O(NM) 空间 O(max(N,M)) 递归栈空间 Union Find: (Quick Union) follow up是找最大岛的面积,想法是设置两个全局变量 max 阅读全文
posted @ 2015-04-11 05:20 neverlandly 阅读(2351) 评论(0) 推荐(0)
Leetcode: Reverse Bits

摘要:Reverse Integer那道题会考虑溢出,因为那是reverse each digit,这里不会溢出,因为reverse的是each bit Q:如果该方法被大量调用,或者用于处理超大数据(Bulk data)时有什么优化方法?A:这其实才是这道题的精髓,考察的大规模数据时算法最基本的优化方法 阅读全文
posted @ 2015-03-14 07:06 neverlandly 阅读(775) 评论(0) 推荐(0)
Leetcode: Number of 1 Bits

摘要:Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit i... 阅读全文
posted @ 2015-03-14 06:02 neverlandly 阅读(478) 评论(0) 推荐(0)
Leetcode: Best Time to Buy and Sell Stock IV

摘要:这道题在Best Time to Buy and Sell Stock III做过,那道题只是把k取了2而已 递推式依然是 local[i][j]=max(global[i-1][j-1]+max(diff,0),local[i-1][j]+diff), global[i][j]=max(local 阅读全文
posted @ 2015-03-01 12:40 neverlandly 阅读(3460) 评论(0) 推荐(0)
Leetcode: Reverse Words in a String II

摘要:这道题要求in-place做法,不能使用extra space, 那么,做法跟Rotate Array那道题非常相似 (1)reverse the whole array (2)reverse each subarray seperated by ' ' 注意不要忘了reverse最后一个word 阅读全文
posted @ 2015-03-01 07:49 neverlandly 阅读(6591) 评论(0) 推荐(0)
Leetcode: Repeated DNA Sequence

摘要:Naive 方法就是两层循环,外层for(int i=0; i<=s.length()-10; i++), 内层for(int j=i+1; j<=s.length()-10; j++), 比较两个字符串s.substring(i, i+10)和s.substring(j, j+10)是否equal 阅读全文
posted @ 2015-03-01 07:31 neverlandly 阅读(477) 评论(1) 推荐(0)
Leetcode: 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].Note:... 阅读全文
posted @ 2015-03-01 05:19 neverlandly 阅读(1722) 评论(0) 推荐(0)
Leetcode: Largest Number

摘要:聪明方法:其实干嘛要挨个比呢,按最直接的方法,接起来,谁大谁在前: 可以换一下思路,要想比较两个数在最终结果中的先后位置,何不直接比较一下不同组合的结果大小? 举个例子:要比较3和34的先后位置,可以比较334和343的大小,而343比334大,所以34应当在前。 这样,有了比较两个数的方法,就可以 阅读全文
posted @ 2015-01-29 07:31 neverlandly 阅读(479) 评论(0) 推荐(1)
Leetcode: Dungeon Game

摘要:The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a... 阅读全文
posted @ 2015-01-29 00:07 neverlandly 阅读(717) 评论(0) 推荐(0)
Leetcode: Binary Search Tree Iterator

摘要:Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the ne... 阅读全文
posted @ 2015-01-28 07:36 neverlandly 阅读(534) 评论(0) 推荐(0)
Leetcode: Two Sum III - Data structure design

摘要:The trade off should be considered: In fact, there has to be one operation's time complexity is O(n) and the other is O(1), no matter add or find. So 阅读全文
posted @ 2015-01-27 12:51 neverlandly 阅读(3524) 评论(0) 推荐(0)
Leetcode: Two Sum II

摘要:Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function ... 阅读全文
posted @ 2015-01-27 12:02 neverlandly 阅读(400) 评论(0) 推荐(0)
Leetcode: Fraction to Recurring Decimal

摘要:Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating,... 阅读全文
posted @ 2015-01-27 09:10 neverlandly 阅读(391) 评论(0) 推荐(0)
Leetcode: Compare Version Numbers

摘要:这道题的意思用一个例子说明就是1.2 < 1.10, 因为1.10跟1.1是不一样的,1.10代表tenth revision. 而1.1代表first revision。所以这道题要按“.”分段,然后分段比较,方法是用split函数把string 分段,每一段挨个比过去。这里参考了网上的一个小技巧 阅读全文
posted @ 2015-01-27 05:09 neverlandly 阅读(461) 评论(0) 推荐(0)
Leetcode: Maximum Gap

摘要:Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Return 0 ... 阅读全文
posted @ 2015-01-26 12:49 neverlandly 阅读(330) 评论(0) 推荐(0)
Leetcode: Missing Ranges

摘要:第二遍做法:参考下面一个vimukthi的解答 https://discuss.leetcode.com/topic/18612/accepted-java-solution-with-explanation/3 low 表示next possible missing integer streak的 阅读全文
posted @ 2015-01-26 10:31 neverlandly 阅读(793) 评论(0) 推荐(0)
Leetcode: One Edit Distance

摘要:注意这道题:Must be exactly one distance apart. Not the same. 另外FB面经有一道比较狠的这个题的变形: 这题就是one edit distance的变形题,难点在于给的Iterator,事先不知道两个file的长度,也不允许用extra space( 阅读全文
posted @ 2015-01-24 05:46 neverlandly 阅读(1285) 评论(0) 推荐(0)
Leetcode: Longest Substring with At Most Two Distinct Characters

摘要:方法一:用HashMap, map里面存元素及其出现次数。维护一个最大长度。用两个指针,右指针一直走到出现3个dinstinct character为止。然后调整左指针删元素,直接从左往右逐个字符的删除,一直删到某个字符不会再出现。判断字符被删光就看次数是否减为了0. 采用方法: 这里每个元素都会进 阅读全文
posted @ 2015-01-22 13:17 neverlandly 阅读(400) 评论(0) 推荐(0)
Leetcode: Read N Characters Given Read4 II - Call multiple times

摘要:这道题跟I不一样在于,read函数可能多次调用,比如read(buf,23)之后又read(buf, 25), 第一次调用时的buffer还没用完,还剩一个char在buffer里,第二次拿出来接着用,这样才能保证接着上次读的地方继续往下读。 1. 所以应该设置这4个char的buffer为inst 阅读全文
posted @ 2015-01-22 07:35 neverlandly 阅读(2821) 评论(0) 推荐(0)

上一页 1 ··· 19 20 21 22 23 24 25 26 27 ··· 32 下一页
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3