随笔分类 -  lintCode

上一页 1 2 3 4 5 6 下一页

Palindrome Partitioning
摘要:这道题是典型的深度优先搜索的问题, 可以多拿出来看看,和我之前做的subset以及permutation不一样的是这道题其实是排列组合中如何切数组的问题[a,a,a,a]-- [a|a|a|a] -> [a|a|aa] -> [a|aa|a] -> [a|aaa] ->[aa|a|a] ->[aa| 阅读全文

posted @ 2016-08-19 09:25 codingEskimo 阅读(207) 评论(0) 推荐(0)

Letter Combinations of a Phone Number
摘要:这道题也属于排列组合问题,所以用recursive。属于深度优先的题目。这道题和单纯的subset和permutation略有不同,因为是从不同数字代表的字母中选,而每个数字内部的组合不需要。 在想这道题的时候我们可以先想两个数字,就是从第一个的数字所代表的字母中依次选出,然后和下一个数字代表的字母 阅读全文

posted @ 2016-08-19 06:55 codingEskimo 阅读(156) 评论(0) 推荐(0)

Combination Sum II
摘要:这道题比Combination Sum还要简单,因为每个数不能重复使用,所以只需要从下一个开始即可,和subset II一样 阅读全文

posted @ 2016-08-18 08:47 codingEskimo 阅读(147) 评论(0) 推荐(0)

Permutations II
摘要:这个题和permutation很像,只是需要考虑重复的数字。重复的数字只有第一次出现的时候加入,保证永远都是第一个重复出现的数字在前,而且要加过(用used数组纪律的时候,重复数字第一个如果没有被mark,就应该跳过)。 阅读全文

posted @ 2016-08-18 08:05 codingEskimo 阅读(123) 评论(0) 推荐(0)

Permutations
摘要:这个题和组合很像,但是排列需要把所有的可能都列出来,所以不需要记住位置,每一层都是从0开始循环。相应的,它需要一个数组,在每一层的时候把是否加过这个数字记下来,最后remove的时候需要把这个flag归位。 (这个题还有非recursive的做法,需要再看) 阅读全文

posted @ 2016-08-18 08:01 codingEskimo 阅读(163) 评论(0) 推荐(0)

Combination Sum
摘要:这个题和permutation以及subset一样, 也属于排列组合问题, 用recursive做。 这种题的时间消耗都是指数级别的 注意for loop里面的第二个if语句是要除去重复的数列,例如{2,2,3,6,7} target 7, 第二个2不需要再次考虑,因为作用和第一个相同,需要注意的是 阅读全文

posted @ 2016-08-18 07:56 codingEskimo 阅读(124) 评论(0) 推荐(0)

Subsets II
摘要:Given a list of numbers that may has duplicate numbers, return all possible subsets class Solution { /** * @param S: A set of numbers. * @return: A li 阅读全文

posted @ 2016-08-08 09:19 codingEskimo 阅读(129) 评论(0) 推荐(0)

Subsets
摘要:Given a set of distinct integers, return all possible subsets. 这是一道排列与组合的问题, 对于这种题目就是求所有的方案,其中90%是用搜索, 而搜素的题目90%会用到递归。 这类题目的模版 1)把现在结果加进去 2)用for loop做 阅读全文

posted @ 2016-08-08 08:27 codingEskimo 阅读(175) 评论(0) 推荐(0)

Merge Sorted Array
摘要:Given two sorted integer arrays A and B, merge B into A as one sorted array. Notice You may assume that A has enough space (size that is greater or eq 阅读全文

posted @ 2016-08-03 11:16 codingEskimo 阅读(176) 评论(0) 推荐(0)

[LeetCode] 231. Power of Two
摘要:Using O(1) time to check whether an integer n is a power of 2. Example For n=4, return true; For n=5, return false; 思路: 知识点: n & (n - 1) 使得从右边数最后一个1变为 阅读全文

posted @ 2016-07-19 11:43 codingEskimo 阅读(129) 评论(0) 推荐(0)

Update Bits
摘要:参考http://www.hawstein.com/posts/5.1.html 方案1:先将N中第0位到第i位保存下来(左闭右开:[0, i)),记作ret, 然后将N中第0位到第j位全清0([0, j]),通过向右移动j+1位然后再向左移动j+1位得到。 最后用上面清0后的值或上(m«i)再或上 阅读全文

posted @ 2016-07-19 11:40 codingEskimo 阅读(141) 评论(0) 推荐(0)

Find the Missing Number
摘要:方法一: 数学方法,先找到最大的值,需要比较最大的值和array size, 要是比array size小, 说明最大值missing。 然后用等差数列公式求得如果不缺失值的和,然后再减去array里数的和。 方法二: 先把不缺失的所有数取xor, 然后再对数组里的数取xor, 剩下的就是最后的结果 阅读全文

posted @ 2016-07-19 11:37 codingEskimo 阅读(268) 评论(0) 推荐(0)

Count 1 in Binary
摘要:方法二: num & (num -1) 一直就把最后一个非0位变成1, 一直变到所有的都是0, 那么process了几次, 就是有几位1 阅读全文

posted @ 2016-07-19 11:11 codingEskimo 阅读(111) 评论(0) 推荐(0)

[LeetCode] 137. Single Number II
摘要:Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your al 阅读全文

posted @ 2016-07-05 10:59 codingEskimo 阅读(179) 评论(0) 推荐(0)

[LeetCode] 136. Single Number
摘要:There are two ways for the problem:1. Tradition: Setup a map or set.2. Bit operation: x ^ x = 0; so if the number keep XOR, the same ones will cancel 阅读全文

posted @ 2016-07-05 08:49 codingEskimo 阅读(134) 评论(0) 推荐(0)

[LintCode] A + B Problem
摘要:这是一个bit opperation 的题目。 九章算法有一个比较好的解释 http://www.jiuzhang.com/solutions/a-b-problem/ a^b是不进位的加法,什么时候需要进位呢,就是a和b都是1的情况。这个时候a&b就能给出所有需要进位的位,所谓进位就是需要左移一位 阅读全文

posted @ 2016-07-05 08:14 codingEskimo 阅读(146) 评论(0) 推荐(0)

Two Strings Are Anagrams
摘要:public class Solution { /** * @param s: The first string * @param b: The second string * @return true or false */ public boolean... 阅读全文

posted @ 2015-12-01 13:54 codingEskimo 阅读(126) 评论(0) 推荐(0)

[LeetCode] 206. Reverse Linked List
摘要:/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * 阅读全文

posted @ 2015-12-01 09:54 codingEskimo 阅读(108) 评论(0) 推荐(0)

Singleton
摘要:class Solution { /** * @return: The same instance of this class every time */ //http://www.cnblogs.com/EdwardLiu/p/4443230.html ... 阅读全文

posted @ 2015-12-01 09:35 codingEskimo 阅读(147) 评论(0) 推荐(0)

[LintCode] Count 1 in Binary
摘要:Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return 1 Given 5, return 2 Given 1023, return 9 Count how many 1 in b 阅读全文

posted @ 2015-11-13 12:41 codingEskimo 阅读(134) 评论(0) 推荐(0)

上一页 1 2 3 4 5 6 下一页

导航