- 博客(22)
- 资源 (2)
- 收藏
- 关注
原创 Algorithms—148.Sort List
思路:归并排序。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public Lis
2015-09-22 12:25:55 300
原创 Algorithms—127.Word Ladder
思路:因为路径可能循环,如果采用深度优先遍历,则要考虑路径回头的判定,这个不管是时间复杂度还是空间复杂度开销巨大(每条路都要走到死且在走的过程中要记住所有曾经走过的路线)。所以采用广度优先遍历。但是不断地超时,百度了下超时的原因,原来采用的比较相邻(只差一个字母)的方法是逐位比较两个字符串,然后将结果记录在map中,如果字典(wordList)中的元素是n个,则该方法的时间复杂度是O(n^2),所
2015-09-18 15:27:36 380
原创 Algorithms—239.Sliding Window Maximum
思路:本质就是维持一个最大队,那么插入的时候拿该数从队伍最后开始比较,删除掉所有小于该数的队伍成员,然后插入该数。取出的时候比较该数是否是该队第一个数,如果不是,则队伍不变。public class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if (nums.length==0||k==0||k
2015-09-16 16:39:49 300
原创 Algorithms—87.Scramble String
思路:原来以为题目值允许中分,后来才发现可以随意拆分,那么按照拆分距离,比较的数组可以分为前半段与原字符串前半段对应,后半段与原字符串后半段对应,或者颠倒,但是要保证程度相同,然后递归求解。使用动态规划防止超时。public class Solution { Map map=new HashMap(); public boolean isScramble(String s1, Str
2015-09-16 15:40:18 423
原创 Algorithms—49.Group Anagrams
思路:先排序原数组,然后把每个String拆成char[]排序重组,放回新数组中,根据相等情况存储编号,然后读取编号。想了一个思路,应该可以把String[]数组拆成char[][]二维数组,然后进行排序,读取。但这个思路没实现。public class Solution { public List> groupAnagrams(String[] strs) { A
2015-09-15 10:42:50 313
原创 Algorithms—201.Bitwise AND of Numbers Range
思路:首先判断是否相等,如果相等则返回n(或m),这步正好过滤掉双0的情况。然后求出m&n和m^n的值,m^n的值的二进制形式表示从哪一位开始不同,用m&n的值抹去后面的部分即可。public class Solution { public int rangeBitwiseAnd(int m, int n) { if(m==n){ return n;
2015-09-14 17:25:20 275
原创 Algorithms—40.Combination Sum II
思路:先排序,然后逆向取数,如果这个数小于目标值,则将该数于目标的差值带入,递归查询。public class Solution { public List> combinationSum2(int[] candidates, int target) { Arrays.sort(candidates); return f(candidates,targe
2015-09-14 11:41:37 285
原创 Algorithms—18.4Sum
思路:跟三和问题本质一样,加上一层循环,两边设置端点,然后内部双端扫描public class Solution { List> ans=new ArrayList>(); public List> fourSum(int[] nums, int target) { if (nums.length<4) { return ans; } Arrays
2015-09-14 11:07:51 256
原创 Algorithms—16.3Sum Closest
思路:跟三和问题本质一样,双端扫描。public class Solution { int ans=Integer.MIN_VALUE; public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int length=nums.length; for (i
2015-09-11 17:09:32 283
原创 Algorithms—15.3Sum
思路:先排序,然后读取数组,接着从剩下的数组中开始两端扫描到符合条件的两个数。注意去重。public class Solution { List> ans=new ArrayList>(); public List> threeSum(int[] nums) { Arrays.sort(nums); if (nums.length<3) { retur
2015-09-10 17:41:42 348
原创 Algorithms—81.Search in Rotated Sorted Array II
思路:可以重复的话,比如数组中只有1个2,其他都是1,这个2可以出现在任何位置,所以直接遍历吧。 public boolean search(int[] nums, int target) { for (int i = 0; i < nums.length; i++) { if (nums[i]==target) { return true; }
2015-09-09 09:43:20 328
原创 Algorithms—95.Unique Binary Search Trees II
思路:再上一题的基础上继续使用动态规划。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */pub
2015-09-08 17:24:37 292
原创 Algorithms—133.Balanced Binary Tree
思路:首先遍历所有的元素,添加到map中,然后读取map,给每个元素编号,放进list中,同时新建list存储每个复制了lable的元素。最后读取原list,查看该list每个邻居,去map中查看这些邻居的编号,去新list中找到对应的邻居,添加到对应的元素的邻居中。/** * Definition for undirected graph. * class UndirectedGraph
2015-09-08 17:04:45 379
原创 Algorithms—110.Balanced Binary Tree
思路:比较左树和右树的深度,如果相差不超过1,继续判断左树和右树是否为平衡树。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val
2015-09-08 16:34:15 278
原创 Algorithms—92.Reverse Linked List II
思路:遍历ListNode,存入list中,然后反向/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solutio
2015-09-07 15:26:11 267
原创 Algorithms—45.Jump Game II
思路:动态规划。public class Solution { public int jump(int[] nums) { if (nums.length==1) { return 0; } int max=0; int[] steps=new int[nums.length]; steps[0]=0; fo
2015-09-07 14:49:52 308
原创 Algorithms—39.Combination Sum
思路:动态规划。public class Solution { public List> combinationSum(int[] candidates, int target) { Arrays.sort(candidates); return f(candidates, target, new HashMap>>()); } public Lis
2015-09-07 12:44:10 280
原创 Algorithms—139.Word Break
思路:动态查询。public class Solution { public boolean wordBreak(String s, Set wordDict) { return f(s,wordDict,new HashMap()); } public boolean f(String s, Set wordDict,Map map){ if
2015-09-06 17:29:45 326
原创 Algorithms—275.H-Index II
思路:二分法。public class Solution { public int hIndex(int[] citations) { if (citations.length==0) { return 0; } Arrays.sort(citations); int s=0; int e=citations.leng
2015-09-06 15:42:56 250
原创 Algorithms—274.H-Index
思路:排序,倒着读。public class Solution { public int hIndex(int[] citations) { Arrays.sort(citations); int ans=0; for (int i = citations.length-1; i >=0 ; i--) { if (citations
2015-09-06 15:27:52 344
原创 Algorithms—79.Word Search
思路:需要记录原来走的路径,之前写的方法是用map来装,耗时太长,参考了下别人的方法。public class Solution { public boolean exist(char[][] board, String word) { if (word==null||word.length()==0||board==null||board.length==0) { r
2015-09-02 16:01:23 338
原创 Algorithms—96.Unique Binary Search Trees
思路:动态规划,可以分为左树和右树,左树和右树加起来的元素和为n-1。public class Solution { public int numTrees(int n) { return f(n,new HashMap()); } public int f(int n,Map map){ if (map.get(n)!=null) { ret
2015-09-02 13:01:42 347
2积分系列——经典算法与人工智能在外卖物流调度中的应用
2018-04-26
Outlier Analysis 2nd Edition.pdf ——2积分系列
2018-03-28
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人