LeetCode
cfanrCoder
对贡献有激情,对回报有信心!
展开
-
【LeetCode】3Sum Closest
题目Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would ha原创 2014-12-14 16:14:59 · 1129 阅读 · 0 评论 -
【LeetCode】Intersection of Two Linked Lists
题目Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘原创 2014-12-08 15:07:25 · 1646 阅读 · 0 评论 -
【LeetCode】Sum Root to Leaf Numbers
题目Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find th原创 2014-12-23 15:27:19 · 1171 阅读 · 0 评论 -
【LeetCode】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.解答注意假的头结点的运用,代码如下:/** * Definition for singly-原创 2014-12-06 16:36:21 · 1389 阅读 · 0 评论 -
【LeetCode】Remove Duplicates from Sorted List
题目Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.解答法1:从表头开始一个个比较原创 2014-12-06 16:00:37 · 1186 阅读 · 0 评论 -
【LeetCode】Insert Interval
题目Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start tim原创 2014-12-05 15:26:05 · 1066 阅读 · 0 评论 -
【LeetCode】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. After removing the second node from the原创 2014-12-05 16:07:31 · 1265 阅读 · 0 评论 -
【LeetCode】Path Sum
题目Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree原创 2014-07-08 14:29:09 · 1301 阅读 · 0 评论 -
【LeetCode】Same Tree
题目Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.解答原创 2014-12-21 16:31:06 · 992 阅读 · 0 评论 -
【LeetCode】Merge Intervals
题目Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].解答先对间隔段按起点由小到大排序(起点相同按终点排),那么问题来了,如何对对象进行排原创 2014-12-05 14:52:20 · 1146 阅读 · 0 评论 -
【LeetCode】Binary Tree Inorder Traversal
题目Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].解答中序遍历二叉树。递归法原创 2014-12-21 15:00:21 · 991 阅读 · 0 评论 -
【LeetCode】Minimum Depth of Binary Tree
题目Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.解答求树的最小深度。类似Maximum Depth原创 2014-12-20 15:36:45 · 1184 阅读 · 0 评论 -
【LeetCode】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.解答题目要求对链表循环右移k的位置,注意k可能会大原创 2014-12-09 15:39:37 · 1296 阅读 · 0 评论 -
【LeetCode】Path Sum II
题目Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 /原创 2014-12-24 17:20:13 · 1062 阅读 · 0 评论 -
【LeetCode】Two Sum
题目Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the tar原创 2014-12-13 15:06:24 · 1143 阅读 · 0 评论 -
【LeetCode】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 then left-out nodes in the end should remain原创 2014-12-12 18:27:47 · 1027 阅读 · 0 评论 -
【LeetCode】Copy List with Random Pointer
题目A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.解答题目要求拷贝一个链表,该链表除了一个next原创 2014-12-12 15:20:04 · 867 阅读 · 0 评论 -
【LeetCode】Merge k Sorted Lists
题目Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.解答方法1:利用分治的思想把合并k个链表分成两个合并k/2个链表的任务,一直划分,知道任务中只剩一个链表或者两个链表。可以很简单的用递归来实现。因此算法复杂度为T(k) = 2原创 2014-12-12 16:57:45 · 1003 阅读 · 0 评论 -
【LeetCode】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.解答题目要求将链表转化为二叉查找树。利用树的中序遍历的递归思想,对链表结点一个个进行访问,先对左子树进行递归,然后将当前结点作为根,迭代到下一个链表结点,最后在递原创 2014-12-12 18:53:56 · 1007 阅读 · 0 评论 -
【LeetCode】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.Your algorithm should use only constant原创 2014-12-10 15:01:01 · 802 阅读 · 0 评论 -
【LeetCode】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 relative order of the nodes in原创 2014-12-11 17:00:59 · 962 阅读 · 0 评论 -
【LeetCode】Linked List Cycle II
题目Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space?解答利用快慢指针使用Linked List Cycle I的办法,判原创 2014-12-10 15:47:59 · 898 阅读 · 0 评论 -
【LeetCode】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 1->4->3->2->5->NULL.Note:Given m, n sat原创 2014-12-08 16:57:33 · 950 阅读 · 0 评论 -
【LeetCode】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 example,Given {1,2,3,4}, reorde原创 2014-12-09 14:57:57 · 897 阅读 · 0 评论 -
【LeetCode】Remove Duplicates from Sorted List II
题目Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Giv原创 2014-12-09 16:35:37 · 1072 阅读 · 0 评论 -
【LeetCode】Insertion Sort List
题目Sort a linked list using insertion sort.解答链表无法像数组那样从后往前依次比较插入,只能从前往后;在链表首部添加一个哨兵可以稍微简化下代码,代码如下:/** * Definition for singly-linked list. * public class ListNode { * int val; *原创 2014-07-09 14:29:19 · 1590 阅读 · 1 评论 -
【LeetCode】Maximum Depth of Binary Tree
题目Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.解答求二叉树的最大深度。递归:a.若二叉树为空,原创 2014-12-20 14:47:47 · 1300 阅读 · 0 评论 -
【LeetCode】Binary Tree Level Order Traversal II
题目Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#原创 2014-12-19 16:34:53 · 993 阅读 · 0 评论 -
【LeetCode】ZigZag Conversion
题目The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA原创 2014-06-29 17:37:19 · 1139 阅读 · 0 评论 -
【LeetCode】Single Number
原文:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without原创 2014-05-25 15:29:10 · 1027 阅读 · 0 评论 -
【LeetCode】Sort Colors
题目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 order red, white and blue.Here, we will use the i原创 2014-05-23 15:21:11 · 1013 阅读 · 0 评论 -
【LeetCode】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 the top?解答原创 2014-05-22 21:05:21 · 957 阅读 · 0 评论 -
【LeetCode】Plus One
题目Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.解答本题考察进位问题,注原创 2014-05-14 09:20:34 · 857 阅读 · 0 评论 -
【LeetCode】Search a 2D Matrix
题目Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer原创 2014-05-13 16:50:58 · 923 阅读 · 0 评论 -
【LeetCode】Binary Tree Preorder Traversal
题目Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursiv原创 2014-05-05 09:43:21 · 979 阅读 · 0 评论 -
【LeetCode】Evaluate Reverse Polish Notation
题目Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2原创 2014-05-04 11:12:55 · 972 阅读 · 0 评论 -
【LeetCode】Reverse Integer
题目Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus points原创 2014-05-03 10:06:22 · 898 阅读 · 0 评论 -
【LeetCode】Reverse Words in a String
题目:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".解答:首先理解题意很重要,不然会浪费很多时间在细节上,先理清题目隐藏的一些细节:1,字符串的两端的空格原创 2014-03-12 22:41:00 · 1639 阅读 · 0 评论 -
【LeetCode】Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new len原创 2014-05-02 10:19:23 · 1056 阅读 · 0 评论 -
【LeetCode】Single Number II
题目Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it wit原创 2014-07-22 17:48:56 · 1152 阅读 · 0 评论