F_G

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

导航

2015年8月12日 #

[Leetcode] String to Integer (atoi)

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

posted @ 2015-08-12 21:59 F_G 阅读(115) 评论(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 阅读(236) 评论(0) 推荐(0) 编辑

[Leetcode] Longest Substring Without Repeating Characters

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

posted @ 2015-08-12 21:20 F_G 阅读(150) 评论(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 阅读(137) 评论(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 阅读(172) 评论(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 阅读(185) 评论(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 阅读(145) 评论(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 阅读(155) 评论(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 阅读(201) 评论(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 阅读(195) 评论(0) 推荐(0) 编辑

[Leetcode] Add Two Numbers

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

posted @ 2015-08-12 20:15 F_G 阅读(164) 评论(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 阅读(166) 评论(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 阅读(150) 评论(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 阅读(102) 评论(0) 推荐(0) 编辑

[Leetcode] Permutation Sequence

摘要: crute method is RE!! 阅读全文

posted @ 2015-08-12 17:09 F_G 阅读(96) 评论(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 阅读(143) 评论(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 阅读(180) 评论(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 阅读(175) 评论(0) 推荐(0) 编辑