- 博客(33)
- 收藏
- 关注
原创 树的相关算法
二叉搜索树 1.Validate Binary Search TreeGiven 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 k
2017-04-11 00:06:10 264
原创 Restore IP Addresses
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.
2017-03-29 21:28:39 217
原创 树的前序、中序、后序遍历
树的前序、中序、后序遍历 前序:根->左子树->右子树 中序:左子树->根->右子树 后序:左子树->右子树->根145. Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes’ values. For example: Given binary t
2017-03-29 21:21:50 343
原创 Reverse Linked List II
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
2017-03-29 00:38:33 199
原创 Decode Ways
Decode Ways A message containing letters from A-Z is being encoded to numbers using the following mapping: Given an encoded message containing digits, determine the total number of ways to decode i
2017-03-28 23:18:23 158
原创 Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = “great”: To scrambl
2017-03-27 20:24:20 222
原创 Partition List
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 nod
2017-03-27 19:56:56 163
原创 二叉搜索树的后序遍历序列
二叉搜索树的后序遍历序列 题目要求判断数组是否为二叉搜索树的后序遍历序列。 二叉搜索树存在的关系:左子树<根节点<右子树 所以后序遍历的时候,根节点在数组序列的末尾,根节点的左子树排在序列的前段,右子树排在序列的中段。 根据这一规律可以判断是否为后序遍历的序列。class Solution { public: bool VerifySquenceOfBST(vector<int> s
2017-03-27 19:52:26 201
原创 Largest Rectangle in Histogram & Maximal Rectangle
1.Largest Rectangle in Histogram Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above i
2017-03-23 22:51:55 174
原创 分金子
分金子 题目描述: A、B两伙马贼意外地在一片沙漠中发现了一处金矿,双方都想独占金矿,但各自的实力都不足以吞下对方,经过谈判后,双方同意用一个公平的方式来处理这片金矿。处理的规则如下:他们把整个金矿分成n段,由A、B开始轮流从最左端或最右端占据一段,直到分完为止。 马贼A想提前知道他们能分到多少金子,因此请你帮忙计算他们最后各自拥有多少金子?(两伙马贼均会采取对己方有利的策略)这是2017年
2017-03-20 23:30:53 1406
原创 剪气球串
剪气球串 小明买了一些彩色的气球用绳子串在一条线上,想要装饰房间,每个气球都染上了一种颜色,每个气球的形状都是各不相同的。我们用1到9一共9个数字表示不同的颜色,如12345则表示一串5个颜色各不相同的气球串。但小明希望得到不出现重复颜色的气球串,那么现在小明需要将这个气球串剪成多个较短的气球串,小明一共有多少种剪法?如原气球串12345的一种是剪法是剪成12和345两个气球串。 注意每种剪法需
2017-03-20 20:47:55 1445
原创 Remove Duplicates from Sorted List I II
Remove Duplicates from Sorted List I 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
2017-03-20 17:08:13 209
原创 Subsets
Subsets Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example, If nums = [1,2,3], a solution is:使用递归的思想来寻找子串,注意空字符串
2017-03-19 17:50:50 155
原创 Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or ve
2017-03-19 17:47:14 162
原创 从上往下打印二叉树
从上往下打印二叉树 从上往下打印出二叉树的每个节点,同层节点从左至右打印。可以利用图的BFS的思想,借助一个双端队列deque.class Solution { public: vector<int> PrintFromTopToBottom(TreeNode* root) { vector<int> rst; if(root==NULL)
2017-03-17 18:48:30 139
原创 Combinations
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: 在leetcode做了很多类似的题目,用递归的思想去解决就可以了。class Solution
2017-03-17 13:18:39 259
原创 Sort Colors
* 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 th
2017-03-17 13:02:27 143
原创 Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example, S = “ADOBECODEBANC” T = “ABC” Minim
2017-03-17 12:57:03 221
原创 Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitte
2017-03-15 16:34:34 146
原创 Search a 2D Matrix I,II
1. Search a 2D Matrix I 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 fi
2017-03-15 16:16:19 283
原创 栈的压入、弹出序列
栈的压入、弹出序列 问题:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)class Solution { public: bool IsPo
2017-03-15 16:05:44 148
转载 转载:求一组数组中出现单次的数字类问题
下面三个算法,觉得很有意思,转载的博客,学习了。1.数组中,只出现一次的数字有一个,其它数字都出现了两次,找出这个数字,原文作者MoreWindows,很好得用到了异或的知识,很巧妙得解决了问题,转载自:http://blog.csdn.net/morewindows/article/details/73545712.与问题1不同的是,数组中有两个出现一次的数字,找出这两个数字。原文作者,通过对所有
2017-03-14 21:17:30 148
原创 二叉树的镜像
二叉树的镜像 class Solution { public: void Mirror(TreeNode *pRoot) { if(pRoot==NULL){ return ; } TreeNode *temp=pRoot->left; pRoot->left=pRoot->right;
2017-03-14 17:06:43 137
原创 Climbing Stairs
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? 问题描述:一个有n层阶梯的楼梯,
2017-03-14 17:03:15 140
原创 Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c”class Solution { public: string simplifyPath
2017-03-14 16:50:44 258
原创 树的子结构
树的子结构 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)。 这是一道剑指offer的题目,觉得题目挺有意思,可以很好得利用递归的思想。/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x)
2017-03-13 16:54:18 208
原创 Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): “123”
2017-03-13 15:17:33 150
原创 Unique Paths I,II
1.Unique Paths I 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 tryin
2017-03-13 14:53:50 139
原创 Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character ‘.’. You may assume that there will be only one unique solution.这道题是求解数独。 解题思想: 1.在每个
2017-03-09 21:24:28 186
原创 Merge Intervals
Given a collection of intervals, merge all overlapping intervals.For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].Given a collection of intervals, merge all overlapping inte
2017-03-08 19:00:46 157
原创 Jump Game I,II
JUMP GAME I 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.Determi
2017-03-08 16:09:44 182
原创 Maximum Subarray
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
2017-03-08 15:40:54 170
原创 n-queens puzzle
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.N皇后问题是指在一个N*N的矩阵中,任意两个棋子不能出现在同行,同列,同斜线中,可以采用递归回溯的思想来编写程序。class Solution { public:
2017-03-07 14:55:25 333 2
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人