
LeetCode
dancheren
这个作者很懒,什么都没留下…
展开
-
LeetCode-Balanced Binary Tree
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 which the depth of the two subtrees of every node never diffe原创 2016-11-26 20:06:59 · 259 阅读 · 0 评论 -
LeetCode-Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.罗马数字组成单元:I: 1IV: 4V: 5IX: 9X: 10XL: 40L: 50XC: 90C: 10原创 2016-10-21 21:48:26 · 295 阅读 · 0 评论 -
LeetCode-Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).Example 1:Input: [3, 2,原创 2016-10-23 21:26:26 · 243 阅读 · 0 评论 -
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.此题是最近本的二叉树递归题,比较一个节点左右子树的深度。代码原创 2016-10-21 14:41:26 · 233 阅读 · 0 评论 -
LeetCode-Remove Elements
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.原创 2016-10-17 22:11:33 · 274 阅读 · 0 评论 -
LeetCode-Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2016-10-17 20:53:56 · 201 阅读 · 0 评论 -
LeetCode-Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine i原创 2016-10-16 20:01:46 · 269 阅读 · 0 评论 -
LeetCode-Reverse Linked List
Reverse a singly linked list.click to show more hints.Hint:A linked list can be reversed either iteratively or recursively. Could you implement both?非递归解法:维护两个指针变量 prev ,temp,temp 和原创 2016-09-25 20:35:45 · 201 阅读 · 0 评论 -
LeetCode-Rotate Function
Given an array of integers A and let n to be its length.Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow:原创 2016-10-16 20:58:43 · 338 阅读 · 0 评论 -
LeetCode-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],the contiguous subarray [4,-1,2,1] ha原创 2016-10-13 20:04:38 · 288 阅读 · 0 评论 -
LeetCode-TwoSum
题目描述:Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example:G原创 2016-08-31 22:32:04 · 242 阅读 · 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原创 2016-09-01 20:01:25 · 329 阅读 · 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.解题思路在代码注释中说明。代码如下:/** * D原创 2016-11-28 13:06:27 · 298 阅读 · 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,null,null,15,原创 2016-11-28 13:59:48 · 233 阅读 · 0 评论 -
LeetCode-Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20原创 2016-11-25 22:13:28 · 274 阅读 · 0 评论 -
LeetCode-Symmetric Tree
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 symmetric: 1 / \ 2 2 / \ / \3 4 4 3原创 2016-11-25 17:45:53 · 284 阅读 · 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.检查两棵二叉树是否等原创 2016-11-25 16:39:07 · 262 阅读 · 0 评论 -
LeetCode-Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired by this original tweet by Max Howe原创 2016-11-24 21:11:39 · 233 阅读 · 0 评论 -
LeetCode-Path Sum III
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it原创 2016-11-23 17:25:02 · 401 阅读 · 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 / \原创 2016-11-23 10:50:46 · 328 阅读 · 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 and sum原创 2016-11-22 19:45:29 · 267 阅读 · 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 ↘原创 2016-09-27 23:15:25 · 251 阅读 · 0 评论 -
LeetCode-Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]原创 2016-10-13 15:38:30 · 330 阅读 · 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原创 2016-09-22 19:33:15 · 250 阅读 · 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.题目意思:拼接连个链表成一个新链表,新链表应该由两个链表中的较小首结点开始,按顺序依次排下去;原创 2016-09-19 20:09:51 · 193 阅读 · 0 评论 -
LeetCode-String to Integer (atoi)
题目:Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible原创 2016-09-06 21:37:50 · 211 阅读 · 0 评论 -
LeetCode-Valid Parentheses
题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are原创 2016-09-18 20:12:15 · 188 阅读 · 0 评论 -
LeetCode-Reverse Integer
题目:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask原创 2016-09-04 19:02:51 · 269 阅读 · 0 评论 -
LeetCode-First Unique Character in a String
题目:Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.原创 2016-09-13 19:31:20 · 330 阅读 · 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原创 2016-09-12 11:42:53 · 221 阅读 · 0 评论 -
LeetCode-Delete Node in a Linked List
题目:Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node w原创 2016-09-12 10:48:00 · 157 阅读 · 0 评论 -
LeetCode-Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings.思路:用双重循环来实现。外循环控制第一行字符串横向顺序,内控制字符串的纵向行数,内循环依次扫描第i个字符是否与第一个字符串的第i个字符相等;代码如下: helloword原创 2016-09-11 19:39:49 · 203 阅读 · 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.代码如下:另解:原创 2016-10-08 20:05:53 · 206 阅读 · 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.Given 1->原创 2016-10-08 20:37:06 · 238 阅读 · 0 评论 -
Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5解题思路:增添一个头结点,让 head 指向该头原创 2016-09-26 20:01:07 · 250 阅读 · 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 satisfy the原创 2016-10-11 22:53:55 · 251 阅读 · 0 评论 -
LeetCode-Roman to Integer
题目:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.解题思路:如果当前数跟前一个数相等,则结果加上当前数;如果当前数小于前一个数,则结果加上前一个数,将当前数赋给前一个数,再取下一个数;如果当前数大于前一个数,原创 2016-09-10 21:08:04 · 171 阅读 · 0 评论 -
LeetCode-House Robber
题目:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjac原创 2016-09-21 21:30:49 · 291 阅读 · 0 评论 -
LeetCode-Palindrome Numbers
题目:Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer原创 2016-09-08 19:32:05 · 200 阅读 · 0 评论 -
LeetCode-Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?思路:检验是否为回文我们需要对比对应数字是否相等,首先我们需要找到链表的中点,寻找中点的办法是制定两个指针,一个慢指针、一个快指针,快指针每原创 2016-09-23 21:32:47 · 200 阅读 · 0 评论