leetcode
文章平均质量分 79
zhaopengnju
这个作者很懒,什么都没留下…
展开
-
二叉树的最大路径和与最远结点距离
首先确定一下单路径的定义:某个结点的单路径就是只经过该结点且不同时包含其左右子树上的结点的路径一、求连接二叉树的任意两个结点的路径的最大和分析:对于二叉树上的每一个结点,求出经过它的路径的最大和,并更新这个最大和即可。经过它的最大和路径可能有三种情况:1、其左子结点的最大单路径和大于0,且其右子结点的最大单路径和大于0,那么经过该结点的最大和路径通过该结点跨过左右子树2、其左子原创 2016-06-25 20:58:19 · 4329 阅读 · 0 评论 -
leetcode——Jump Game II
贪心和动态规划原创 2016-06-18 15:27:10 · 308 阅读 · 0 评论 -
leetcode——Majority Element II
题目:Given an integer array of size n, find all elements that appear more than⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.分析: 针对“选择出现次数大于n/2的元素”的问题,每次舍弃两个不同的元原创 2016-07-04 17:20:31 · 291 阅读 · 0 评论 -
leetcode——Find the Duplicate Number
题目:Given an array nums containing n + 1 integers where each integer is between 1 andn (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate nu原创 2016-07-04 20:53:59 · 308 阅读 · 0 评论 -
leetcode——Intersection of Two Arrays II
题目:Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].class Solution {public: vector intersect(vect原创 2016-07-15 20:08:02 · 399 阅读 · 0 评论 -
leetcode——Longest Substring Without Repeating Characters
题目:Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is原创 2016-07-05 15:35:33 · 208 阅读 · 0 评论 -
leetcode——Merge Intervals
区间合并问题原创 2016-06-19 14:32:58 · 202 阅读 · 0 评论 -
leetcode——First Missing Positive
题目:Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses const原创 2016-06-21 11:50:21 · 189 阅读 · 0 评论 -
leetcode——Spiral Matrix
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原创 2016-06-21 13:58:54 · 333 阅读 · 0 评论 -
leetcode——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"Minimum window is "原创 2016-07-06 21:17:13 · 302 阅读 · 0 评论 -
leetcode——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 verti原创 2016-06-21 15:02:45 · 377 阅读 · 1 评论 -
leetcode——Maximal Rectangle
题目: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 is a histogram where widt原创 2016-06-22 11:15:21 · 470 阅读 · 2 评论 -
leetcode——Single Number III
题目一:Given an array of integers, every element appearstwice except for one. Find that single one.class Solution {public: int singleNumber(vector& nums) { int res = 0; for (au原创 2016-07-07 19:43:14 · 232 阅读 · 0 评论 -
leetcode——Combination Sum
子集问题的应用原创 2016-06-17 23:05:02 · 213 阅读 · 0 评论 -
leetcode——Combination Sum II
子集问题的应用原创 2016-06-17 22:56:06 · 197 阅读 · 0 评论 -
leetcode——Maximum Product Subarray
题目:Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the lar原创 2016-06-28 21:22:17 · 298 阅读 · 0 评论 -
leetcode——Count Primes
题目:输出小于n的质数的个数方法一:能不能快速判断一个数字n是不是质数呢?1、遍历所有小于n的整数,如果都不可以整除那么n为质数2、遍历所有小于sqrt(n)的整数,如果都不可以整除那么n为质数3、遍历所有小于sqrt(n)的质数,如果都不可以整除那么n为质数显然3是最快的做法,因此我们在搜索到一个质数后要将其保存到数组后面,下次直接遍历质数数组来判断n是不是质数c原创 2016-07-10 19:53:08 · 305 阅读 · 0 评论 -
leetcode——Isomorphic Strings
题目:Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced wi原创 2016-07-10 21:54:32 · 282 阅读 · 0 评论 -
leetcode——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 multiple peaks,原创 2016-06-30 10:47:00 · 302 阅读 · 0 评论 -
leetcode——Find Minimum in Rotated Sorted Array II
题目一: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 duplicate exists i原创 2016-06-30 10:58:44 · 252 阅读 · 0 评论 -
leetcode——Minimum Size Subarray Sum
题目:Given an array of n positive integers and a positive integers, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.For example, given the arra原创 2016-06-30 13:14:10 · 204 阅读 · 0 评论 -
leetcode——Search in Rotated Sorted Array
指导思想:二分查找 二分查找维护两个指针first和last,在迭代过程中必须保证first指向待查找元素的左边(包括待查找元素),last指向待查找元素的右边(不包括待查找元素)且first和last之间的距离变短,这一不变条件一定要至死捍卫! 本题遵循上述的原则,也可以采用二分查找的策略在旋转有序数组中进行目标查找。但是有一个注意点就是需要判断mid(first和last的原创 2016-06-13 22:13:06 · 258 阅读 · 0 评论 -
leetcode——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 lowest pos原创 2016-06-15 12:25:02 · 237 阅读 · 0 评论 -
leetcode——Container With Most Water
题目:Given n non-negative integers a1,a2, ..., an, where each represents a point at coordinate (i,ai). n vertical lines are drawn such that the two endpoints of linei is at (i, ai) and (i, 0).原创 2016-06-15 12:27:57 · 207 阅读 · 0 评论 -
leetcode——Median of Two Sorted Arrays
题目: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)).typedef vector::iterat原创 2016-06-15 12:29:58 · 268 阅读 · 0 评论 -
leetcode——Search for a Range
二分查找找出最前面的目标和最后面的目标原创 2016-06-15 22:49:03 · 220 阅读 · 0 评论 -
leetcode——Search Insert Position
二叉查找的改进原创 2016-06-15 23:11:38 · 198 阅读 · 0 评论 -
leetcode——String to Integer (atoi)
一、如果字符串为空,返回0二、去除字符串首部的空格三、判断正负号四、判断并转化数字字符,如果遇到非数字字符,就返回已转化的值五、分别考虑正数和负数的溢出情况class Solution {public: int myAtoi(string str) { if (str.empty()) return 0; int i = 0;原创 2016-08-31 22:31:45 · 328 阅读 · 0 评论