- 博客(38)
- 资源 (2)
- 收藏
- 关注
原创 Algorithms—257.Binary Tree Paths
思路:找到所有的路径,然后读数。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public
2015-08-31 12:17:21 325
原创 Algorithms—273.Integer to English Words
思路:拆成3位数一组进行拼接,考虑一下边界条件,写的比较冗余public class Solution { public String numberToWords(int num) { if (num==0) { return "Zero"; } Map map=new HashMap(); map.put("1", "One");
2015-08-31 11:54:54 368
原创 Algorithms—198.House Robber
思路:动态规划。ans[i]=Math.max(ans[i-2]+nums[i],ans[i-1])。public class Solution { public int rob(int[] nums) { int n=nums.length; if (n==0) { return 0; } int[] ans=new int[n+1
2015-08-26 11:00:05 286
原创 Algorithms—230.Kth Smallest Element in a BST
思路:每次进来数一下左侧的节点数数,如果恰好比左侧节点数多1,则返回根节点,小于在左侧,大于在右侧。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(in
2015-08-25 10:11:26 335
原创 Algorithms—235.Lowest Common Ancestor of a Binary Search Tree
思路:二叉搜索树,直接判断p.val和q.val于root.val的大小,如果同向,则同侧继续判断,如果不同向,则返回root。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; *
2015-08-24 16:05:46 269
原创 Algorithms—242.Valid Anagram
思路:等价于判定两个字符串字母组成是否相同。public class Solution { public boolean isAnagram(String s, String t) { if (s.length()!=t.length()) { return false; } Map smap=new HashMap(); Ma
2015-08-24 15:38:57 286
原创 Algorithms—264.Ugly Number II
思路:看了hint写的,每个新的ugly number是原来的ugly number *2 or *3 or *5 o获得的。public class Solution { public int nthUglyNumber(int n) { if (n<7) { return n; } List list=new ArrayList();
2015-08-24 15:24:29 319
原创 Algorithms—263.Ugly Number
思路:2,3,5依次尝试,能整除则整除,不能整除则返回false,整除到1则返回truepublic class Solution { public boolean isUgly(int num) { if (num==0) { return false; } while (num!=1) { if (num%2==0) { num/=
2015-08-24 14:55:13 218
原创 Algorithms—268.Missing Number
思路:限制线性时间复杂度,常数空间复杂度,刚开始以为是有序数组,直接读取,找到 nums[i]!=i即可,结果跑了一遍发现不是有序的,想到arrays.sort排序,但是这个方法太偷懒了。换另外一个思路,因为判定的数组是从0开始,只缺少一个(或者不缺少,不缺少就是取数组最大值+1),那么如果这个数组是完整的(从0开始,一个不少),那么nums[i]的总和等于i的总和。如果这个数组例如是0,……n-
2015-08-24 14:48:38 335
原创 Algorithms—164.Maximum Gap
思路:限定时间和空间复杂度为O(n),桶排序和计数排序都可以。取巧使用了Arrays.sort排序。public class Solution { public int maximumGap(int[] nums) { if (nums.length<2) { return 0; } Arrays.sort(nums); int an
2015-08-20 11:30:36 306
原创 Algorithms—152.Maximum Product Subarray
思路:以0分割,拆成非0数组;每个非0数组中,判断有几个负数,如果是偶数个,直接返回,如果不是,则计算2端的乘积直到第一个非负数,比较大小,截取。public class Solution { public int maxProduct(int[] nums) { List> totalList=new ArrayList>(); List list=n
2015-08-20 11:07:03 245
原创 Algorithms—128.Longest Consecutive Sequence
思路:限定了实践复杂度O(n),本来想直接用计数排序的原理解题的,但是发现测试数据尝试了从int的最小值到最大值,故直接调用arrays.sort,然后遍历一遍public class Solution { public int longestConsecutive(int[] nums) { if (nums.length<2) { return nums.lengt
2015-08-20 09:24:28 242
原创 Algorithms—120.Triangle
思路:动态规划,限定了空间复杂度为行数,从最底层开始动态规划。public class Solution { public int minimumTotal(List> triangle) { int[] ans=new int[triangle.get(triangle.size()-1).size()]; for (int i = 0; i <triangle.
2015-08-19 15:18:41 273
原创 Algorithms—112.Path Sum
思路:easy的题目,不用动态规划也过了/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */pub
2015-08-19 14:29:14 247
原创 Algorithms—97.Interleaving String
思路:动态规划,每次2条路可以选择,因为判定是否可以,所以只要第一条路正确即可。public class Solution { public boolean isInterleave(String s1, String s2, String s3) { return f(s1,s2,s3,new HashMap()); } public boolean f(S
2015-08-19 13:53:35 231
原创 Algorithms—80.Remove Duplicates from Sorted Array II
思路:遍历读取,做个布尔值判断是否重复了第二次。关键点:先获取前一个值。再进行覆盖。public class Solution { public int removeDuplicates(int[] nums) { if (nums.length<3) { return nums.length; } boolean flag=true; int
2015-08-18 17:41:48 233
原创 Algorithms—129.Sum Root to Leaf Numbers
思路:找到所有的路径,然后处理数字,用long防止超届再转化回来。。。。这思路居然也可以。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x)
2015-08-12 11:06:04 240
原创 Algorithms—113.Path Sum II
思路:遍历所有可能的路径,然后判断路径总和是否为目标值。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }
2015-08-11 12:45:31 415
原创 Algorithms-98.Validate Binary Search Tree
思路:本质是验证该树的中序遍历结果是否为排序好的数组。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }
2015-08-11 11:19:42 191
原创 Algorithms-94.Binary Tree Inorder Traversal
思路:中序遍历,递归,按照定义来做。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publi
2015-08-11 10:48:43 227
原创 Algorithms-91.Decode Ways
思路:f(0,s)=f(1,s)+f(2,s)public class Solution { public int numDecodings(String s) { if (s.length()==0) { return 0; } Map map=new HashMap(); return f(s,map); } pu
2015-08-11 10:07:10 277
原创 Algorithms-63.Unique Paths II
思路:还是动态规划,f(x,y)=f(x+1,y)+f(x,y+1)。f(终点)=1。public class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { if (obstacleGrid[0][0]==1||obstacleGrid[obstacleGrid.length-1]
2015-08-11 09:27:25 283
原创 Algorithms-64.Minimum Path Sum
思路:动态规划。f(x,y)=(x,y)+Min(f(x+1,y),f(x,y+1)),只是用这个递归会导致Time Limit Exceeded,再递归查询的同时建立一个hashmap,存储查询过的f(x,y),避免重复查询。public class Solution { public int minPathSum(int[][] grid) { int xl=grid.l
2015-08-11 08:29:43 198
原创 Algorithms—82.Remove Duplicates from Sorted List II
思路:首先查询第一个有效元素(不重复的),然后递归查询。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solut
2015-08-07 10:15:42 290
原创 Algorithms—143.Reorder List
思路:老套路,把ListNode的每个节点写进List,然后拼接/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class S
2015-08-07 09:19:33 298
原创 Algorithms—162.Find Peak Element
思路:这个题目比较简单,题目要求暗示了解题思路,要求时间复杂度为O(log n) ,也就是分治思想,那么二分法,比较中值和旁边的值谁大即可,取大的所在的一侧。public class Solution { public int findPeakElement(int[] nums) { if (nums.length==1) { return 0; }
2015-08-05 13:02:53 349
原创 Algorithms—166.Fraction to Recurring Decimal
思路:首先是边界问题,除数为int类型最小值时不好处理,直接将被除数和除数都转化成long型,然后是去掉负号;再先计算整数位,然后开始进行取余乘10的操作,并将每次余数写进map,直到读到循环。public class Solution { public String fractionToDecimal(int numerator, int denominator) { lo
2015-08-05 08:43:04 279
原创 Algorithms—199.Binary Tree Right Side View
思路:逐层查询所有的节点,然后找到最后一个节点的val。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }
2015-08-04 14:32:48 210
原创 Algorithms—116.Populating Next Right Pointers in Each Node
思路:不太能理解怎么跨树相连,直接遍历整个树,每层的node装入list中,然后遍历list,next连接。/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; *
2015-08-04 12:31:29 214
原创 Algorithms—215.Kth Largest Element in an Array
思路:排序,返回倒着数第k个值。public class Solution { public int findKthLargest(int[] nums, int k) { Arrays.sort(nums); return nums[nums.length-k]; }}耗时:300ms,中上游。
2015-08-04 10:26:15 197
原创 Algorithms—216.Combination Sum III
思路:循环list,逐个加值判断,保证每个子list的长度不超过k。public class Solution { public List> combinationSum3(int k, int n) { List> answerList = new ArrayList>(); for (int i = 1; i < 10; i++) { List list = new
2015-08-04 09:53:45 222
原创 Algorithms—240.Search a 2D Matrix II
思路:试了一下穷举,也通过了,提示没有足够的通过量以构造通过时间分布图,换了一个下台阶的方式,从最后一行第一个数字开始比较,如果该值小于target,则右移一步,如果大于,则向上移一行。结果时间仍然是600多ms。public class Solution { public boolean searchMatrix(int[][] matrix, int target) {
2015-08-03 16:41:41 223
原创 Algorithms—66.Plus One
思路:一个数组表示一个数,从左到右。然后加上一,主要就是判断下有几位是9。public class Solution { public int[] plusOne(int[] digits) { int i=digits.length-1; boolean flag= false; do { if (digits[i]<9) {
2015-08-03 16:02:01 284
原创 Algorithms—83.Remove Duplicates from Sorted List
思路:判断一个节点和节点的next是否为空,不为空的话比较两个的值,如果相同,改节点的next.next覆盖next,然后递归处理。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) {
2015-08-03 15:47:50 203
原创 Algorithms—165.Compare Version Numbers
思路:比较版本号,用"."来分割,注意split(''\\.');public class Solution { public int compareVersion(String version1, String version2) { String[] v1=version1.split("\\."); String[] v2=version2.spl
2015-08-03 15:35:00 230
原创 Algorithms—169.Majority Element
思路:题目特地强调了出现最多的书至少出现了一半的次数,那么,排序后取中值即可。public class Solution { public int majorityElement(int[] nums) { Arrays.sort(nums); return nums[nums.length/2]; }}耗时:400ms,中上游。
2015-08-03 12:21:28 277
原创 Algorithms—160.Intersection of Two Linked Lists
思路:提示限制了线性时间,常量空间。那么先读一遍2个ListNode的长度,然后截取掉长的多余的一部分,开始比较,如果不等于,则2段ListNode都取next继续比较。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; *
2015-08-03 11:13:06 287
原创 Algorithms—28.Implement strStr()
思路:没太理解题目的意思,直接暴力破解。public class Solution { public int strStr(String haystack, String needle) { if (needle.length()==0) { return 0; } for (int i = 0; i <haystack.length()-ne
2015-08-03 10:23:28 295
2积分系列——经典算法与人工智能在外卖物流调度中的应用
2018-04-26
Outlier Analysis 2nd Edition.pdf ——2积分系列
2018-03-28
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人