- 博客(139)
- 收藏
- 关注
原创 leetcode #165 in cpp
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 co
2016-07-01 11:23:29
318
原创 *leetcode #162 in cpp
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
2016-06-30 11:15:21
305
原创 leetcode #160 in cpp
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘
2016-06-30 10:33:43
449
原创 leetcode #155 in cpp
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get
2016-06-29 11:54:16
268
原创 leetcode #154 in cpp
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pivot u
2016-06-29 11:35:02
256
原创 leetcode #153 in cpp
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
2016-06-29 11:04:00
208
原创 leetcode #153 in cpp
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
2016-06-29 11:00:13
232
原创 leetcode #152 in cpp
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 largest
2016-06-29 10:47:58
237
原创 leetcode #151 in cpp
Solution:1. reversed the whole string2. reversed each word in the reversed string.3. remove multiple space between words. Code:class Solution {public: void reverseWords(string &s) {
2016-06-29 10:28:55
200
原创 leetcode #150 in cpp
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1",
2016-06-28 11:15:17
230
原创 leetcode #149 in cpp
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.Solution: three points,p1,p2,p3, are on the same line if and only if slope(p2) == slope(p3)
2016-06-28 10:59:58
219
原创 leetcode #145 in cpp
Solution:The post-order is the reverse of the root->right->left order. The root->right->left order is similar to pre-order traversal. Thus we first do a pseudo-pre-order traversal, by collecting roo
2016-06-26 12:06:47
255
原创 leetcode #144 in cpp
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive soluti
2016-06-26 11:52:14
241
原创 Leetcode #143 in cpp
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 to
2016-06-26 06:31:59
208
原创 #leetcode #142 in cpp
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?So
2016-06-26 05:46:02
202
原创 leetcode #141 in cpp
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?Solution;Make a slow pointer which step 1 in each iteration and a fast pointer
2016-06-25 12:01:24
197
原创 leetcode #140 in cpp
Code:class Solution {public: vector wordBreak(string s, unordered_set& wordDict) { vector res; if(s.empty()) return res; int n = s.length(); vector> prev(n,ve
2016-06-25 11:56:39
179
原创 leetcode #139 in cpp
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet"
2016-06-25 11:08:21
204
原创 *leetcode 138 in cpp
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.The problem is to maintain the
2016-06-24 11:28:47
370
原创 *leetcode #136 in cpp
Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using ext
2016-06-22 12:38:15
229
原创 *leetcode #135 in cpp
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least on
2016-06-22 12:27:27
195
原创 *leetcode #134 in cpp
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to
2016-06-22 11:49:04
174
原创 *leetcode #132 in cpp
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Return
2016-06-22 04:14:48
132
原创 leetcode #131 in cpp
Solution:We have already done in #4 about how to find palindrome in a string. Given isPal[i][j] which represents whether s[i...j] is palindrome, we could scan through the array to find the first pal
2016-06-21 04:28:09
190
原创 leetcode #4 in cpp
Code:class Solution {public: string longestPalindrome(string s) { int n = s.length(); bool dp[n][n] = {false}; int maxx = 1; int begin = 0; int end =
2016-06-21 04:03:40
133
原创 leetcode #130 in cpp
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,X X X XX O O X
2016-06-21 02:28:15
179
原创 leetcode #129 in cpp
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-06-20 13:28:25
155
原创 *leetcode #128 in cpp
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3
2016-06-20 13:17:35
250
原创 leetcode #127 in cpp
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord toendWord, such that:Only one letter can be changed at a t
2016-06-20 12:32:56
173
原创 leetcode #125 in cpp
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a
2016-06-20 05:01:29
187
原创 *leetcode #124 in cpp
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-06-20 04:45:58
161
原创 *leetcode #123 in cpp
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You may
2016-06-20 02:54:52
286
原创 leetcode #122 in cpp
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy on
2016-06-19 07:13:50
220
原创 leetcode #121 in cpp
Solution:We should look for increasing order in the list. Suppose we have an increasing list, then the maximum profit is the list end - list start in this list. Suppose we have the increasing list
2016-06-19 06:57:19
150
原创 leetcode #119 in cpp
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?Code:class S
2016-06-19 06:06:27
168
原创 leetcode #118 in cpp
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]Code:class Solution {p
2016-06-19 05:57:18
166
原创 leetcode #117 in cpp
Solution:Similar to #116. Only difference is that we push left or right into queue when left/right is not NULL.Code:/** * Definition for binary tree with next pointer. * struct TreeLinkNode {
2016-06-19 05:41:33
229
原创 leetcode #116 in cpp
Solution:perform BFS to traverse the tree level by level using a queue. For each level, pop current front node, link the node's next to the new front node in the queue.Code:/** * Definition fo
2016-06-19 05:31:23
198
原创 *leetcode #115 in cpp
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be non
2016-06-19 05:19:34
171
原创 leetcode #114 in cpp
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1
2016-06-18 22:12:58
591
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人