F_G

许多问题需要说清楚就可以&&走永远比跑来的重要

导航

08 2015 档案

[Leetcode] Basic Calculator II
摘要:Basic Calculator IIImplement a basic calculator to evaluate a simple expression string.The expression string contains only non-negative integers, +, -... 阅读全文

posted @ 2015-08-31 20:51 F_G 阅读(215) 评论(0) 推荐(0)

[Leetcode] Basic Calculator
摘要:Basic CalculatorImplement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses )... 阅读全文

posted @ 2015-08-31 19:56 F_G 阅读(152) 评论(0) 推荐(0)

[底层] 为什么Integer.MIN_VALUE-1会等于Integer.MAX_VALUE
摘要:Integer.MIN_VALUE-1 = Integer.MAX_VALUEInteger.MAX_VALUE+1 = Integer.MIN_VALUE实际上这里是计算机底层的位运算法则问题[1]计算机底层采用了补码来进行加减乘除的运算,好处是符号位参与运算.举上面两个例子来说明问题。Integ... 阅读全文

posted @ 2015-08-31 18:48 F_G 阅读(2456) 评论(0) 推荐(1)

[Leetcode] Combination Sum III
摘要:Combination Sum IIIFind all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each com... 阅读全文

posted @ 2015-08-31 10:48 F_G 阅读(174) 评论(0) 推荐(0)

[算法] Quick Sort
摘要:Quick Sort下面是快速排序的递归实现。快速排序的一个问题是,可能面临退化的现象,也就是数据已经是排好序的,应对策略是对于第一个元素,即pivot的选择使用随机策略。 1 public class QuickSort { 2 3 public QuickSort(){ 4 ... 阅读全文

posted @ 2015-08-30 14:08 F_G 阅读(252) 评论(0) 推荐(0)

[Leetcode] Bitwise AND of Numbers Range
摘要:Bitwise AND of Numbers RangeGiven a range [m, n] where 0 > 1;20 n = n >> 1;21 order++;22 }23 return m<<order;2... 阅读全文

posted @ 2015-08-30 13:32 F_G 阅读(172) 评论(0) 推荐(0)

[算法] 背包问题
摘要:背包问题 阅读全文

posted @ 2015-08-29 23:29 F_G 阅读(142) 评论(0) 推荐(0)

[一个结构] 堆的实现
摘要:堆是一个数组结构,可以将其看做一个完全二叉树。对的最重要的操作时堆的调整操作。所谓调整是自定向下调整一个元素的位置。如果当前元素比两个孩子小(至少一个),那么就将其和拥有最大值的孩子进行交换,知道满足他比两个孩子大,这里说的是针对最大堆。建堆:从n/2到0个孩子依次进行堆得调整操作,最后会得到得到符... 阅读全文

posted @ 2015-08-29 23:28 F_G 阅读(291) 评论(0) 推荐(0)

[Leetcode] Word Break II
摘要:此题和另外wordladder II有异曲同工之妙,最终路径的搜索都是用图。这种搜索路径的题目实际上可以总结出一个非常通用的减少递归深度,计算量的方法,是使用一定的预处理措施,将前期的依赖关系简化为一个图,然后使用图算法来进行路径的计算 1 import java.util.*; 2 3 publ... 阅读全文

posted @ 2015-08-29 12:09 F_G 阅读(213) 评论(0) 推荐(0)

[算法] 图DFS非递归实现
摘要:图的遍历方式当中DFS和BFS是两种主要的遍历方式。DFS主要是使用递归思想来实现的,BFS主要是使用队列来保存下面的节点。BFS的一个优势是不是非递归形式,所以栈溢出的可能性很小,相反DFS在这方面的限制比较大。因此,如何将DFS改为非递归形式意义重大。如何将DFS变为非递归形式呢?实际上,前面带... 阅读全文

posted @ 2015-08-27 21:52 F_G 阅读(1269) 评论(0) 推荐(0)

[算法] 快速排序的非递归形式实现
摘要:fastsort如何实现?在实际的递归算法当中,我们我们使用一个pivot将数组分为了两部分,然后在分别递归处理这两部分,使用递归先处理左半部分,然后是右半部分,这是用递归可以很容易实现。但是使用递归的坏处是可能会溢出,当然对于比较良好的分布,即在每一步都能讲数组评分的情况,栈溢出的可能性很小,但是... 阅读全文

posted @ 2015-08-27 21:46 F_G 阅读(1383) 评论(0) 推荐(0)

[Leetcode] Decode Ways
摘要:Decode WaysA message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded ... 阅读全文

posted @ 2015-08-27 19:54 F_G 阅读(200) 评论(0) 推荐(0)

[Leetcode] 不是从零开始的计数问题
摘要:一、Permutation Sequence二、Excel Sheet Column Title三、Excel Sheet Column Number 阅读全文

posted @ 2015-08-26 16:36 F_G 阅读(175) 评论(0) 推荐(0)

[Leetcode] Integer to Roman
摘要:第三次刷这个题了,这次一次A过!总结起来是利用了小于4000的数字的罗马数字表示上的相对独立性,我们可以只针对正数表示的个位、十位、百位、千位来分别设计计算的策略。0:return “”1-3:return相应数字个当前符号4:返回当前符号+高一级的符号5:返回高一级符号6-8:返回高一级符号+相应... 阅读全文

posted @ 2015-08-23 18:51 F_G 阅读(152) 评论(0) 推荐(0)

[Leetcode] Roman to Integer
摘要:Roman to IntegerI:1V:5X:10L:50C:100D:500M:1000这个问题的求解已经很清楚了:在每次累加的时候记录前面的遇到的字符所对应的数字,如果当前字符所对应的数字比前面的大,则应该减去两倍的前面的字符所对应的数字。附上代码。这里还有一个收获是对map的一个初始化方式。... 阅读全文

posted @ 2015-08-23 18:23 F_G 阅读(183) 评论(0) 推荐(0)

[Leetcode] Fraction to Recurring Decimal
摘要:[1]Fraction to Recurring Decimal这个题目可以在下面几个方面得到训练提示一、对于数学运算的模拟问题应该随时注意溢出问题,防止溢出的一个手段是把int转化为double二、打印问题:即使是double最后还是要通过int的形式打印出来,如果是double类型使用Strin... 阅读全文

posted @ 2015-08-21 16:41 F_G 阅读(204) 评论(0) 推荐(0)

[Leetcode] maximum gap
摘要:[1]https://leetcode.com/problems/maximum-gap/这里具有使用桶的思想来对整数进行规整,通过详细的设计桶的大小,可以保证最大间隔一定大于等于桶的大小,从而保证最大间隔不会出现在桶的内部,而是桶之间。这里的关键是如何设计桶的大小?假设N个数字,均等分布,那么间隔... 阅读全文

posted @ 2015-08-20 15:17 F_G 阅读(234) 评论(0) 推荐(0)

[Bug] 两个链表,交错组合为另一个链表
摘要:ListNode dummy3 = new ListNode(-1);ListNode pre = dummy3;while(head2!=null&&head1!=null){ pre.next=head1; ListNode head1next = head1.next; ... 阅读全文

posted @ 2015-08-19 20:48 F_G 阅读(248) 评论(0) 推荐(0)

[Java] switch-case 当中的变量定义
摘要:1 switch(c){ 2 case '+': 3 int res =d1+d2; 4 data.push(res); 5 ... 阅读全文

posted @ 2015-08-19 19:53 F_G 阅读(4701) 评论(0) 推荐(0)

[Leetcode] Word Break I
摘要:Word Break使用DFS遍历当然是可以的,尝试使用DP应该更加简洁,发现leetcode讨论确实是一个非常不错的资源。下面是我的改进代码 1 public class Solution { 2 public boolean wordBreak(String s, Set wordDic... 阅读全文

posted @ 2015-08-19 15:57 F_G 阅读(208) 评论(0) 推荐(0)

[Leetcode] BFS
摘要:DFS一般会使用递归调用的方式来实现,简单,但是如果递归的深度非常大,可能会造成递归栈的overflowBFS可以实现对图的非递归遍历。在leetcode当中有两道经典的BFS遍历的题目[1]Number of Islands 1 public class Solution { 2 priv... 阅读全文

posted @ 2015-08-19 14:27 F_G 阅读(268) 评论(0) 推荐(0)

[Hulu 2014] 两递增数组A和B,求A[i]+B[j]中前k个最小值
摘要:在许多地方发现了对这个提的介绍关键是对下个最小的状态的选取,需要证明一下。[2]使用了程序判断是否重复的方式,并且覆盖了equals方法,可以借鉴一下![1] http://www.cnblogs.com/weixliu/p/3779288.html[2] http://blog.csdn.net/... 阅读全文

posted @ 2015-08-18 21:48 F_G 阅读(221) 评论(0) 推荐(0)

[Leetcode] Count Complete Tree Nodes
摘要:Count Complete Tree NodesGiven acompletebinary tree, count the number of nodes.Definition of a complete binary tree fromWikipedia:In a complete binary... 阅读全文

posted @ 2015-08-18 20:19 F_G 阅读(173) 评论(0) 推荐(0)

[Leetcode] Linked List Cycle II
摘要:如何找到环的起始位置?算法大家估计都已经是耳熟能详了,关键是证明。我们假设从起始位置到环的起点的长度为X,相遇位置离起始点的距离为K,环的长度为Y,由于fast pointer的速度是low pointer的两倍。所以X+mY+K=2*(X+nY+K)X+K=(m-2n)Y从这个等式可以解释为什么:... 阅读全文

posted @ 2015-08-18 09:15 F_G 阅读(123) 评论(0) 推荐(0)

[Leetcode] Single Number II
摘要:参考当中有对一个bug的说明[1] http://shmilyaw-hotmail-com.iteye.com/blog/2153924 阅读全文

posted @ 2015-08-18 08:34 F_G 阅读(110) 评论(0) 推荐(0)

[Leetcode] Surrounded Regions
摘要:使用DFS可能会溢出,所以使用BFS比较保险。对于矩阵的位置如何保存,刚开始我想到使用创建class node来保存横纵坐标,当发现参考文献【1】的时候,才发现,只需要一个整数值,就可以了。position = rowindex*col + colindex;如要得到相应的横纵坐标,使用相反的取模等... 阅读全文

posted @ 2015-08-17 20:29 F_G 阅读(121) 评论(0) 推荐(0)

[算法] 字符串压缩
摘要:参考[1] http://www.cnblogs.com/venow/archive/2012/09/03/2668454.html[2] http://blog.csdn.net/fivedoumi/article/details/7961835 阅读全文

posted @ 2015-08-17 19:36 F_G 阅读(233) 评论(0) 推荐(0)

[算法] 坏的问题
摘要:一、链表中存在坏,找出坏的开始位置二、找出N个元素当中的一个重复元素,取值范围为 [1,N-1]参考 阅读全文

posted @ 2015-08-17 19:31 F_G 阅读(138) 评论(0) 推荐(0)

[算法]二叉树的序列化、反序列化
摘要:一、 使用先序遍历的方式【1】中的方式是采用设定#的方式,当访问读取了#时候,创建null,返回。二、二叉搜索树的序列化二叉搜索树具有的有序的性质,可以利用这个性质来递归的反序列化BST对当前节点要处理的数据的范围设定一个边界,当读取的值是在这个范围里面的时候,则进行node的创建,否则创建NULL... 阅读全文

posted @ 2015-08-17 19:04 F_G 阅读(164) 评论(0) 推荐(0)

[底层] 大端小端
摘要:参考[1] http://blog.csdn.net/zhaoshuzhaoshu/article/details/37600857/ 阅读全文

posted @ 2015-08-17 16:43 F_G 阅读(138) 评论(0) 推荐(0)

sort算法总结
摘要:sort算法总结冒泡排序,插入排序,选择排序,heap 排序,fast 排序等等 阅读全文

posted @ 2015-08-17 15:30 F_G 阅读(211) 评论(0) 推荐(0)

[Leetcode] Longest Consecutive Sequence
摘要:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest ... 阅读全文

posted @ 2015-08-17 15:29 F_G 阅读(142) 评论(0) 推荐(0)

[Leetcode] Pascal's Triangle II
摘要:Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm to use onlyO(... 阅读全文

posted @ 2015-08-17 11:11 F_G 阅读(162) 评论(0) 推荐(0)

[Leetcode] Largest Number
摘要:再次熟悉java当中的comparator的写法Given a list of non negative integers, arrange them such that they form the largest number.For example, given[3, 30, 34, 5, 9]... 阅读全文

posted @ 2015-08-17 10:24 F_G 阅读(156) 评论(0) 推荐(0)

[Leetcode] binary tree 右视问题
摘要:[1] Populating Next Right Pointers in Each Node[2] Populating Next Right Pointers in Each Node II[3] Binary Tree Right Side View一、参考我的另一篇博客二、同一三、使用层次遍... 阅读全文

posted @ 2015-08-17 10:10 F_G 阅读(187) 评论(0) 推荐(0)

[Leetcode] 旋转问题(旋转数组的查找,旋转list,旋转矩阵)
摘要:[1] Search in Rotated Sorted Array在二分查找问题当中,对于边界的查找,尤其是==的情况应该放到左边界上,因为对于mid的求取又是就是left,所以这是检测应该是true,所以下面的nums[mid]>=nums[left],如果改为nums[mid]>nums[le... 阅读全文

posted @ 2015-08-16 11:29 F_G 阅读(504) 评论(0) 推荐(0)

[Leetcode] 回文问题
摘要:[1] Palindrome Number[2] Valid Palindrome[3] Palindrome Partitioning[4] Palindrome Partitioning II[5] Shortest Palindrome[6] Palindrome Linked List[7]... 阅读全文

posted @ 2015-08-16 11:08 F_G 阅读(335) 评论(0) 推荐(0)

[Leetcode] Word Ladder I,II
摘要:一、问题描述:Given:start="hit"end="cog"dict=["hot","dot","dog","lot","log"]As one shortest transformation is"hit" -> "hot" -> "dot" -> "dog" -> "cog",return... 阅读全文

posted @ 2015-08-16 11:00 F_G 阅读(253) 评论(0) 推荐(0)

[Leetcode] Best Time to Buy and Sell Stock I,II,III,IV
摘要:三种股票交易算法一、交易次数没有限制使用贪心策略,找最长递增序列,同时累加相应利润。二、只有一次交易使用动态规划算法,从前往后,依次记记录相应时间节点前面的最小price,同时获得在这个节点的最大利润,同时更新最小price三、最多两次使用两次动态规划1、从左向右,记录在相应的时间节点卖出的最大利润... 阅读全文

posted @ 2015-08-15 18:55 F_G 阅读(203) 评论(0) 推荐(0)

[Leetcode] Populating Next Right Pointers in Each Node I,II
摘要:这两个题目使用相同的code就可以解决。算法在每一层遍历当中记录head节点,那么在下一层的遍历当中就可以使用这个head遍历整层,同时记录下层的head,依次类推,直到head为空,表示到达了最底层。 阅读全文

posted @ 2015-08-15 18:50 F_G 阅读(115) 评论(0) 推荐(0)

[Leetcode] Construct Binary Tree from Inorder and Postorder Traversal I,II
摘要:这两个问题实际上是同一个问题,需要对三种遍历方式的规律非常清楚。对于前序遍历,第一个元素实际上就是root,然后后面的元素前半部分是左树的node,后半部分是右树的node对于中序遍历,一旦我们知道了root节点,那么就可以将其分为两半部分,也就是左树和右树对于后序遍历,我们可以缺点最后一个节点是r... 阅读全文

posted @ 2015-08-15 18:47 F_G 阅读(127) 评论(0) 推荐(0)

[Leetcode] Convert Sorted List to Binary Search Tree
摘要:对于Convert Sorted array to Binary Search Tree相对简单,使用二分的方式就可以确定左树和右树的范围。一、对于此问题,使用二分的方式也是可以的,但是没有数组的随机访问的特性,需要遍历数组找到list的中间node。为了便于查找中间node可以首先统计节点的个数。... 阅读全文

posted @ 2015-08-15 18:25 F_G 阅读(180) 评论(0) 推荐(0)

[Leetcode] Unique Binary Search Trees I,II
摘要:一、Unique Binary Search TreesI统计个数迭代的方法可以解决。f(0)=1,f(1)=1,f(2)=2,f(3)=3.f(4)=f(0)*f(3)+f(1)*f(2)+f(2)*f(1)+f(3)*f(0)=10;f(5)=f(0)*f(4)+f(1)*f(3)+f(2)*f... 阅读全文

posted @ 2015-08-15 16:52 F_G 阅读(130) 评论(0) 推荐(0)

[Leetcode] Binary Tree Level order travelsal (normal and zigzag and bottom-up )
摘要:一、normal fasion使用queue记录上次访问的是记录的孩子节点 1 public List> levelOrder(TreeNode root) { 2 List> res = new LinkedList>(); 3 Queue queue = new LinkedLi... 阅读全文

posted @ 2015-08-15 16:18 F_G 阅读(526) 评论(0) 推荐(0)

[Leetcode] Binary tree travelsal (preorder, inorder, postorder)
摘要:一、前序 1 public List preOrder(Node root){ 2 List res = new LinkedList(); 3 Stack stack = new Stack(); 4 stack.push(root); 5 while(root!=... 阅读全文

posted @ 2015-08-14 23:55 F_G 阅读(330) 评论(0) 推荐(0)

[Leetcode] Jump Game
摘要:可以使用贪心策略在当前跳跃可以确定一个最远的距离,在这个距离range 之内不需要再进行多次跳跃,但是需要记录所能达到的最远的距离。当跨出这个range的时候,跳跃次数加一,并且使用记录的最远距离作为下次能够跳跃的做大边界。以此类推直至达到最后为止。 阅读全文

posted @ 2015-08-14 22:24 F_G 阅读(137) 评论(0) 推荐(0)

[Leetcode] Trapping Rain Water
摘要:对于这个题目一个快速解法是统计每一个位置的柱子上面最多可以放多少水?因此需要统计他的前面和后面的柱子的最大高度,去两者的最小值即可!《》 阅读全文

posted @ 2015-08-14 21:47 F_G 阅读(104) 评论(0) 推荐(0)

[Leetcode] Divide Two Integers
摘要:大体的思想史使用折半。dividend(被除数) 和 divisor(除数)除数不断进行翻倍,这里使用加法而不是乘以2的操作,同时记录divisor的个数,同样可以使用加法实现,当大于dividend的时候,停止,并且输出当前统计的divisior的个数。同时从dividend当中减去当前的对div... 阅读全文

posted @ 2015-08-14 21:37 F_G 阅读(177) 评论(0) 推荐(0)

[Leetcode] Largest Rectangle in Histogram
摘要:首先感谢网友的分享[1] [2]一、找单调递增的序列,当遇到第一个递减元素的时候进行面积的计算。为什么这个时候可以进行面积的计算?假设当前的序列为:.....,2,4,5,6,3,......那么我们可以确定2之前的元素都是比2小的,现在遇到了第一个递减元素3,那么此时4->6,5->6,6->之间... 阅读全文

posted @ 2015-08-14 17:24 F_G 阅读(144) 评论(0) 推荐(0)

[Leetcode] Letter Combinations of a Phone Number
摘要:每个数字对应多个字符,给定一个数字串,给出所有与这个数字串对应的字符串。一、使用递归的方式最容易想到了 1 import java.util.*; 2 3 public class Solution { 4 void backtree(String digits,int index,Str... 阅读全文

posted @ 2015-08-14 11:04 F_G 阅读(231) 评论(0) 推荐(0)

[Leetcode] Wildcard Matching
摘要:*匹配任意多个字符,.匹配单个字符关键在于*的情况,如果遇到了星号,那么需要枚举所有与星号匹配的情况,包括空串的情况一、递归解法 1 boolean isMRecursive(String s,String p){ 2 if(p.compareTo("")==0) return s.comp... 阅读全文

posted @ 2015-08-13 23:34 F_G 阅读(252) 评论(0) 推荐(0)

[C++] volatile
摘要:一个变量被volatile修饰之后,volatile就会告诉编译器这个变量随时都可能被修改,因此每次对其read都会从内存中进行读取,而不是从cache当中进行读取。对于volatile编译器是不会进行优化的。[1] http://blog.csdn.net/zzulp/article/detail... 阅读全文

posted @ 2015-08-13 19:42 F_G 阅读(155) 评论(0) 推荐(0)

[Leetcode] Regular Expression Matching
摘要:这里只需要实现正则表达式当中的*和.但是正则表达式包含的内容不止这些,现在这里简单总结一下。1、a*:匹配零个或者多个a2、.:匹配任意一个字符3、a?:匹配零个或者一个a4、a+:匹配一次或者多个a5、开始^,结束$6、范围[m,n],最多出现n次,最少出现m次我们这里实现以下* and . an... 阅读全文

posted @ 2015-08-13 14:27 F_G 阅读(149) 评论(0) 推荐(0)

[Leetcode] String to Integer (atoi)
摘要:这个题目关键是边界条件的检查,为了防止溢出将临时的计算结果放到double当中。一、对于符号的处理二、如果遇到了非法的字符,那么需要返回当前记录的数值 阅读全文

posted @ 2015-08-12 21:59 F_G 阅读(122) 评论(0) 推荐(0)

[Leetcode] Median of Two Sorted Arrays
摘要:这个问题是求第K大的一个中特殊情况。假设连个数组是A,B,我们分别求A和B的前m段和前n段,且m+n=k。我们比较A[m-1] 和 B[n-1]如果A[m-1] s2) return findkth(nums2,start2,s2,nums1,start1,s1,k); 4 if(s... 阅读全文

posted @ 2015-08-12 21:56 F_G 阅读(245) 评论(0) 推荐(0)

[Leetcode] Longest Substring Without Repeating Characters
摘要:这道题目使用的方法具有非常大的普遍性,实际上是两个指针。一个指针记录当前所记录的子串的开始,另一个是当前遍历的位置,如果产生了重复,那么需要进行修正,实际上是对子串进行收缩。从当前子串开始位置到重复位置,重置相应的字符为违被搜索状态。在收缩之前需要进行,最长子串长度的更新。 1 public cla... 阅读全文

posted @ 2015-08-12 21:20 F_G 阅读(162) 评论(0) 推荐(0)

[Leetcode] Combination Sum III
摘要:这里只能使用1到9九个数字,并且使用的数字的个数有限制。方法类似 1 public class Solution { 2 public void dp(List> list, List listone, int tmpsum, int start, int k, int target){ 3... 阅读全文

posted @ 2015-08-12 21:06 F_G 阅读(152) 评论(0) 推荐(0)

[Leetcode] Combination Sum II
摘要:实际上这里的道理和Combination Sum是一样的,只是对于每个元素的次数我们不需要在每次递归的时候进行计算上限,因为题目限制了最多的出现次数。其他类似。 1 import java.util.*; 2 3 public class Solution { 4 private void... 阅读全文

posted @ 2015-08-12 21:02 F_G 阅读(183) 评论(0) 推荐(0)

[Leetcode] Combination Sum
摘要:由于每个元素我可以出现的次数没有限制,我们可以在使用某个元素的时候进行计算一下最多的个数,进行枚举。同样的道理,在每一层递归只考虑一个元素,并且在当前sum大于目标sum的时候进行剪枝。 1 import java.util.*; 2 3 public class Solution { 4 ... 阅读全文

posted @ 2015-08-12 20:59 F_G 阅读(194) 评论(0) 推荐(0)

[Leetcode] Two Sum II - Input array is sorted
摘要:这里因为是有序的,可以利用这个有序性来进行查找left=0,right=num.length-1;while(lefttarget) right--; else return true;}return false; 阅读全文

posted @ 2015-08-12 20:28 F_G 阅读(152) 评论(0) 推荐(0)

[Leetcode] 4Sum
摘要:一、使用2sum降低复杂度为O(N^2) 1 public class Solution { 2 public List> fourSum(int[] nums, int target) { 3 Arrays.sort(nums); 4 List> res =... 阅读全文

posted @ 2015-08-12 20:25 F_G 阅读(164) 评论(0) 推荐(0)

[Leetcode] 3Sum Closest
摘要:使用2sum降低复杂度为O(N^2) 1 public class Solution { 2 public int threeSumClosest(int[] nums, int target) { 3 Arrays.sort(nums); 4 int clo... 阅读全文

posted @ 2015-08-12 20:23 F_G 阅读(210) 评论(0) 推荐(0)

[Leetcode] 3Sum
摘要:这道题目实际上有非常好的O(N^2)的解法。关键是利用了2sum的线性解法public class Solution { public List> threeSum(int[] nums) { List> res = new LinkedList>(); if(nu... 阅读全文

posted @ 2015-08-12 20:21 F_G 阅读(202) 评论(0) 推荐(0)

[Leetcode] Add Two Numbers
摘要:数字表示为链表的格式,需要注意的几个地方:一、在计算过程当中要注意进位问题,刚开始的进位为0,后面进位要在计算过程当中进行更新。二、如果某个数字的list比较长,那么需要在最后将其加上三、最后如果进位不是零,说明需要额外增加一位代码如下: 1 /** 2 * Definition for sing... 阅读全文

posted @ 2015-08-12 20:15 F_G 阅读(176) 评论(0) 推荐(0)

[Leetcode] Two Sum
摘要:1 import java.util.*; 2 3 public class Solution { 4 public int[] twoSum(int[] nums, int target) { 5 Map map = new HashMap(); 6 i... 阅读全文

posted @ 2015-08-12 20:12 F_G 阅读(177) 评论(0) 推荐(0)

[CTCI] Fobonacci Problem
摘要:The problem is well known, so there is no need to repeat it!we can apply the iteration function f(n+1) = f(n)+f(n-1), however the time complexity is O... 阅读全文

posted @ 2015-08-12 20:04 F_G 阅读(159) 评论(0) 推荐(0)

[Leetcode] Subset I
摘要:method 1: permutation !method 2: use integer and select element according to the bit in each integer. 阅读全文

posted @ 2015-08-12 17:12 F_G 阅读(109) 评论(0) 推荐(0)

[Leetcode] Permutation Sequence
摘要:crute method is RE!! 阅读全文

posted @ 2015-08-12 17:09 F_G 阅读(106) 评论(0) 推荐(0)

[Leetcode] Next Permutation
摘要:1. From the last index, we seach the first decrease order, such i,i+1 and num[i] 14532 -> 14235The last step is to change the order from 5 to 2 to be ... 阅读全文

posted @ 2015-08-12 16:56 F_G 阅读(153) 评论(0) 推荐(0)

[Leetcode] Permutations II
摘要:The difference betweent Problem Permutation I and Permuation II lies in the duplicates elements may exist in the second one.For the first one , the en... 阅读全文

posted @ 2015-08-12 16:27 F_G 阅读(191) 评论(0) 推荐(0)

[Leetcode] Permutations
摘要:I have two solutions for this problem, the first one is from the undergraduate stage, the second one is from the CTCI (permute string)The common point... 阅读全文

posted @ 2015-08-12 15:59 F_G 阅读(191) 评论(0) 推荐(0)

Mysql当中的on和where的区别
摘要:MySQL当中的限制条件可以使用on或者where,两者在不同的情况下具有不同而意义如果是left join on,这里的on是对右表的限制,对左表没有限制如果是inner join on/where意思是一样的[1]http://www.jb51.net/article/39814.htm 阅读全文

posted @ 2015-08-11 23:33 F_G 阅读(530) 评论(0) 推荐(0)

[Leetcode] The Skyline Problem
摘要:从直观上来理解这个问题如何进行cope with:我们从最左边向右进行遍历所有的节点,观察在这个节点上的最高度是多少?如果最高高度和上一次监测的最高高度不一致,那么说明这里需要更新。这里需要注意的是,如果如果当前位置是一个矩形的后边界,那么需要将它的前边界取出候选队列,因为他的影响已经结束了,而且要... 阅读全文

posted @ 2015-08-11 19:46 F_G 阅读(150) 评论(0) 推荐(0)

使用java的迭代器对list进行遍历
摘要:List> vec2d = new LinkedList>();vec2d.add(Arrays.asList(new Integer[]{1,2,3}));vec2d.add(Arrays.asList(new Integer[]{4,null,5}));vec2d.add(null);vec2d... 阅读全文

posted @ 2015-08-11 19:40 F_G 阅读(700) 评论(0) 推荐(0)

[Leetcode] Kth largest element in an array
摘要:这个题目使用快排。这里涉及如何实现快排的问题。在[1]当中有一个快排的实现例子。在这里我们关注与快排的实现而不是这个问题本身,因为快排的问题解决了,这个问题就解决了快排的思想是选择一个元素,从两边进行遍历将遇到的第一对不满足序列关系的元素进行交换。关键是这里如何实现这种兑换1、可以在同时找到这两个元... 阅读全文

posted @ 2015-08-11 19:32 F_G 阅读(271) 评论(0) 推荐(0)

Java构造函数的public,private,protected
摘要:[1] http://blog.csdn.net/sunxing007/article/details/4362184 阅读全文

posted @ 2015-08-08 22:18 F_G 阅读(697) 评论(0) 推荐(0)

[Leetcode] Valid Anagram
摘要:一、可以使用排序的方式Java当中对于String,并没有实现相应的排序方法,如果需要对String当中的字符进行排序在需要进行转换,将String转化为char [], 再使用Arrays.sort()方法进行排序这样的时间复杂度是O(nlogn), 空间复杂度是O(1)二、如果需要更快的可以使用... 阅读全文

posted @ 2015-08-06 20:07 F_G 阅读(147) 评论(0) 推荐(0)

Java当中数组和容器之间的相互转换
摘要:一、容器转化到数组使用toArray()方法二、数组转化为容器,使用Arrays.asList(数组)会返回一个ArrayList,然后使用容器的方法可以对其进行操作。但是需要注意的是Arrays.asList中的数组不能使primitive的,应该是对象,int要是Integer。[1] http... 阅读全文

posted @ 2015-08-05 22:16 F_G 阅读(525) 评论(0) 推荐(0)

Java中的linkedList
摘要:Java中的LinkedList实际上是双向链表实现方式LinkedList list = new LinkedList();list.add(1);list.add(2)1->2这说明默认的add方式是在list的尾部添加了一个元素list.addLast(3);1->2->3list.addFi... 阅读全文

posted @ 2015-08-05 21:48 F_G 阅读(195) 评论(0) 推荐(0)

[Leetcode] Palindrome Linked List
摘要:一、因为要判断左右链表的断电的对称,可以先将节点取出,放到vector当中,然后使用随机访问的方式对其进行对称性判断空间复杂度为 O(n),时间复杂度为O(n)二、可否将空间复杂度降下来,可以采用两个指针,将链表分为两部分。dummy->head从dummy开始一个指针每次前进一步,另一个指针每次前... 阅读全文

posted @ 2015-08-02 12:04 F_G 阅读(125) 评论(0) 推荐(0)

[Leetcode] Maximal Square
摘要:一、枚举所有宽度为举着宽度的子矩阵,从中找出是否具有相应长度的正方形,复杂度为O(n^3);二、[1]中使用动态规划,decrease the complexity to O(n^2)dp[i][j] mean the maximum square which has right-down corn... 阅读全文

posted @ 2015-08-01 14:30 F_G 阅读(136) 评论(0) 推荐(0)