www

导航

2019年2月25日 #

从上往下打印二叉树

摘要: public ArrayList PrintFromTopToBottom(TreeNode root) { ArrayList ret = new ArrayList(); if(root==null) return ret; Queue queue = new LinkedList(); queue.offer(root); while(queue.s... 阅读全文

posted @ 2019-02-25 23:20 www_practice 阅读(129) 评论(0) 推荐(0) 编辑

合并两个排序链表

摘要: public ListNode Merge(ListNode list1,ListNode list2) { if(list1==null) return list2; if(list2==null) return list1; ListNode head=new ListNode(0); ListNode current = head; ... 阅读全文

posted @ 2019-02-25 22:58 www_practice 阅读(106) 评论(0) 推荐(0) 编辑

反转链表

摘要: public ListNode ReverseList(ListNode head) { if(head==null||head.next==null) return head; ListNode pReverseHead=null; ListNode pNode=head; ListNode pPrev=null; while... 阅读全文

posted @ 2019-02-25 21:06 www_practice 阅读(165) 评论(0) 推荐(0) 编辑

二进制中1的个数

摘要: public int NumberOf1(int n) { int count = 0; while (n!=0) { n = n&(n-1); count++; } return count; } 阅读全文

posted @ 2019-02-25 20:47 www_practice 阅读(93) 评论(0) 推荐(0) 编辑

链表中倒数第k个结点

摘要: public ListNode FindKthToTail(ListNode head,int k) { if(head==null) return head; ListNode pre=head; ListNode last=head; for(int i=0; i<k; i++){ if(last==null) return null; ... 阅读全文

posted @ 2019-02-25 20:45 www_practice 阅读(128) 评论(0) 推荐(0) 编辑

调整数组顺序使奇数位于偶数前面

摘要: public void reOrderArray(int [] array) { if(array==null||array.length==0) return; int start=0, end=array.length-1; while(startstart&&array[end]%2!=0) end--; if(start<end){ ... 阅读全文

posted @ 2019-02-25 19:22 www_practice 阅读(126) 评论(0) 推荐(0) 编辑

重建二叉树

摘要: public TreeNode reConstructBinaryTree(int [] pre,int [] in) { if(pre==null||in==null||pre.length!=in.length||pre.lengthpe || is>ie) return null; int val = pre[ps]; int ind... 阅读全文

posted @ 2019-02-25 19:10 www_practice 阅读(123) 评论(0) 推荐(0) 编辑

旋转数组的最小数字

摘要: public int minNumberInRotateArray(int [] array) { if(array==null||array.length==0) return -1; int start=0, end=array.length-1; int mid=start; while(array[start]>=arra... 阅读全文

posted @ 2019-02-25 18:59 www_practice 阅读(142) 评论(0) 推荐(0) 编辑

MergeKLists

摘要: public ListNode mergeKLists(ListNode[] lists) { if(lists==null||lists.length==0) return null; PriorityQueue minHeap=new PriorityQueue((o1, o1)->{ return o1.val-o2.val; ... 阅读全文

posted @ 2019-02-25 18:51 www_practice 阅读(166) 评论(0) 推荐(0) 编辑

Top K Frequent Elements

摘要: public List topKFrequent(int[] nums, int k){ List ret = new ArrayList(); if (nums==null || nums.length map = new HashMap(); for (int n:nums) map.put(n,map.getOrDefault(n,0)+1)... 阅读全文

posted @ 2019-02-25 18:23 www_practice 阅读(116) 评论(0) 推荐(0) 编辑