leetcode
文章平均质量分 53
leetcode解题报告
wteo
这个作者很懒,什么都没留下…
展开
-
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 is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the f原创 2014-04-05 14:18:18 · 931 阅读 · 0 评论 -
LeetCode之Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary原创 2014-04-05 14:26:37 · 864 阅读 · 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,#,#,15,7}, 3 / \ 9 20 /原创 2014-04-05 12:57:57 · 984 阅读 · 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,#,#,15,7},原创 2014-04-05 13:12:15 · 876 阅读 · 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./** * Def原创 2014-03-20 14:25:49 · 623 阅读 · 0 评论 -
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原创 2014-03-20 18:40:47 · 817 阅读 · 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: Recursive soluti原创 2014-03-20 19:32:01 · 830 阅读 · 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].Note: Recursive solutio原创 2014-03-20 19:54:41 · 871 阅读 · 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 length.c原创 2014-03-20 20:31:02 · 639 阅读 · 0 评论 -
LeetCode之Merge Sorted Array
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 (size that is greater or equal to m + n) to hold additional elements from原创 2014-03-20 20:47:23 · 561 阅读 · 0 评论 -
LeetCode之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 algorithm should run in O(n) time and uses constant原创 2014-03-20 21:48:59 · 794 阅读 · 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 ]]原创 2014-03-21 12:36:10 · 847 阅读 · 0 评论 -
LeetCode之Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [原创 2014-03-21 12:54:44 · 651 阅读 · 0 评论 -
LeetCode之Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?和之前写的矩阵翻转算法是一样的。class Solution {public: vo原创 2014-03-21 13:10:07 · 752 阅读 · 0 评论 -
LeetCode之Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin原创 2014-03-21 14:14:26 · 757 阅读 · 0 评论 -
LeetCode之Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A si原创 2014-03-21 19:46:18 · 772 阅读 · 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 target, whe原创 2014-03-21 20:54:39 · 714 阅读 · 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}, reorder it t原创 2014-03-22 01:10:46 · 955 阅读 · 0 评论 -
LeetCode之Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?/** * Definition for singly-linked list. * struct ListNode { * int val;原创 2014-03-22 01:23:54 · 570 阅读 · 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?之前的the beginning of the loop也有介绍/**原创 2014-03-22 01:35:05 · 797 阅读 · 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.“put the nodes in the original原创 2014-03-22 13:16:58 · 840 阅读 · 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.“Let's start with an exam原创 2014-03-22 14:33:17 · 781 阅读 · 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.测试用例:NULL原创 2014-03-22 14:50:15 · 701 阅读 · 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 space. Y原创 2014-03-22 15:33:29 · 565 阅读 · 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->1-原创 2014-03-22 17:51:15 · 754 阅读 · 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 each of原创 2014-03-22 22:32:18 · 615 阅读 · 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-linked list. * struct ListN原创 2014-03-22 22:50:09 · 692 阅读 · 0 评论 -
LeetCode之Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.多遍LeetCode之Merge Two Sorted Lists思想/** * Definition for singly-linked list. * struct Lis原创 2014-03-22 23:25:33 · 779 阅读 · 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 as it is原创 2014-03-23 00:43:10 · 1109 阅读 · 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 end, the原创 2014-03-23 10:46:32 · 773 阅读 · 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 t原创 2014-03-23 11:14:28 · 851 阅读 · 0 评论 -
LeetCode之Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible原创 2014-03-23 23:45:30 · 824 阅读 · 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 the total原创 2014-03-24 22:01:50 · 1163 阅读 · 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原创 2014-03-24 22:15:05 · 777 阅读 · 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./** * Definition for binary原创 2014-03-24 22:28:26 · 1048 阅读 · 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 of Binary Tree有原创 2014-03-24 22:51:15 · 910 阅读 · 0 评论 -
LeetCode之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 in order.You may assume no duplicates in the array.原创 2014-03-26 17:52:17 · 697 阅读 · 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 of each原创 2014-03-26 18:05:21 · 801 阅读 · 0 评论 -
LeetCode之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 O(log n).If the target is not found in t原创 2014-03-26 18:39:14 · 724 阅读 · 0 评论 -
LeetCode之Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.注意:越界问题class Solution {public: int sqrt(int x) { if(x<0) return -1; if(x==0||x==1) return x; i原创 2014-03-31 12:10:16 · 951 阅读 · 0 评论