02 2018 档案

摘要:https://leetcode.com/problems/symmetric-tree/description/Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is ... 阅读全文
posted @ 2018-02-28 23:53 davidnyc 阅读(117) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/same-tree/description/Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally iden... 阅读全文
posted @ 2018-02-28 23:19 davidnyc 阅读(154) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/count-univalue-subtrees/description/Given a binary tree, count the number of uni-value subtrees.A Uni-value subtree means all nodes of the subtree have the same value.For... 阅读全文
posted @ 2018-02-28 11:11 davidnyc 阅读(111) 评论(0) 推荐(0)
摘要:网上还有一种做法用STACK 现在无法理解,以后再弄! 阅读全文
posted @ 2018-02-28 05:50 davidnyc 阅读(135) 评论(0) 推荐(0)
摘要:given an array, and an element to insert, and the position to insert this element,return a new array with the element inserted1,2,3,4,5,6 -> 1,2,3,15,4,5,6 1 public static void main(String[] args) {... 阅读全文
posted @ 2018-02-28 03:20 davidnyc 阅读(201) 评论(0) 推荐(0)
摘要:用一次遍历,一个指针完成 2,3,8,4,1 -> 1,4,8,3,2 1 public static void main(String[] args) { 2 int[] org = {2,3,8,4,1 } ; 3 int[] res = reverseArray(org); 4 print(res); 5 } 6 7 ... 阅读全文
posted @ 2018-02-28 02:22 davidnyc 阅读(170) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/invert-binary-tree/description/Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1time: o(n) : n nodes space:... 阅读全文
posted @ 2018-02-27 23:41 davidnyc 阅读(105) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/binary-tree-inorder-traversal/description/ Given a binary tree, return the inorder traversal of its nodes' values. For e 阅读全文
posted @ 2018-02-27 09:32 davidnyc 阅读(118) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/binary-tree-preorder-traversal/description/ Given a binary tree, return the preorder traversal of its nodes' values. For 阅读全文
posted @ 2018-02-27 09:29 davidnyc 阅读(116) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/maximum-depth-of-binary-tree/description/Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node do... 阅读全文
posted @ 2018-02-27 02:58 davidnyc 阅读(112) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/balanced-binary-tree/description/Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as:a binary tree in wh... 阅读全文
posted @ 2018-02-27 02:39 davidnyc 阅读(136) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/binary-tree-postorder-traversal/description/ Description Implement an iterative, post-order traversal of a given binary 阅读全文
posted @ 2018-02-27 01:31 davidnyc 阅读(176) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/sort-list/description/Sort a linked list in O(n log n) time using constant space complexity.找到中点之后的处理,和 143 "Reorder List" 是一样的 : 然后把tail 传后面去 ListNode tail = mid.next ;/... 阅读全文
posted @ 2018-02-26 06:19 davidnyc 阅读(167) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/intersection-of-two-arrays/description/Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Not... 阅读全文
posted @ 2018-02-26 01:33 davidnyc 阅读(161) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/reorder-list/description/Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.... 阅读全文
posted @ 2018-02-25 23:51 davidnyc 阅读(118) 评论(0) 推荐(0)
摘要:取中点: 翻转: 合并: 插入:使用DUMMY head 避免了头部不确定, 头部为空等各种情况 阅读全文
posted @ 2018-02-25 12:25 davidnyc 阅读(279) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/find-peak-element/description/A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return... 阅读全文
posted @ 2018-02-25 11:56 davidnyc 阅读(93) 评论(0) 推荐(0)
摘要:这个笨方法也能通过 阅读全文
posted @ 2018-02-25 11:21 davidnyc 阅读(125) 评论(0) 推荐(0)
摘要:与Find Minimum in Rotated Sorted Array类似。因为rotate, 所以不能直接用Binary Search, 需要进行 二次判定。 case 1: nums[mid] == target, return mid. case 2: nums[mid] < nums[r 阅读全文
posted @ 2018-02-25 09:49 davidnyc 阅读(137) 评论(0) 推荐(0)
摘要:Given a 2D matrix that contains integers only, which each row is sorted in an ascending order. The first element of next row is larger than (or equal 阅读全文
posted @ 2018-02-25 04:34 davidnyc 阅读(229) 评论(0) 推荐(0)
摘要:Given a target integer T and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to T. Assumptions There can 阅读全文
posted @ 2018-02-25 03:53 davidnyc 阅读(184) 评论(0) 推荐(0)
摘要:Given a target integer T and an integer array A sorted in ascending order, find the index of the last occurrence of T in A or return -1 if there is no 阅读全文
posted @ 2018-02-25 00:18 davidnyc 阅读(207) 评论(0) 推荐(0)
摘要:Given a target integer T and an integer array A sorted in ascending order, find the index of the first occurrence of T in A or return -1 if there is n 阅读全文
posted @ 2018-02-25 00:08 davidnyc 阅读(213) 评论(0) 推荐(0)
摘要:Given a target integer T and an integer array A sorted in ascending order, find the index i such that A[i] == T or return -1 if there is no such index 阅读全文
posted @ 2018-02-24 23:53 davidnyc 阅读(145) 评论(0) 推荐(0)
摘要:(1) 先找中点(2) 再reverse后半段(3) 不用管两个子linked list的长度是否相等,从各个子链表的头开始一个一个比较。值不相等就返回false,相等就继续移动。 当取中点时,一定要 fast.next.next != null 阅读全文
posted @ 2018-02-24 10:13 davidnyc 阅读(178) 评论(0) 推荐(0)
摘要:设:链表头是X,环的第一个节点是Y,slow和fast第一次的交点是Z。各段的长度分别是a,b,c,如图所示。环的长度是L。slow和fast的速度分别是qs,qf。 阅读全文
posted @ 2018-02-24 09:10 davidnyc 阅读(161) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/add-two-numbers/description/You are given two non-empty linked lists representing two non-negative integers.The digits are stored in reverse order and each of their nodes... 阅读全文
posted @ 2018-02-23 11:34 davidnyc 阅读(150) 评论(0) 推荐(0)
摘要:time: o(n) complexity: o(1) 阅读全文
posted @ 2018-02-23 10:06 davidnyc 阅读(109) 评论(0) 推荐(0)
摘要:230,82,83 是一类题 time o(1) space o(1) 阅读全文
posted @ 2018-02-23 08:27 davidnyc 阅读(97) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return... 阅读全文
posted @ 2018-02-23 07:06 davidnyc 阅读(120) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/partition-list/description/ Given a linked list and a target value T, partition it such that all nodes less than T are l 阅读全文
posted @ 2018-02-23 02:38 davidnyc 阅读(123) 评论(0) 推荐(0)
摘要:Description Insert a value in a sorted linked list. Examples L = null, insert 1, return 1 -> null L = 1 -> 3 -> 5 -> null, insert 2, return 1 -> 2 -> 阅读全文
posted @ 2018-02-23 01:43 davidnyc 阅读(195) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->... 阅读全文
posted @ 2018-02-22 03:20 davidnyc 阅读(138) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/delete-node-in-a-linked-list/description/Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked... 阅读全文
posted @ 2018-02-22 00:16 davidnyc 阅读(117) 评论(0) 推荐(0)
摘要:千万注意,不要遍历两遍 什么 while(oddcurr) while(evencurr) 因为underline 的链表是一个,所以遍历第一遍之后就已经把 next 的关系搞混了, 后面那个遍历就不准确了。 阅读全文
posted @ 2018-02-21 23:32 davidnyc 阅读(147) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/merge-two-sorted-lists/description/Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two ... 阅读全文
posted @ 2018-02-21 22:58 davidnyc 阅读(125) 评论(0) 推荐(0)
摘要:2-1-4-3-5 5 自己就弹了,所以没有后面的处理,所以(head=3)head.next 还是 直接连 last pop newhead(5) 所以是 2-1-4-3-5 要深刻理解当时stack 里 head = 3 newhead=5 这一点! 阅读全文
posted @ 2018-02-21 12:54 davidnyc 阅读(121) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/linked-list-cycle/description/ time: o(n) space: o(1) Follow up:Can you solve it without using extra space? if you use e 阅读全文
posted @ 2018-02-21 12:04 davidnyc 阅读(122) 评论(0) 推荐(0)
摘要: 阅读全文
posted @ 2018-02-21 11:29 davidnyc 阅读(102) 评论(0) 推荐(0)
摘要:recursive: assume Node2 is the head, subproblem return: null<-Node3 <- Node4 then you want to change to ListNode newHead = reverse(head.next) will gen 阅读全文
posted @ 2018-02-21 11:11 davidnyc 阅读(115) 评论(0) 推荐(0)
摘要:time complexity: offer(), size(), isEmpty(): o(1) for poll and peek: worst case: offer n times, peek/poll only once: o(n) average case: offer n times, 阅读全文
posted @ 2018-02-20 23:50 davidnyc 阅读(179) 评论(0) 推荐(0)
摘要:Stack: LIFO, the Stack is a deprecated interface we should use Deque interface. The implementation can use LinkedList or array push, pop, peek, size() 阅读全文
posted @ 2018-02-20 23:07 davidnyc 阅读(140) 评论(0) 推荐(0)
摘要:queue: FIFO, Queue is interface, the implementation could be LinkedList or array offer, poll, peek, size() isEmpty() 阅读全文
posted @ 2018-02-20 22:59 davidnyc 阅读(98) 评论(0) 推荐(0)
摘要:时间: o(n) 空间 o(1): 没有recursion, 只有当前一层的call stack sorting 如果是general sorting algo(1:不能对数组的性质加以限制) comparison based(2: sorting 只能通过两个数的比较来进行) , 最好的时间复杂度 阅读全文
posted @ 2018-02-20 08:08 davidnyc 阅读(199) 评论(0) 推荐(0)
摘要:时间复杂度, 空间复杂度 average: nlogn : logn 层 每层 n 次排序 n n/2 n/2 n/4 n/4 n/4 n/4 worst: 每次取最大或者最小值 阅读全文
posted @ 2018-02-15 03:03 davidnyc 阅读(114) 评论(0) 推荐(0)
摘要:1 public class MergeSort { 2 3 public static void sortIntegers(int[] array) { 4 // write your code here 5 if (array == null || array.length ==0 ) { 6 return ; ... 阅读全文
posted @ 2018-02-15 00:23 davidnyc 阅读(137) 评论(0) 推荐(0)
摘要:1 /* 2 * 1: time complexity o(n^2) 3 * 2: good performance for items around 10-20: better than merge sort and quick sort 4 * 3: no extra space needed 5 * */ 6 public class SelectSort { 7 p... 阅读全文
posted @ 2018-02-14 23:04 davidnyc 阅读(174) 评论(0) 推荐(0)