- 博客(72)
- 收藏
- 关注
原创 124.leetcode Binary Tree Maximum Path Sum(hard)[先序遍历]
Given a binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The
2016-08-26 21:30:05 394
原创 129.leetcode Sum Root to Leaf Numbers(medium)[深度遍历DFS]
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.Find the tota
2016-08-26 21:00:12 368
原创 113.leetcode Path Sum II(meidum)[DFS加回溯 ]
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, 5 / \
2016-08-26 16:59:14 381
原创 24.leetcode Merge k Sorted Lists(hard)[归并k个有序链表]
其实整体思想和归并k个有序数组是一样的,有三种方法,效率最慢的是两个有序链表和并之后再与其他链表合并,需要合并n-1次。第二种方法是两两一组分别归并,第三种方法是采用一个k大小的堆,刚开始的时候将每个链表的第一个元素都放入k的最小堆中,那么就可以直接得到k中最小的值,再将k中刚取出的最小值的下一个节点加入堆中,直到所有的元素都处理完成。/** * Definition for singly-
2016-08-26 15:55:36 486
原创 83.leetcode Remove Duplicates from Sorted List(easy)[链表删除重复部分]
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.class Solution {public
2016-08-26 11:38:28 307
原创 82.leetcode Remove Duplicates from Sorted List II(medium)[链表删除重复]
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.Given 1->
2016-08-26 11:36:02 237
原创 34.leetcode Search for a Range(meidum)[二分查找]
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 not found
2016-08-26 11:05:47 205
转载 leetcode 题目类型分类总结
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心)http://oj.leetcode.com/problems/valid-parentheses/
2016-08-26 10:36:13 845
原创 17.leetcode Letter Combinations of a Phone Number(meidum)[递归回溯]
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit st
2016-08-26 09:50:14 236
原创 40.leetcode Combination Sum II(medium)[递归回溯]
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in Cwhere the candidate numbers sums to T.Each number in C may only be used once in the combinat
2016-08-25 21:09:25 206
原创 39.leetcode Combination Sum(medium)[递归回溯]
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numb
2016-08-25 20:57:39 191
原创 52.leetcode N-Queens II(hard)[基于N-Queens修改返回值]
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.基于N-Queens修改返回值接口即可,比N-Queens更加简单。class Solution {public:
2016-08-25 16:52:20 168
原创 51.leetcode N-Queens(hard)[递归回溯剪枝]
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.
2016-08-25 16:48:15 442
原创 78.leetcode Subsets(medium)[回溯递归循环调用combination即可]
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:[ [3], [1],
2016-08-25 11:00:29 333
原创 77.leetcode Combinations(medium)[回溯递归]
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], [1,4],]
2016-08-25 10:41:34 182
原创 374.leetcode Guess Number Higher or Lower(easy)[二分查找]
We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I'll tell you whether the number is h
2016-08-24 21:21:15 179
原创 199.leetcode Binary Tree Right Side View(medium)[层次遍历二叉树 队列]
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 tree, 1
2016-08-24 20:45:36 311
原创 151.leetcode Reverse Words in a String(medium)[字符串分词翻转]
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".class Solution {public: void reverseWords(string &s) {
2016-08-24 19:49:17 418
原创 92.leetcode Reverse Linked List II(medium)[链表逆序]
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 the
2016-08-24 16:30:23 243
原创 兵临城下 (乐视编程题)[DFS 回溯递归]
卢卡斯的驱逐者大军已经来到了赫柏的卡诺萨城,赫柏终于下定决心,集结了大军,与驱逐者全面开战。卢卡斯的手下有6名天之驱逐者,这6名天之驱逐者各赋异能,是卢卡斯的主力。为了击败卢卡斯,赫柏必须好好考虑如何安排自己的狂战士前去迎战。狂战士的魔法与一些天之驱逐者的魔法属性是相克的,第i名狂战士的魔法可以克制的天之驱逐者的集合为Si(Si中的每个元素属于[0,5])。为了公平,两名狂战士不能
2016-08-24 10:36:22 417
原创 143.leetcode Reorder List (medium)[链表调整]
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it t
2016-08-23 21:51:14 230
原创 93.leetcode Restore IP Addresses(medium)[回溯 DFS]
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.35"]. (Order
2016-08-23 20:49:46 911
原创 4.leetcode Median of Two Sorted Arrays(medium)[求两个数组的中位数]
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1:nums1 =
2016-08-23 16:21:55 418
原创 142.leetcode Linked List Cycle II(medium)[有环链表]
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up:Can you solve it without using extra space?
2016-08-22 21:36:21 212
原创 378.leetcode Kth Smallest Element in a Sorted Matrix(medium)[堆求第K小的 ]
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order, not
2016-08-22 20:26:07 263
原创 387.leetcode First Unique Character in a String(easy)[统计字符串字符次数]
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.题目要找第一个没有重复的字
2016-08-22 19:51:53 771
原创 357.leetcode Count Numbers with Unique Digits(easy)[数学问题 非重复数字]
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n.Example:Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x [11,22,33,44,
2016-08-22 15:11:23 228
原创 383.leetcode Ransom Note(easy)[字符统计]
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constru
2016-08-22 11:49:29 276
原创 167.leetcode Two Sum II - Input array is sorted(medium)[两数求和固定值]
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two number
2016-08-22 11:14:22 600
原创 211.leetcode Add and Search Word - Data structure design(medium)[Trie树]
Design a data structure that supports the following two operations:void addWord(word)bool search(word)search(word) can search a literal word or a regular expression string containing only lett
2016-08-09 20:32:24 299
原创 15.leetcode 3Sum(medium)[排序 查找 Ksum问题]
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not contain
2016-08-09 19:47:24 207
转载 KSum问题
(中文旧版)前言:做过leetcode的人都知道, 里面有2sum, 3sum(closest), 4sum等问题, 这些也是面试里面经典的问题, 考察是否能够合理利用排序这个性质, 一步一步得到高效的算法. 经过总结, 本人觉得这些问题都可以使用一个通用的K sum求和问题加以概括消化, 这里我们先直接给出K Sum的问题描述和算法(递归解法), 然后将这个一般性的方法套用到具体的K, 比如
2016-08-09 16:13:16 1039
原创 222.leetcode.Count Complete Tree Nodes(medium)[完全二叉树 节点个数]
Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled,
2016-08-08 21:36:01 478
原创 109.leetcode Convert Sorted List to Binary Search Tree(medium)[链表 平衡二叉搜索树 二分查找]
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.题目给出一个有序链表,要求将其转换成平衡二叉搜索树,其实与数组转换成的思想相同,每次查找中间节点作为根节点,然后将左右区间分别作为左右子树。/** * Definiti
2016-08-08 21:08:18 275
原创 235.leetcode Lowest Common Ancestor of BST(easy)[二叉搜索树 递归]
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw
2016-08-06 11:39:53 189
原创 260.leetcode Single Number III(medium)[两个单数]
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given
2016-08-06 11:39:39 251
原创 137.leetcode Single Number II(medium)[单数 三次]
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without u
2016-08-06 11:39:25 245
原创 141.leetcode Linked List Cycle(easy)[链表是否有环 快慢指针]
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?题目的意思是判断一个链表是否有环,判断的方法是用快慢两个指针,一个指针每次走两步,一个指针每次走一步,当两个指针相遇时那么存在环。/** * Definiti
2016-08-05 15:56:36 286
原创 58.leetcode Length of Last Word(easy)[字符串 分割]
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is
2016-08-05 15:52:22 178
原创 230.leetcode Kth Smallest Element in a BST(medium)[二叉搜索树 先序遍历 栈]
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:What if the
2016-08-05 15:46:54 231
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人