leetcode刷题之路
记录自己leetcode的解题过程
BurningWall
这个作者很懒,什么都没留下…
展开
-
leetcode 刷题之路 92 Climbing Stairs
一个台阶总共有n级,如果一次可以跳1级,也可以跳2级,求总共有多少总跳法。原创 2014-08-16 20:46:56 · 1195 阅读 · 1 评论 -
leetcode 刷题之路 91 Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]原创 2014-08-13 21:38:55 · 696 阅读 · 0 评论 -
leetcode 刷题之路 90 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 integers原创 2014-08-13 21:29:31 · 840 阅读 · 0 评论 -
leetcode 刷题之路 89 Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of原创 2014-08-13 17:11:24 · 780 阅读 · 0 评论 -
leetcode 刷题之路 88 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原创 2014-08-13 16:36:20 · 729 阅读 · 0 评论 -
leetcode 刷题之路 87 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-08-13 16:17:59 · 741 阅读 · 0 评论 -
leetcode 刷题之路 86 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-08-13 15:55:43 · 672 阅读 · 0 评论 -
leetcode 刷题之路 85 Merge Two Sorted Lists
erge 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.合并两个有序链表,合并后的链表仍然有序。原创 2014-08-13 15:34:45 · 717 阅读 · 0 评论 -
leetcode 刷题之路 84 Single Number II
给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数。原创 2014-08-13 13:46:36 · 929 阅读 · 0 评论 -
leetcode 刷题之路 83 Maximum Subarray
思路:从左向右遍历数组元素相加求和得到和sum,若sum小于0,必然会对总的和有损耗,因此将sum重置为0,从当前位置继续重复上述过程,直到数组结束,与此同时设置max变量记录求和过程中遇到的最大值。执行完上述过程,判断max等于0(max初值为0),若大于0,max为所求结果,返回max。若仍然等于0说明求和过程中未出现过正数,数组中全是负数或0,此时数组最大和就是数组中最大的最原创 2014-08-13 13:27:19 · 769 阅读 · 0 评论 -
leetcode 刷题之路 82 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-08-13 13:18:56 · 873 阅读 · 0 评论 -
leetcode 刷题之路 81 Populating Next Right Pointers in Each Node
Populate each next pointer to point to its next right node. 二叉树的结构体里增加了next指针,编写程序,将二叉树里每个节点的next指针指向它右边的节点。原创 2014-08-13 09:56:45 · 734 阅读 · 0 评论 -
leetcode 刷题报告 80 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 using e原创 2014-08-12 20:03:16 · 596 阅读 · 0 评论 -
leetcode 刷题之路 79 Binary Tree Preorder Traversal
非递归实现树的前序遍历原创 2014-08-12 15:01:47 · 795 阅读 · 0 评论 -
leetcode 刷题之路 78 Binary Tree Postorder Traversal
非递归方式实现二叉树的后序遍历。原创 2014-08-12 14:49:40 · 967 阅读 · 0 评论 -
leetcode 刷题之路 77 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1原创 2014-08-11 17:21:29 · 955 阅读 · 0 评论 -
leetcode 刷题之路 76 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.删除排序链表中重复的节点,删除操作原创 2014-08-11 15:47:19 · 713 阅读 · 0 评论 -
leetcode 刷题之路 75 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.用vector表示一个数,在这个数上j原创 2014-08-10 19:25:32 · 881 阅读 · 0 评论 -
leetcode 刷题之路 74 Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.从前序遍历结果和中序遍历结果中原创 2014-08-10 18:26:02 · 613 阅读 · 0 评论 -
leetcode 刷题之路 73 Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You ma原创 2014-08-10 17:55:39 · 657 阅读 · 0 评论 -
leetcode 刷题之路 72 Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy on原创 2014-08-10 16:32:26 · 538 阅读 · 0 评论 -
leetcode 刷题之路 71 Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),原创 2014-08-10 16:00:11 · 532 阅读 · 0 评论 -
leetcode 刷题之路 70 earch 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-08-10 15:46:55 · 818 阅读 · 0 评论 -
leetcode 刷题之路 69 Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1原创 2014-08-10 14:36:00 · 460 阅读 · 0 评论 -
leetcode 刷题之路 68 Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to原创 2014-08-10 13:17:54 · 721 阅读 · 0 评论 -
leetcode 刷题之路 67 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-08-10 11:06:44 · 439 阅读 · 0 评论 -
leetcode 刷题之路 66 Path Sum II
给定一个二叉树和数字sum,输出二叉树中从根节点到叶子节点所有路径中和等于sum的路径。原创 2014-08-10 10:25:08 · 744 阅读 · 0 评论 -
leetcode 刷题之路 65 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-08-09 23:50:24 · 532 阅读 · 0 评论 -
leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.给出二叉树的中序遍历和后序遍历结果,恢复出二叉树。后序遍历序列的最后一个元素值是二叉树的根节点的值,查找原创 2014-08-09 23:06:54 · 729 阅读 · 0 评论 -
leetcode 刷题之路 63 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).zigzag层序遍历树For example:Given binary原创 2014-08-09 22:05:08 · 713 阅读 · 0 评论 -
leetcode 刷题之路 62 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.求树的最大深度。树的最大深度d原创 2014-08-09 20:56:36 · 489 阅读 · 0 评论 -
leetcode 刷题之路 61 Sort List
Sort a linked list in O(n log n) time using constant space complexity.题目要求,对链表进行排序,要求时间buzad原创 2014-08-09 20:45:57 · 526 阅读 · 0 评论 -
leetcode 刷题之路 60 Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321对一个整数进行原创 2014-08-09 19:49:00 · 729 阅读 · 1 评论 -
leetcode 刷题之路 59 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?原创 2014-08-09 19:42:27 · 489 阅读 · 0 评论 -
leetcode 刷题之路 58 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?原创 2014-08-09 19:04:00 · 467 阅读 · 0 评论 -
leetcode 刷题之路 57 Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain dupli原创 2014-08-09 16:29:11 · 537 阅读 · 0 评论 -
leetcode 刷题之路 56 Subsets (非递归解法)
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For exa原创 2014-08-09 15:51:46 · 1004 阅读 · 0 评论 -
leetcode 刷题之路 55 Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".二进制字符串xiang原创 2014-08-09 01:43:04 · 558 阅读 · 0 评论 -
leetcode 刷题之路 54 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原创 2014-08-09 01:23:35 · 500 阅读 · 0 评论 -
leetcode 刷题之路 53 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-08-09 00:00:10 · 480 阅读 · 0 评论