- 博客(45)
- 资源 (11)
- 收藏
- 关注
原创 LeetCode 之 Decode Ways
原题:A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine
2013-11-29 00:35:48 796
原创 leetCode 之 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
2013-11-22 01:43:17 790
原创 LeetCode 之 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 stat
2013-11-21 23:28:33 938
原创 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 sati
2013-11-18 23:58:03 1370 1
原创 LeetCode 之 Count and Say
原题:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read
2013-11-17 02:00:45 1335
原创 LeetCode 之 Binary Tree Postorder Traversal
原题:Given a binary tree, return the postorder traversal of its nodes' values.二叉树后序遍历,可以看看先序和中序直接上代码(20ms):class Solution {public: vector postorderTraversal(TreeNode *root) { // IMP
2013-11-16 22:08:32 797
原创 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,#,
2013-11-16 20:16:09 765
原创 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},return [1,3,2].二叉树的中序遍历,废话不说,和先序遍历几乎一样代码(24ms):class Solution {pu
2013-11-16 20:01:20 775
原创 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},return [1,2,3].二叉树的先序遍历,用递归很简单代码(20ms):class Solution {
2013-11-16 19:36:09 791
原创 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).这个题其实就是二叉树的层次遍历,跟前边的这个题基本类似Populating Next Right Pointers in Each Node
2013-11-16 17:24:21 867
原创 LeetCode 之 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,
2013-11-16 15:38:02 819
原创 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.Fi
2013-11-15 17:05:57 819
原创 LeetCode 之 Pow(x, n)
原题:Implement pow(x, n).这个题就是求x的n次方先来个牛x的代码:class Solution {public: double pow(double x, int n) { // IMPORTANT: Please reset any member data you declared, as // the same
2013-11-15 16:00:42 766
原创 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.这个题是要求把链表的倒数第N个元素删除。。。当然只能扫描一次,其实很简单,用队列就可以,比如要删第i个,非常好,我这个队列就保存i个,没遇到一个节点我就把整个队列向前移一步,那么最后队列的头部就是这个要删除的节点喽
2013-11-15 15:43:36 710
原创 LeetCode 之 Plus One
原题:Given a number represented as an array of digits, plus one to the number.这个题和add two numbers一样。。。。记录一个carry就可以了,而且最后需要查看carry一样,carry不为0,则需要添加到最前边。而且这个题的结果是从后面开始算得,那么每次在vector的头部添加一个数时,用这个i
2013-11-15 01:46:35 755
原创 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,这个题跟第一个基本一样,看看第一个。就是需要返回一个路径
2013-11-14 23:37:44 750
原创 LeetCode 之 Populating Next Right Pointers in Each Node II
原题:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use co
2013-11-14 16:52:59 679
原创 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 n
2013-11-14 16:36:03 739
原创 LeetCode 之 Add Two Numbers
原题:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it
2013-11-14 11:18:59 807
原创 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.不就是求二叉树从根节点到叶子节点的最小深度么。。。。这个题跟这
2013-11-14 02:04:00 740
原创 LeetCode 之 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: You can only move either down or r
2013-11-13 23:02:00 751
原创 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 targ
2013-11-13 22:03:41 749
原创 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
2013-11-13 17:25:13 941
原创 LeetCode 之 Unique Paths
原题:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to rea
2013-11-13 01:09:10 911
原创 LeetCode 之 Palindrome Number
原题:Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of co
2013-11-12 17:47:55 1032
原创 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
2013-11-12 16:43:50 840
原创 LeetCode 之 Pascal's Triangle II
原题:Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].杨辉三角2,其实和上一个题没区别,可以回忆一下上一个题杨辉三角只不过只需要最后的结果,因此用两个指针来回指着,及时delete就能节约空间。不过需要复习一下v
2013-11-12 01:16:59 733
原创 LeetCode 之 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]]这个题比较简单,杨辉三角么,利用队列直接解
2013-11-12 00:46:57 729
原创 LeetCode 之 Populating Next Right Pointers in Each Node
看到这个题第一印象是先序遍历嘛,不过有一个非常棘手的问题,即每一行的最后一个元素不是指向下一行的第一个元素,而是指向NULL,传统的先序遍历如下: 由于这个题的要求是每一行的最后一个元素的next指向NULL,因此学渣看了别人的算法,用一个NULL来搞定!!!1 由于先序遍历是用队列来搞定的,在root入队后,再入队一
2013-11-11 22:30:33 716
原创 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,
2013-11-10 12:54:50 832
原创 LeetCode 之 Merge Sorted Array
原题:Given two sorted integer arrays A and B, merge B into A as one sorted array.合并两个有序的数列前面有一个是链表形式,而这个数组形式的非常相似,只是需要复制罢了。。。。主要思想:1 因为需要合并两个数组到A中,因此数组A会增加,从尾部直接考虑可以减少来回移动的时间2 用一个指针指向新的数组的尾部,分
2013-11-09 23:45:52 805
原创 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 lengt
2013-11-09 22:49:34 755
原创 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.Dete
2013-11-08 23:44:55 622
原创 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 pla
2013-11-06 22:19:47 577
原创 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
2013-11-06 20:57:55 568
原创 LeetCode 之 Climbing Stairs (方法2)
上一次说了自顶向下,没看的可以回去看看climbing stairs 方法1(自顶向上)由于本人是学渣,自顶向下太抽象肿么破。。。。尼玛哥教你一个更抽象的,自底向上,自底向上代码比较简单,基本思想一致,弄个数组record记录一下,本人是学渣,代码不是太优化,不过大体上是这样滴:1 如果n是1,直接返回1,n是2直接返回22 如果n>2 , 就是学渣领悟的最高境界了,number
2013-11-06 17:18:06 853
原创 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?第一次看到这个题,我擦,又一个动态规划,好
2013-11-06 16:58:08 639
原创 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.将两个有序的链表整合应该是很简单的,只需比较两个链表的数就可以,需要注意的有这么几点:1 初始状态下
2013-11-06 00:54:31 584
原创 leetcode 之 Linked List Cycle
原题:Given a linked list, determine if it has a cycle in it.这么高端大气的题目方法比较特殊,当然学渣也是问了学长才知道的,弄俩指针,一个走的快,一个走的慢,要是有环就会相遇(这个关于相遇的问题,一个走一步,另一个走2步肯定会相遇,要是一个走2步,另一个走4步,那就有可能不相遇了。。。不过还是走平常路吧),没环走的快的就会走到NULL。代码
2013-11-05 19:55:21 598
原创 LeetCode 之 Reverse Integer
原题:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321题目本身有个Have you thought about this?主要是一些溢出神马的,暂时未考虑。。。。其实本体比较简单,每次%10,压栈就可以了,不过由于本人是学渣,压栈忘了,于
2013-11-05 19:48:56 742
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人