
leetcode
爱橙子的OK绷
时刻准备着。。。
展开
-
leetcode165---Compare Version Numbers
问题描述:Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and原创 2016-01-15 22:52:34 · 376 阅读 · 0 评论 -
leetcode122---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. 给你一个数组,里边的第i个元素存储的是股票在第i天的价格 Design an algorithm to find the maximum profit. You may complete as many tra原创 2015-05-28 17:50:55 · 430 阅读 · 0 评论 -
leetcode121---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. 给你一个数组,里边的第i个元素存储的是股票在第i天的价格 If you were only permitted to complete at most one transaction (ie, buy one a原创 2015-05-28 17:41:31 · 560 阅读 · 0 评论 -
leetcode123---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. 给你一个数组,里边的第i个元素存储的是股票在第i天的价格 Design an algorithm to find the maximum profit. You may complete at most two原创 2015-05-29 13:15:54 · 384 阅读 · 0 评论 -
leetcode 188---Best Time to Buy and Sell Stock IV
问题描述:Say you have an array for which the ith element is the price of a given stock on day i. 给你一个数组,里边的第i个元素存储的是股票在第i天的价格 Design an algorithm to find the maximum profit. You may complete at most k t转载 2015-05-29 13:36:12 · 483 阅读 · 0 评论 -
leetcode15---3Sum
问题描述:给定一个数组,在此数组中寻找出3个数,使得3个数的和为0;并且3个数按非递减顺序输出。如输入:{-1,0,1,2,-1,-4};得到:-1 -1 2-1 0 1代码:#include <iostream>#include<vector>#include<algorithm>using namespace std;class Solution {public: vector原创 2015-12-14 21:47:17 · 311 阅读 · 0 评论 -
leetcode17---Letter Combinations of a Phone Number
问题描述: 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 stri原创 2015-12-15 20:38:35 · 342 阅读 · 0 评论 -
leetcode19---Remove Nth Node From End of List
问题描述:Given a linked list, remove the nth node from the end of list and return its head.For example,Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the linked l原创 2015-12-17 22:12:56 · 327 阅读 · 0 评论 -
leetcode23---Merge k Sorted Lists
问题描述:有k个已经排好序的链表,将其合并为一个有序链表。代码:#include <iostream>#include<stdlib.h>#include<vector>using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {原创 2015-12-18 23:28:11 · 371 阅读 · 0 评论 -
leetcode25---Reverse Nodes in k-Group
问题描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it原创 2015-12-20 23:59:09 · 517 阅读 · 0 评论 -
leetcode29---Divide Two Integers
问题描述:Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.也就是不用乘、除、取模运算实现两个数的除法。问题求解:因为不能用乘除法和取余运算,我们只能使用位运算和加减法。 任何一个整数可以表示成以2的幂为底的一组基的线性组合,即n原创 2015-12-21 15:51:31 · 436 阅读 · 0 评论 -
leetcode30---Substring with Concatenation of All Words
问题描述:You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once an原创 2015-12-22 15:29:18 · 384 阅读 · 0 评论 -
leetcode32---Longest Valid Parentheses
问题描述: Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.For “(()”, the longest valid parentheses substring is “()”, wh原创 2015-12-23 21:41:13 · 385 阅读 · 0 评论 -
leetcode98---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’s key. The原创 2015-12-26 13:15:41 · 384 阅读 · 0 评论 -
leetcode99---Recover Binary Search Tree(morris中序遍历恢复BST)
问题描述: Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note: A solution using O(n) space is pretty straight forward. Could you devise原创 2015-12-28 11:06:02 · 508 阅读 · 0 评论 -
leetcode34--- Search for a Range
问题描述: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 in t原创 2015-12-24 09:42:07 · 267 阅读 · 0 评论 -
leetcode67---Add Binary(二进制加法)
问题描述:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".问题求解:二进制加法都是从最低位开始加(从右加到左)。所以对两个字符串要从最后一位开始加。每次相加之前先加上进位。class Solution {public: st原创 2016-01-18 21:49:14 · 493 阅读 · 0 评论 -
leetcode129---Sum Root to Leaf Numbers
问题描述: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 total sum原创 2016-01-01 14:17:26 · 349 阅读 · 0 评论 -
leetcode112---Path Sum
问题描述:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example: Given the below binary tree and su原创 2016-01-01 19:28:15 · 303 阅读 · 0 评论 -
leetcode110---Balanced Binary Tree
问题描述:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2016-01-02 21:47:00 · 307 阅读 · 0 评论 -
leetcode169---Majority Element(数组中出现次数超过一半的数)
问题描述:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element al原创 2016-01-20 09:53:25 · 1397 阅读 · 0 评论 -
leetcode74---杨氏矩阵查找SearchIn2DMatrix
题目叙述: 在行和列都单增的矩阵里查找一个数是否存在 (Leetcode 74)题目求解: 代码实现(c++)class Solution {public: //利用vector<vector<int>>& matrix表示二维整型数组 bool searchMatrix(vector<vector<int>>& matrix, int target) {原创 2015-05-05 14:06:15 · 657 阅读 · 0 评论 -
leetcode257---Binary Tree Paths
问题描述:Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]问题求解:利用先序遍历,深度优先搜索方法求解。对此题而言原创 2016-01-04 09:42:50 · 326 阅读 · 0 评论 -
leetcode240---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.Integers in eac原创 2016-01-20 11:06:02 · 292 阅读 · 0 评论 -
leetcode104---Maximum Depth of Binary Tree
问题描述: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.方法一:深度优先搜索!/** * Definition for a b原创 2016-01-04 22:38:24 · 391 阅读 · 0 评论 -
leetcode111---Minimum Depth of Binary Tree
问题描述: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.方法一:DFS/** * Definition for a binar原创 2016-01-04 23:22:05 · 310 阅读 · 0 评论 -
leetcode215---Kth Largest Element in an Array(第k大元素)
问题描述:Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.Not原创 2016-01-21 23:06:58 · 426 阅读 · 0 评论 -
leetcode278---First Bad Version
问题描述:You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on原创 2016-01-21 23:51:39 · 365 阅读 · 0 评论 -
leetcode69---Sqrt(x)(求x的平方根)
问题描述:Implement int sqrt(int x).Compute and return the square root of x.问题求解:二分法。O(logN)class Solution {public: int mySqrt(int x) { long long low =1; long long high=x; lon原创 2016-01-22 11:26:00 · 641 阅读 · 0 评论 -
leetcode33---Search 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).You are given a target value to search. If found in the array return its原创 2016-01-22 15:16:59 · 284 阅读 · 0 评论 -
leetcode81---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 target is in the arr原创 2016-01-22 15:20:11 · 537 阅读 · 0 评论 -
leetcode222---Count Complete Tree Nodes(求完全二叉树节点数)
问题描述:Given a complete binary tree, count the number of nodes.In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as po原创 2016-01-22 22:28:26 · 843 阅读 · 1 评论 -
leetcode153---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 duplicate exists in the array.原创 2016-01-22 23:08:01 · 413 阅读 · 0 评论 -
leetcode162---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, in that原创 2016-01-23 11:35:42 · 427 阅读 · 0 评论 -
leetcode230---Kth Smallest Element in a BST(BST中寻找第K小)
问题描述: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.再BST中找出第k小的元素。问题求解:中序遍历,只遍历k个原创 2016-01-23 16:36:54 · 424 阅读 · 0 评论 -
leetcode290---Word Pattern
问题描述:Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.Exampl原创 2016-01-06 23:42:47 · 451 阅读 · 0 评论 -
leetcode300---Longest Increasing Subsequence(最长递增子序列)
问题描述:Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefor原创 2016-01-24 23:03:35 · 1558 阅读 · 0 评论 -
leetcode209-Minimum Size Subarray Sum(最小长度子数组和)
问题描述:Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn’t one, return 0 instead.For example, given the array [2,3,1原创 2016-01-25 23:15:37 · 1335 阅读 · 0 评论 -
leetcode303-Range Sum Query - Immutable
问题描述:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRange(0,原创 2016-01-26 23:53:40 · 307 阅读 · 0 评论 -
leetcode198-House Robber
问题描述:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent h原创 2016-01-27 00:28:54 · 291 阅读 · 0 评论