leetcode
文章平均质量分 64
Clairezz_
这个作者很懒,什么都没留下…
展开
-
[leetcode] Best Time to Buy and Sell Stock II
class Solution {public: //可以交易任意多次单不能同时拥有多于一支股票 //只要将所有的有增长的地方获得的利润加起来即可 int maxProfit(vector& prices) { if (prices.empty()) return 0; int res = 0; for (int i = 1; i < prices.size(); ++i){原创 2015-05-08 13:52:14 · 350 阅读 · 0 评论 -
[leetcode][DP] Best Time to Buy and Sell Stock
class Solution {public: //DP //只做一笔交易取得最大值 //如何在第0...i天获得最大利润?肯定是历史最低价买进,那什么时候卖出呢? //每天有两种选择:卖或不卖 //当天卖能取得最大利润是:当天价格减去历史最低价 //当天不卖能取得最大利润是:前一天能取得的最大利润 //当天能取得的最大利润是以上两者中的最大值 int maxProfit(vec原创 2015-05-08 13:40:37 · 286 阅读 · 0 评论 -
[leetcode][list] Add Two Numbers
题目:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as原创 2015-05-08 20:34:45 · 342 阅读 · 0 评论 -
[leetcode][hash] Two Sum
题目:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the targ原创 2015-05-08 20:10:03 · 363 阅读 · 0 评论 -
[leetcode][string] Longest Substring Without Repeating Characters
题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3.原创 2015-05-08 21:46:28 · 354 阅读 · 0 评论 -
[leetcode][string] ZigZag Conversion
题目:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA原创 2015-05-08 22:14:01 · 330 阅读 · 0 评论 -
[leetcode][math] Palindrome Number
题目:Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of原创 2015-05-08 22:56:12 · 338 阅读 · 0 评论 -
[leetcode][two pointers] 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 line i is at (i, ai) and (i,原创 2015-05-08 23:13:27 · 342 阅读 · 0 评论 -
[leetcode][DP] Best Time to Buy and Sell Stock III
class Solution {public: //DP //最多两笔交易能取得的最大利润 int maxProfit(vector& prices) { if (prices.empty()) return 0; int *tbl1 = new int[prices.size()];//tbl1[i]表示第i天可以取得的最大利润 int *tbl2 = new int[pri原创 2015-05-08 13:54:09 · 385 阅读 · 0 评论 -
[leetcode][大数] Reverse Integer
题目:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask原创 2015-05-08 22:31:25 · 332 阅读 · 0 评论 -
[leetcod][two pointers] 4Sum
题目:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:原创 2015-05-09 18:39:45 · 305 阅读 · 0 评论 -
[leetcode][string] Longest Common Prefix
题目:Write a function to find the longest common prefix string among an array of strings.class Solution {public: //方法一:根据前i个串求出前缀res,再求i+1和res的LCP。此方法的本质是将问题转化为“求两个串的LCP” //这种方法效率不高,可能前面几个字符串的的原创 2015-05-09 12:49:26 · 371 阅读 · 0 评论 -
[leetcode][two pointers][大数] 3Sum Closest
题目:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would hav原创 2015-05-09 18:01:38 · 260 阅读 · 0 评论 -
[leetcode][list][two pointers] 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原创 2015-05-09 20:41:23 · 307 阅读 · 0 评论 -
[leetcode][two pointers] 3Sum
题目: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:Elements in a tripl原创 2015-05-09 15:06:22 · 317 阅读 · 0 评论 -
[leetcode][回溯] 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原创 2015-05-09 19:09:11 · 310 阅读 · 0 评论 -
[leetcode][list] Merge Two Sorted Lists
题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists./** * Definition for singly-linked list. * str原创 2015-05-09 21:05:10 · 282 阅读 · 0 评论 -
[leetcode][回溯] Generate Parentheses
题目:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(()原创 2015-05-09 22:00:33 · 287 阅读 · 0 评论 -
[leetcode][栈] Valid Parentheses
题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are原创 2015-05-09 20:03:28 · 316 阅读 · 0 评论 -
[leetcode][堆] Merge k Sorted Lists
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity./** * Definition for singly-linked list. * struct ListNode { * int val; * ListN原创 2015-05-10 15:37:04 · 284 阅读 · 0 评论 -
[leetcode][two pointers] Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new len原创 2015-05-11 13:21:24 · 284 阅读 · 0 评论 -
[leetcode][array] Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in p原创 2015-05-11 12:34:24 · 343 阅读 · 0 评论 -
[leetcode][string] Implement strStr()
题目:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Update (2014-11-02):The signature of the function had bee原创 2015-05-11 19:21:03 · 313 阅读 · 0 评论 -
[leetcode][search] 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 in t原创 2015-05-12 19:54:27 · 337 阅读 · 0 评论 -
[leetcode][search] 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 n原创 2015-05-12 19:30:34 · 291 阅读 · 0 评论 -
[leetcode][hash] Valid Sudoku
题目:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partia原创 2015-05-13 19:00:08 · 303 阅读 · 0 评论 -
[leetcode][string] Count and Say
题目:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is rea原创 2015-05-13 19:49:13 · 305 阅读 · 0 评论 -
[leetcode][回溯] Gray Code
题目:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the seq原创 2015-05-19 17:08:22 · 510 阅读 · 0 评论 -
[leetcode][list][two pointers] 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 sa原创 2015-05-20 09:10:08 · 256 阅读 · 0 评论 -
[leetcode][回溯] Subsets II
题目:Given a collection of integers that might contain duplicates, nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not c原创 2015-05-19 17:42:55 · 382 阅读 · 0 评论 -
[leetcode][DP]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 [4,−原创 2015-05-16 13:36:18 · 298 阅读 · 0 评论 -
[leetcode][贪心] Jump Game II
题目: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.Yo原创 2015-05-16 12:54:07 · 241 阅读 · 0 评论 -
[leetcode][贪心] Jump Game
题目: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.De原创 2015-05-15 22:29:55 · 392 阅读 · 0 评论 -
[leetcode][string] Length of Last Word
题目: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:原创 2015-05-16 14:09:30 · 269 阅读 · 0 评论 -
[leetcode][回溯] Combination Sum II
题目:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in th原创 2015-05-14 17:50:26 · 312 阅读 · 0 评论 -
[leetcode][Array] Rotate Image
题目:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?class Solution {public: void rotate(vec原创 2015-05-16 13:18:49 · 372 阅读 · 0 评论 -
[leetcode][DP] 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 to r原创 2015-05-16 15:14:52 · 286 阅读 · 0 评论 -
[leetcode][tree] Binary Tree Level Order Traversal II
题目:Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,原创 2015-05-20 10:44:03 · 267 阅读 · 0 评论 -
[leetcode][tree] 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原创 2015-05-20 20:45:28 · 403 阅读 · 0 评论 -
[leetcode][tree] 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].Note: Recursiv原创 2015-05-20 09:34:41 · 280 阅读 · 0 评论