- 博客(64)
- 资源 (2)
- 收藏
- 关注
原创 Algorithms—205.Isomorphic Strings
思路:分别给两个字符串设计2个map,同时读取,每读一个,去自己的map中搜索,看是否有值且相等,如果否,则返回false,否则同时将此字符串写入map。 public class Solution { public boolean isIsomorphic(String s, String t) { if (s.length()!=t.length()) { re
2015-07-30 22:42:07 267
原创 Algorithms—217.Contains Duplicate
思路:遍历 public class Solution { public boolean containsDuplicate(int[] nums) { Map map=new HashMap(); for(int i=0;i<nums.length;i++){ if(map.get(nums[i])==null){
2015-07-30 22:01:06 265
原创 Algorithms—223.Rectangle Area
思路:理清关系,考虑好各种情况。 public class Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { return (H-F)*(G-E)+(D-B)*(C-A)-(((H>D?D:H)>(F>B?F:B))?((H>D?D:H)-
2015-07-30 21:48:25 270
原创 Algorithms—226.Summary Ranges
思路:左右互换,递归做。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public clas
2015-07-30 21:10:02 319
原创 Algorithms—228.Summary Ranges
思路:遍历,按规则插入list。 public class Solution { public List summaryRanges(int[] nums) { List list=new ArrayList(); if (nums.length==0) { return list; } int k=0; if (nums.l
2015-07-30 20:44:21 323
原创 Algorithms—231.Power of Two
思路:既然是int,那么2的幂是有限的,穷举比较。当然,正常的速度是除2进行比较。 public class Solution { public boolean isPowerOfTwo(int n) { if(n==1||n==2||n==4||n==8||n==16||n==32||n==64||n==128||n==256||n==512||n==1024||n==
2015-07-30 20:23:28 263
原创 Algorithms—237.Delete Node in a Linked List
思路:把这个节点的值换成其下一个节点的值,然后跳过下一个节点。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class So
2015-07-30 16:45:14 298
原创 Algorithms—86.Partition List
思路:按照要求,把节点分为2个list,递归。然后再合并。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solu
2015-07-30 16:12:47 298
原创 Algorithms—90.Subsets II
思路:78题的方法拿过来,最后做一次过滤。非常傻X的思路。 public class Solution { public List> subsetsWithDup(int[] nums) { for (int i = 0; i < nums.length; i++) { for (int j = i+1; j < nums.length; j++) { if (n
2015-07-29 16:24:25 283
原创 Algorithms—78.Subsets
思路:先排序,然后再处理。这个方法太垃圾了,就不多说了。 public class Solution { public List> subsets(int[] nums) { for (int i = 0; i < nums.length; i++) { for (int j = i+1; j < nums.length; j++) { if (nums[i
2015-07-29 16:10:47 287
原创 Algorithms—203.Remove Linked List Elements
思路:把符合要求的节点装入list,重新连接。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution {
2015-07-29 14:19:36 293
原创 Algorithms—206.Reverse Linked List
思路:每个节点装进list,反向拼装 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution {
2015-07-29 13:43:26 302
原创 Algorithms—238.Product of Array Except Self
思路:逐个相乘,每位相除,注意有0的情况。 public class Solution { public int[] productExceptSelf(int[] nums) { int length=nums.length; int k=0; boolean flag=true; int product=1; for (int
2015-07-28 12:55:58 241
原创 Algorithms—61.Rotate List
思路:找到断点,打断拼接。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { publi
2015-07-27 15:19:52 266
原创 Algorithms—48.Rotate Image
思路:本来自信空间思维能力,结果转晕了,还是画了个图才解决。 public class Solution { public void rotate(int[][] matrix) { int n=matrix.length; for (int i = 0; i < n/2; i++) { for (int j = i; j <n-i-1; j++) {
2015-07-27 15:00:00 249
原创 Algorithms—43.Multiply Strings
思路:按照乘法的定义,逐位相乘。 public class Solution { public String multiply(String num1, String num2) { if (num1.equals("0")||num2.equals("0")) { return "0"; } if (num1.length()<num2.length
2015-07-27 11:05:37 256
原创 Algorithms—31.Next Permutation
思路:首先找到最后一段从大到小的排序子列,然后将其反向,找到这个子列开头的数字,与该子列比较,找到第一个比他大的数字,交换。 public class Solution { public void nextPermutation(int[] nums) { if (nums.length>1) { int a=0; for (int i =
2015-07-24 13:53:51 264
原创 Algorithms—25.Reverse Nodes in k-Group
思路:把每段需要交换的数值读出来,然后再按要求赋值回去。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solut
2015-07-24 11:26:01 228
原创 Algorithms—24.Swap Nodes in Pairs
思路:递归,然后交换相邻的两个值。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { p
2015-07-24 11:02:29 277
原创 Algorithms—23.Merge k Sorted Lists
思路:分治思想,拆分成2个进行处理。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution {
2015-07-24 10:21:46 430
原创 Algorithms—21.Merge Two Sorted Lists
思路:很简答, 递归比较复制。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { pub
2015-07-23 17:16:19 278
原创 Algorithms—19.Remove Nth Node From End of List
思路:读入list中,考虑首位的因素,截断。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution {
2015-07-23 16:51:27 252
原创 Algorithms—3.Longest Substring Without Repeating Characters
思路:遍历查看字符串,同时存入list和map中,通过list记住顺序,通过map记住位置。 public class Solution { public int lengthOfLongestSubstring(String s) { int answer=0; Map map=new HashMap(); List list=new L
2015-07-21 17:43:41 371
原创 Algorithms—2.Add Two Numbers
思路:递归逐个相加,进位用布尔值传递。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution {
2015-07-21 17:03:51 298
原创 Algorithms—172.Factorial Trailing Zeroes
思路:本质上就是求最后的积中有多少个5的因数。 public class Solution { public int trailingZeroes(int n) { int sum=0; while (n!=0) { n/=5; sum+=n; } return sum; } } 耗时:332ms,下游,换一种写法可以
2015-07-21 14:16:15 275
原创 Algorithms—150.Evaluate Reverse Polish Notation
思路:所有的数字存入list,读到符号取出后两个运算完放回去,最后返回list第一个值。 public class Solution { public int evalRPN(String[] tokens) { List list = new ArrayList(); for (int i = 0; i < tokens.length; i++) { if (tokens
2015-07-20 11:32:42 201
原创 Algorithms—149.Max Points on a Line
思路:3点判断一直线,遍历判断,注意重点。 /** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * Point(int a, int b) { x = a; y = b; } * } */ public class S
2015-07-20 11:10:41 287
原创 Algorithms—145.Binary Tree Postorder Traversal
思路:跟144题一样,换一个赋值位置即可。都是按照定义来的。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } *
2015-07-20 10:24:09 245
原创 Algorithms—144.Binary Tree Preorder Traversal
思路:比较简单,按照定义操作即可。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public
2015-07-20 10:21:01 197
原创 Algorithms—138.Copy List with Random Pointer
思路:很简单的一题,next当作主线递归赋值,random作为支线附上即可。 /** * Definition for singly-linked list with a random pointer. * class RandomListNode { * int label; * RandomListNode next, random; * RandomLis
2015-07-20 10:06:55 254
原创 Algorithms—142.Linked List Cycle II
思路:双指针跑圈,跑到有圈的时候快指针停止,重新做一个指针从头开始,跟慢指针一样以一次一步的速度跑,跑到相同时返回,因为多余的长度L等于圈长M的倍数+慢指正回到起点的步数。 快指针速度:2,慢指针速度:1 多余长度:L 圈长:M 快慢指针相遇时间:T 相遇地点距离圈起始位置:N 2T=L+X*M+N; T=L+Y*M+N; => T=(X-Y)*M; => L=(X-
2015-07-17 17:51:50 252
原创 Algorithms—141.Linked List Cycle
思路:经典跑圈。 /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ pub
2015-07-16 17:57:12 203
原创 Algorithms—108.Convert Sorted Array to Binary Search Tree
思路:二分法,每次取终点给TreeNode赋值,然后左右递归。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; }
2015-07-16 13:19:12 224
原创 Algorithms—106.Construct Binary Tree from Inorder and Postorder Traversal
思路:根据后序先找出节点,然后去中序找出左右,依次递归。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }
2015-07-16 13:05:58 274
原创 Algorithms—105.Construct Binary Tree from Preorder and Inorder Traversal
思路:根据前序先找出节点,然后去中序找出左右,依次递归。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }
2015-07-16 13:00:35 226
原创 Algorithms—107.Binary Tree Level Order Traversal II
思路:抄102题,循环载入每层的node,逆向赋值即可。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }
2015-07-16 12:33:26 244
原创 Algorithms—104.Maximum Depth of Binary Tree
思路:分左右两路递归查询,查到某个节点下left和right都为null时返回,比较左右的值,取大的 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(in
2015-07-16 12:28:31 251
原创 Algorithms—111.Minimum Depth of Binary Tree
思路:分左右两路递归查询,查到某个节点下left和right都为null时返回,比较左右的值,取小的 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(in
2015-07-16 12:26:04 220
原创 Algorithms—103.Binary Tree Zigzag Level Order Traversal
思路:参考102题,依然是先放TreeNode进去,然后每次反向存入,注意left和right要跟着方向来。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNod
2015-07-15 16:41:03 238
原创 Algorithms—102.Binary Tree Level Order Traversal
思路:逐层把TreeNode添加到list中然后逐层读取其val。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; }
2015-07-15 13:55:43 265
2积分系列——经典算法与人工智能在外卖物流调度中的应用
2018-04-26
Outlier Analysis 2nd Edition.pdf ——2积分系列
2018-03-28
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人