- 博客(56)
- 资源 (9)
- 收藏
- 关注
原创 Longest Consecutive Sequence
一、问题描述Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence
2016-08-31 15:19:00 171
原创 Construct Binary Tree from Inorder and Postorder Traversal
一、问题描述Given inorder and postorder traversal of a tree, construct the binary tree.二、思路和Construct Binary Tree from Preorder and Inorder Traversal思路基本一致,主要是根据在中序遍历中找到当前的值,然后通过对应关系递归。三、代码/**
2016-08-31 13:38:40 216
原创 Construct Binary Tree from Preorder and Inorder Traversal
一、问题描述Given preorder and inorder traversal of a tree, construct the binary tree.二、思路二叉树的题目用递归做是最便捷的,本题也不例外。递归的退出条件是: if(pre_begin > preorder.size() - 1 || in_begin > in_end) return N
2016-08-30 12:37:08 261
原创 Triangle
一、问题描述Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2],
2016-08-29 23:09:52 319
原创 Binary Tree Postorder Traversal
一、问题描述Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].二、思路二叉树的后
2016-08-29 11:43:26 307
原创 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
2016-08-29 11:35:19 318
原创 Binary Tree Right Side View
一、问题描述Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary t
2016-08-28 11:07:19 370
原创 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].二、思路可知此题意在返回第k个杨辉三角的行。递归思路,递归退出条件是: if(rowIndex == 0){
2016-08-28 10:39:12 328
原创 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]]二、思路从杨辉三角中找到规律
2016-08-28 09:35:55 330
原创 剑指offer-数组中出现超过一半的数字
一、问题描述数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。二、思路快排算法中的patition算法,返回中间的数即可。但要注意有可能数组中没有出现,此时我们需要对数组中间的数字验证: int num =
2016-08-27 23:03:48 394
原创 剑指offer-字符串全排列(有重复值)
一、问题描述输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 结果请按字母顺序输出。 输入描述:输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。二、思路与 Permutations II思路完全一致。三、代码cl
2016-08-27 20:45:21 2202
原创 剑指offer-最大连续子数组和
一、问题描述HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。你会不会被他忽悠住?
2016-08-27 14:28:40 529
原创 剑指offer-反转链表
一、问题描述输入一个链表,反转链表后,输出链表的所有元素。二、思路思路和逆序打印链表:逆序打印链表完全一致。三、代码/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class Solution {public:
2016-08-27 14:16:58 425
原创 剑指offer-1的个数
一、问题描述输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。二、思路本题中最关键的是: n = n & (n - 1);这一操作相当于是将2进制的数的最后一位1由1变为0,有多少位1,就会变多少次。三、代码class Solution {public: int NumberOf1(int n) {
2016-08-27 14:09:04 405
原创 剑指offer-逆序打印链表
一、问题描述二、思路采用头插法先逆置链表,然后依次遍历链表节点。注意考虑链表的边界条件。三、代码/*** struct ListNode {* int val;* struct ListNode *next;* ListNode(int x) :* val(x), next(NULL) {*
2016-08-27 13:53:55 532
原创 剑指offer-指数幂
一、问题描述给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。二、思路注意考虑边界情况,如指数幂的正负号以及底数为0的情况。三、代码class Solution {public: double Power(double base, int exponent) { double result =
2016-08-27 13:22:37 502
原创 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.二、思路
2016-08-27 12:44:36 347
原创 剑指offer-青蛙变态跳台阶
一、问题描述一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。二、思路分析:用Fib(n)表示青蛙跳上n阶台阶的跳法数,青蛙一次性跳上n阶台阶的跳法数1(n阶跳),设定Fib(0) = 1; 当n = 1 时, 只有一种跳法,即1阶跳:Fib(1) = 1; 当n = 2 时, 有两种跳的方
2016-08-27 11:22:04 975
原创 剑指offer-菲波那切数列
一、问题描述实现斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。n二、思路循环实现即可。三、代码class Solution {public: int Fibonacci(int n) { if(n < 0) return -1; vector vec; vec.push_back(1
2016-08-27 10:45:58 441
原创 剑指offer-青蛙跳台阶
一、问题一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。二、思路本题即为斐波那契数列,递归式如下: | 1, (n=1)f(n) = | 2, (n=2) | f(n-1)+f(n-2) ,(n>2,n为整数)三、代码cl
2016-08-27 10:36:15 385
原创 剑指offer-字符串空格替换为“ ”
一、问题描述请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。二、思路由于规定是char*类型,所以没法用string类型字符串处理,我们从后往前依次替换,注意替换过程中需要考虑输出长度不超过length。三、代码//length为牛客系统规定字符串输出的最大长度,固定为一个
2016-08-27 10:23:17 801
原创 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
2016-08-26 20:20:58 318
原创 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
2016-08-26 20:04:03 316
原创 Validate Binary Search Tree
一、问题描述Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node'
2016-08-26 19:20:09 298
原创 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.二、思路数组的关键在于下标,链表的关键在于首位节点。我们递归的退出条件是头尾节点相等: if(head == tail)
2016-08-26 17:35:50 373
原创 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
2016-08-26 14:12:34 346
原创 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:G
2016-08-26 11:29:25 180
原创 Combinations
一、问题描述Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3]
2016-08-25 23:31:32 293
原创 Convert Sorted Array to Binary Search Tree
一、问题描述二、思路思路很简单,递归建立二叉搜索树,将中间值作为根节点即可,然后分别递归建立左子树和右子树。在建立节点时,如果节点为空,必须申请空间,同时赋值三个域即可。还有 就是在传递参数时,由于我们平时在定义二叉树时除了定义一个TreeNode节点,还会定义一个二叉树的指针,这样在传递参数时就不会感觉那么奇怪。比如本例中由于没有定义,所以传递参数时出现下面这种 *&符号连写,让自己以为
2016-08-25 22:09:35 187
原创 Restore IP Addresses
一、问题描述Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.3
2016-08-25 20:10:15 218
原创 Unique Binary Search Trees II
一、问题描述Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown
2016-08-25 10:20:26 132
原创 Unique Binary Search Trees
一、问题描述Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2
2016-08-25 09:29:15 176
原创 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 node
2016-08-25 00:53:28 159
原创 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.
2016-08-25 00:36:10 248
原创 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,
2016-08-24 23:39:38 151
原创 Search in Rotated Sorted Array II
一、问题描述Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given targ
2016-08-24 21:53:10 158
原创 Remove Duplicates from Sorted Array II
一、问题描述Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with
2016-08-24 19:18:56 162
原创 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].二、思路二叉树的先序
2016-08-24 18:58:35 148
原创 Binary Tree Inorder Traversal
一、问题描述Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,3,2].二、思路中序递归
2016-08-24 17:47:12 151
原创 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
2016-08-24 17:12:29 134
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人