leetcode
xiaoo_gan
做一个快乐的程序员
展开
-
61 Rotate List
题目链接:rotate-list/** * * Given a list, rotate the list to the right by k places, where k is non-negative. * For example: * Given 1->2->3->4->5->NULL and k = 2, * return 4->5->1->2->3->NULL原创 2015-03-14 23:49:12 · 704 阅读 · 0 评论 -
92. Reverse Linked List II
题目链接:reverse-linked-list-ii/** * * Reverse a linked list from position m to n. Do it in-place and in one-pass. * * For example: * Given 1->2->3->4->5->NULL, m = 2 and n = 4, * * return原创 2015-03-15 13:06:19 · 381 阅读 · 0 评论 -
23. Merge k Sorted Lists
题目链接:merge-k-sorted-lists/** * * Merge k sorted linked lists and return it as one sorted list. * * Analyze and describe its complexity. * */public class MergeKSortedLists { public c原创 2015-03-15 15:06:36 · 512 阅读 · 0 评论 -
24. Swap Nodes in Pairs
题目链接:swap-nodes-in-pairs/** * * Given a linked list, swap every two adjacent nodes and return its head. * For example, * Given 1->2->3->4, you should return the list as 2->1->4->3. * You原创 2015-03-15 10:20:27 · 434 阅读 · 0 评论 -
[LeetCode 56] Merge Intervals
题目链接:merge-intervalsimport java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;/** * Given a collection of intervals, merge all overlapping i原创 2015-03-25 09:22:44 · 700 阅读 · 0 评论 -
[LeetCode 103] Binary Tree Zigzag Level Order Traversal
题目链接:binary-tree-zigzag-level-order-traversal类似的题型:1 Binary Tree Level Order Traversal2 Binary Tree Level Order Traversal II3 Minimum Depth of Binary Tree4 Sum Root to Leaf Numbers原创 2015-03-25 18:57:50 · 600 阅读 · 0 评论 -
53.Maximum Subarray
题目链接:maximum-subarray/** * Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], t原创 2015-03-19 10:59:20 · 415 阅读 · 0 评论 -
19. Remove Nth Node From End of List
题目链接:remove-nth-node-from-end-of-list/** * * 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->4->5, and n = 2.原创 2015-03-15 09:55:42 · 407 阅读 · 0 评论 -
[LeetCode 64] Minimum Path Sum
题目链接:minimum-path-sum/** * Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note:原创 2015-03-24 21:47:42 · 703 阅读 · 0 评论 -
[LeetCode 106] Construct Binary Tree from Inorder and Postorder Traversal
题目链接:construct-binary-tree-from-inorder-and-postorder-traversalimport java.util.Arrays;//根据中序和后序遍历构建二叉树/** * * Given Inorder and Postorder traversal of a tree, construct the binary tr原创 2015-03-25 18:35:29 · 641 阅读 · 0 评论 -
25. Reverse Nodes in k-Group
题目链接:reverse-nodes-in-k-group/** * * Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. * * If the number of nodes is not a multiple of k the原创 2015-03-15 11:01:53 · 386 阅读 · 0 评论 -
88.Merge Sorted Array
题目链接:merge-sorted-arrayimport java.util.Arrays;/** * * Given two sorted integer arrays A and B, merge B into A as one sorted array. * * Note: * You may assume that A has enough space (s原创 2015-03-15 15:52:58 · 368 阅读 · 0 评论 -
20.Valid Parentheses
题目链接:valid-parenthesesimport java.util.ArrayList;import java.util.List;/** * * Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string原创 2015-03-15 16:42:51 · 376 阅读 · 0 评论 -
[LeetCode 41] First Missing Positive
题目链接:first-missing-positive/** * Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your al原创 2015-03-25 13:07:54 · 669 阅读 · 0 评论 -
[LeetCode 34] Search for a Range
题目链接:search-for-a-range/** * Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of原创 2015-03-25 10:28:04 · 633 阅读 · 0 评论 -
[LeetCode 105] Construct Binary Tree from Preorder and Inorder Traversal
题目链接:construct-binary-tree-from-preorder-and-inorder-traversalimport java.util.Arrays;//根据前序和中序遍历构建二叉树/** * Given preorder and inorder traversal of a tree, construct the binary tree.原创 2015-03-25 18:00:36 · 767 阅读 · 0 评论 -
21.Merge Two Sorted Lists
题目链接:merge-two-sorted-lists/** * * 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 lists. * */pu原创 2015-03-15 14:25:59 · 356 阅读 · 0 评论 -
144.Binary Tree Preorder Traversal
题目链接:binary-tree-preorder-traversalimport java.util.ArrayList;import java.util.List;import java.util.Stack;/** * Given a binary tree, return the preorder traversal of its nodes' values.原创 2015-03-19 00:34:09 · 553 阅读 · 0 评论 -
145.Binary Tree Postorder Traversal
题目链接:binary-tree-postorder-traversalimport java.util.ArrayList;import java.util.List;import java.util.Stack;/** * Given a binary tree, return the postorder traversal of its nodes' valu原创 2015-03-19 10:24:33 · 480 阅读 · 0 评论 -
94.Binary Tree Inorder Traversal
题目链接:binary-tree-inorder-traversalimport java.util.ArrayList;import java.util.List;/** * Given a binary tree, return the inorder traversal of its nodes' values. For example: Given bi原创 2015-03-19 00:08:24 · 544 阅读 · 0 评论 -
35.Search Insert Position
题目链接:search-insert-position有待继续优化/** * * Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted原创 2015-03-19 13:08:41 · 395 阅读 · 0 评论 -
[LeetCode 109] Convert Sorted List to Binary Search Tree
题目链接:convert-sorted-list-to-binary-search-tree/** * Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. * */public class Conve原创 2015-03-25 22:51:35 · 713 阅读 · 0 评论 -
[LeetCode 147] Insertion Sort List
题目链接:insertion-sort-list/** * Sort a linked list using insertion sort. * */public class InsertionSortList { public class ListNode { int val; ListNode next; ListNode(int x) { va原创 2015-03-25 21:58:56 · 717 阅读 · 0 评论 -
169 Majority Element [LeetCode Java实现]
题目链接:majority-element/** * Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the arra原创 2015-03-19 14:17:34 · 687 阅读 · 0 评论 -
96.Unique Binary Search Trees [LeetCode Java实现]
题目链接:unique-binary-search-treesimport java.util.Arrays;/** * Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, th原创 2015-03-19 16:26:55 · 752 阅读 · 0 评论 -
70.Climbing Stairs
题目链接:climbing-stairs/** * You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to th原创 2015-03-19 17:12:58 · 456 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree
题目链接:convert-sorted-array-to-binary-search-tree/** * Given an array where elements are sorted in ascending order, convert it to a height balanced BST. * */public class ConvertSortedArray原创 2015-03-19 17:50:24 · 487 阅读 · 0 评论 -
153.Find Minimum in Rotated Sorted Array [LeetCode Java实现]
题目链接:find-minimum-in-rotated-sorted-array有待继续优化代码结构,但是已经AC了/** * Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6原创 2015-03-19 19:03:14 · 1305 阅读 · 0 评论 -
[LeetCode 117] Populating Next Right Pointers in Each Node II
题目链接:populating-next-right-pointers-in-each-node-ii相同题型:Populating Next Right Pointers in Each Nodeimport java.util.LinkedList;/** * Follow up for problem "Populating Next Right Poin原创 2015-03-26 09:57:24 · 606 阅读 · 0 评论 -
[LeetCode 148] Sort List
题目链接:sort-list/** * Sort a linked list in O(n log n) time using constant space complexity. * */public class SortList { public class ListNode { int val; ListNode next; ListNode(in原创 2015-03-26 08:29:22 · 634 阅读 · 0 评论 -
[LeetCode 114] Flatten Binary Tree to Linked List
题目链接:flatten-binary-tree-to-linked-listimport java.util.Stack;/** * Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \原创 2015-03-26 16:43:03 · 418 阅读 · 0 评论 -
[LeetCode 143] Reorder List
题目链接:reorder-list/** * 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. For exa原创 2015-03-26 16:46:55 · 1287 阅读 · 0 评论 -
[LeetCode 86] Partition List
题目链接:partition-list/** * Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original rela原创 2015-03-27 16:43:05 · 947 阅读 · 0 评论 -
[leetCode 75] Sort Colors
题目链接:sort-colorsimport java.util.Arrays;/** * Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the or原创 2015-03-20 14:38:20 · 949 阅读 · 0 评论 -
[leetCode 168] Excel Sheet Column Title
题目链接:excel-sheet-column-title/** * * Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 ->原创 2015-03-20 15:37:47 · 781 阅读 · 0 评论 -
[LeetCode 42] Trapping Rain Water
题目链接:trapping-rain-waterimport java.util.Stack;/** * Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to原创 2015-03-27 19:39:30 · 616 阅读 · 0 评论 -
[LeetCode 90] Subsets II
题目链接:subsets-iiimport java.util.ArrayList;import java.util.Arrays;import java.util.HashSet;import java.util.List;import java.util.Set;/** * Given a collection of integers that might co原创 2015-03-28 00:32:07 · 1362 阅读 · 0 评论 -
[LeetCode 125] Valid Palindrome
题目链接:valid-palindrome/** * Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama"原创 2015-03-20 17:35:08 · 694 阅读 · 0 评论 -
5.Longest Palindromic Substring
题目链接:longest-substring-without-repeating-characters/** * * Given a string S, find the longest palindromic substring in S. * You may assume that the maximum length of S is 1000, * and ther原创 2015-03-16 12:42:21 · 413 阅读 · 0 评论 -
33.Search in Rotated Sorted Array
题目链接:search-in-rotated-sorted-array/** * * Suppose a sorted array is rotated at some pivot unknown to you beforehand. * (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). * You are given a tar原创 2015-03-17 10:38:25 · 797 阅读 · 0 评论