自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

sadjuno的博客

一只埋头进步的小菜鸟

  • 博客(64)
  • 收藏
  • 关注

原创 [230]Kth Smallest Element in a BST

【题目描述】Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:

2015-10-31 19:28:00 176

原创 [63]Unique Paths II

【题目描述】Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respective

2015-10-31 15:56:35 225

原创 [62]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 t

2015-10-31 14:11:30 161

原创 [95]Unique Binary Search Trees II

【题目描述】Given 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 below.

2015-10-29 20:27:13 144

原创 [96]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

2015-10-29 10:29:01 179

原创 [116]Populating Next Right Pointers in Each Node

【题目描述】Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next

2015-10-29 10:00:40 150

转载 反向迭代器(Reverse Iterator)

1. 定义反向迭代器(Reverse Iterator)是一种反向遍历容器的迭代器。也就是,从最后一个元素到第一个元素遍历容器。反向迭代器将自增(和自减)的含义反过来了:对于反向迭代器,++运算将访问前一个元素,而--运算则访问下一个元素。2. 作用(1)反向迭代器需要使用自减操作符:标准容器上的迭代器(reverse_iterator)既支持自增运算,也支持自减运算。但是,流迭代器由

2015-10-28 20:38:59 582

转载 reverse()

反转在范围内的元素的顺序。template void reverse( BidirectionalIterator _First, BidirectionalIterator _Last );参数_First指向第一个元素的位置的双向迭代器在元

2015-10-28 20:37:41 176

原创 [47]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],

2015-10-28 20:34:22 164

原创 [31]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 low

2015-10-28 19:49:59 163

原创 [46]Permutations

【题目描述】Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].

2015-10-28 15:31:08 205

转载 STL算法:prev_permutation和next_permutation的使用

在标准库算法中,next_permutation应用在数列操作上比较广泛.这个函数可以计算一组数据的全排列.但是怎么用,原理如何,我做了简单的剖析.首先查看stl中相关信息.函数原型:[cpp] view plaincopytemplateclass BidirectionalIterator>     bool next_p

2015-10-27 16:59:56 239

转载 fill和fill_n函数的应用

fill函数的作用是:将一个区间的元素都赋予val值。函数参数:fill(first,last,val);//first为容器的首迭代器,last为容器的末迭代器,val为将要替换的值。例题:给你n个数,然后输入一些操作:start,end,paint。表示从start到end都赋予paint的值,并输出每一次操作后的数组状态。代码:#include #

2015-10-27 16:58:25 329

原创 [77]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]

2015-10-27 15:55:13 196

原创 [73]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.【思路】设置两个bool数组来记录每行和每列是否有0【代码】class Solution {public: void setZeroes(vector>& matr

2015-10-26 21:46:31 283

原创 [240]Search a 2D Matrix II

【题目描述】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 in ascending from left to right.

2015-10-26 20:59:12 180

原创 [74]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 int

2015-10-26 20:07:11 216

原创 [54]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

2015-10-25 21:14:42 208

原创 [59]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

2015-10-25 19:22:12 224

原创 [162]Find Peak Element

【题目描述】A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multi

2015-10-25 11:15:01 208

原创 [24]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 const

2015-10-25 10:16:13 196

原创 [80]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

2015-10-24 19:47:42 166

原创 [48]Rotate Image

【题目描述】You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).【思路】如果逐步旋转的话速度太慢,考虑先沿主对角线翻转,然后再沿水平中线翻转一次。【代码】class Solution {public: void

2015-10-24 10:59:35 167

转载 九大排序算法总结

本文是 http://blog.csdn.net/xiazdong/article/details/7304239 的补充,当年看了《大话数据结构》总结的,但是现在看了《算法导论》,发现以前对排序的理解还不深入,所以打算对各个排序的思想再整理一遍。本文首先介绍了基于比较模型的排序算法,即最坏复杂度都在Ω(nlgn)的排序算法,接着介绍了一些线性时间排序算法,这些排序算法虽然都在线性时间,但是都

2015-10-24 09:54:56 271

原创 [75]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 t

2015-10-23 21:56:42 191

原创 [110]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.【思路】基本上和 Convert Sorted Array to Binary Search Tree这道题的类似,不过因为是列表,所以需要通过遍历的

2015-10-23 17:41:57 253

原创 [109]Convert Sorted Array to Binary Search Tree

【题目描述】Given an array where elements are sorted in ascending order, convert it to a height balanced BST.【思路】平衡二叉查找树的特点是除了满足BST的基本特点,另外对于任意一个结点,它左子树和右子树的深度最大差1。所以每次用中间结点做每一步的根结点,递归就可以了。【代码】/

2015-10-23 15:49:10 201

转载 连续子数组最大和问题

问题描述输入一个整形数组,求数组中连续的子数组使其和最大。比如,数组x应该返回 x[2..6]的和187.问题解决我们很自然地能想到穷举的办法,穷举所有的子数组的之和,找出最大值。穷举法i, j的for循环表示x[i..j],k的for循环用来计算x[i..j]之和。maxsofar = 0for i = [0, n) for j

2015-10-22 20:47:07 177

原创 [53]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 [

2015-10-22 19:43:05 158

原创 [153]Find Minimum in Rotated Sorted Array

【题目描述】Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicat

2015-10-22 11:29:41 140

原创 [12]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、V、X、L、C、D、M相应的阿拉伯数字表示为:1、5、10、50、100、500、

2015-10-21 11:48:53 152

原创 [137]Single Number II

【题目描述】Given an array of integers, every element appears three times except for one. Find that single one.【思路】其实和Single Number的思路差不多,第一种方法是将数组排序,遍历数组,判断查找哪个数字没有出现满3次,第二种方法则是利用位操作。【代码】clas

2015-10-20 09:46:21 274

原创 [121]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

2015-10-19 21:02:26 191

原创 [268]Missing Number

【题目描述】Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.【思路】因为这n个数是

2015-10-19 20:02:02 174

原创 [35]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 i

2015-10-17 21:39:40 179

原创 [141]Linked List Cycle

【题目描述】Given a linked list, determine if it has a cycle in it.【思路】用两个指针low和fast,low走一步,fast走两步,如果两个指针会相遇,则说明该链表中有环。【代码】/** * Definition for singly-linked list. * struct ListNode { *

2015-10-16 18:15:55 178

原创 [145]Binary Tree Postorder Traversal

【题目描述】【思路】(1)如果二叉树为空,空操作(2)如果二叉树不为空,后序遍历左子树,后序遍历右子树,访问根节点。递归的版本比较简单,很快就写出来了,非递归的版本还要记录该根节点是否是第一次遍历到(如果是第一次遍历的,不能访问,要先去访问其右子树),比较复杂,写的比较坑绊,要牢记非递归版本的写法。【代码】递归:/** * Definition for a

2015-10-15 17:04:37 175

原创 [94]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].【思路】(1)如果二叉

2015-10-15 16:37:22 136

原创 [144]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].【思路】(1)如果二

2015-10-15 10:32:31 161

原创 [238]Product of Array Except Self

【题目描述】Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without divis

2015-10-14 19:02:44 207

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除