- 博客(17)
- 资源 (2)
- 收藏
- 关注
原创 Algorithms—208.Implement Trie (Prefix Tree)
思路:构造一个最多有26叉的树,为了方便,用map装子树,增加一个布尔值标志,布尔值为真,表示至此节点,有一个单词,布尔值为假,表示此节点不是某个单词的最后一个字母。class TrieNode { // Initialize your data structure here. Map sons=new HashMap(); char val; boolean
2015-10-30 16:57:17 255
原创 Algorithms—11.Container With Most Water
思路:从外层开始,计算蓄水量,每次移动一格左挡板或者右挡板,移动目前较短的一个。public class Solution { public int maxArea(int[] height) { int left=0; int right=height.length-1; int ans=(right-left)*Math.min(he
2015-10-30 09:36:21 330
原创 Algorithms—68.Text Justification
思路:主要是理解题目意思,注意最后一行要单独处理。public class Solution { public List fullJustify(String[] words, int maxWidth) { List> sumList=new ArrayList>(); int length=maxWidth; List list=ne
2015-10-29 12:34:39 376
原创 Algorithms—295.Find Median from Data Stream
思路:插入的时候用二分插入,时间复杂度O(lgn),取数的时候时间复杂度为O(1);class MedianFinder { List list=new ArrayList(); // Adds a number into the data structure. public void addNum(int num) { if(list.size()==
2015-10-28 15:59:19 303
原创 Algorithms—297.Serialize and Deserialize Binary Tree
思路:根据条件描述/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class C
2015-10-28 11:14:48 270
原创 Algorithms—114.Flatten Binary Tree to Linked List
思路:用一个list按照题目要求遍历数组,存储,然后读取。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } *
2015-10-26 17:55:35 369
原创 Algorithms—222.Count Complete Tree Nodes
思路:利用完全二叉树的对称性质,左右分别探索长度,如果相同,直接返回2^h-1,否则分别计算左树和右树/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(i
2015-10-23 17:28:57 266
原创 Algorithms—234.Palindrome Linked List
思路:快慢双指针扫描,找到中点,从中点开始原地翻转,拿翻转后的进行比较,注意比较的终止条件用翻转后的为空,不然需要判断原链表长度的奇偶性。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x)
2015-10-23 14:50:11 293
原创 Algorithms—131.Palindrome Partitioning
思路:动态规划。。。。public class Solution { Map>> map=new HashMap>>(); public List> partition(String s) { if (map.get(s)!=null) { return map.get(s); } List> ans=new ArrayList>
2015-10-20 16:19:45 316
原创 Algorithms—140.Word Break II
思路:动态规划,很简单,就是写的比较糟糕,而且好多逻辑错误判定是time limit。。也是蛋疼。注意用一个新map存储走不通的路径。public class Solution { Map> map=new HashMap>(); Map bmap=new HashMap(); public List wordBreak(String s, Set wordDict) {
2015-10-20 15:57:10 380
原创 Algorithms—278.First Bad Version
思路:这个题目真的是蛋疼,版本号有21亿个吗。。。二分法求解,注意二分的时候不要溢出。/* The isBadVersion API is defined in the parent class VersionControl. boolean isBadVersion(int version); */public class Solution extends VersionCo
2015-10-09 16:35:11 277
原创 Algorithms—279.Perfect Squares
思路:动态规划,然后发现超时,思考了下原因,估计是每次尝试的时候都要尝试1进去,然后1显示在大树的时候不是最快的,于是换了一个测试方法,顺带优化了下算法,311ms又想了下,关于最小值处理的时候应该带入上一次测试的平方数,下一次处理的平方数不能大于上一次处理的平方数,不过这样不利于冬天规划,所以还是算了。public class Solution { Map map=new Hash
2015-10-09 16:21:58 378
原创 Algorithms—89.Gray Code
思路:手动写了3位和4位的格雷码,发现了规律,3位格雷码变成4位的,原3位的不动,前面加0,然后逆向原3位的,前面加1即可。public class Solution { public List grayCode(int n) { List ans=new ArrayList(); List list=new ArrayList(); if (n==0)
2015-10-08 17:26:21 318
原创 Algorithms—229.Majority Element II
思路:逐个向后比较⌊ n/3 ⌋位,如果有,则接着该数后面一个开始比较。返回结果前验证下是否有2个相同的答案。public class Solution { public List majorityElement(int[] nums) { List ans=new ArrayList(); if (nums.length==0) { return an
2015-10-08 15:42:56 278
原创 Algorithms—287.Find the Duplicate Number
思路:1.不准排序,2.常数空间使用,3.小于O(n^2)的时间复杂度,4.不是每个数都会出现。1.不准排序限定了不能直接通过相邻比较。2.常数空间使用限定了不能开辟额外的线性空间存储做hash查询3.小于O(n^2)限定了不能暴力破解比较。4.不是每个数都会出现限定了不能使用求和比较。那么,只能用二分法了,写了一个accept的方法,但是不会证明时间复杂度低于O(n^2),只
2015-10-08 15:20:48 312
原创 Algorithms—290.Word Pattern
思路:一对一对应,居然只用了2ms,感觉网站有问题。public class Solution { public boolean wordPattern(String pattern, String str) { String[] strs=str.split(" "); if (pattern.length()!=strs.length) { r
2015-10-08 12:59:50 245
原创 Algorithms—289.Game of Life
思路:按照题目的要求写成代码,居然只有28ms。public class Solution { public void gameOfLife(int[][] board) { int[][] newboard=new int[board.length][board[0].length]; for (int i = 0; i < board.length;
2015-10-08 12:52:01 268
2积分系列——经典算法与人工智能在外卖物流调度中的应用
2018-04-26
Outlier Analysis 2nd Edition.pdf ——2积分系列
2018-03-28
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人