- 博客(270)
- 收藏
- 关注
原创 Leetcode 171. Excel Sheet Column Number
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example:A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 s思路:
2017-02-08 02:49:21 193
原创 Leetcode 169. 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 always
2017-02-07 16:39:00 117
原创 Leetcode 168. Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example:1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB s思路: 1. 把10进制转换26进制。 2. 这种一一映射,找出背后的数学公式(n-
2017-02-07 16:28:56 291
原创 Leetcode 167. Two Sum II - Input array is sorted
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 numbers suc
2017-02-07 15:36:31 270
原创 Leetcode 166. Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in parentheses.For exam
2017-02-07 15:28:56 164
原创 Leetcode 165. 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 conta
2017-02-07 10:39:37 109
原创 Leetcode 162. 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 case
2017-02-07 09:50:35 201
原创 Leetcode 160. Intersection of Two Linked Lists
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 ↘ c1
2017-02-07 09:30:03 109
原创 Leetcode 155. Min Stack
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 the
2017-02-07 08:47:28 183
原创 Leetcode 153. Find Minimum in Rotated Sorted Array
Suppose an array sorted in ascending order 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 exist
2017-02-07 08:12:54 212
原创 Leetcode 152. 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 largest produ
2017-02-07 07:38:40 288
原创 Leetcode 151. Reverse Words in a String
Given an input string, reverse the string word by word.For example, Given s = “the sky is blue”, return “blue is sky the”.Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) spa
2017-02-07 03:44:33 173
原创 Leetcode 150. Evaluate Reverse Polish Notation
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”, “+”, “3”, ““]
2017-02-06 14:51:20 134
原创 Leetcode 148. Sort List
Sort a linked list in O(n log n) time using constant space complexity.s思路: 1. 排序+链表。o(nlgn)的方法有merge sort,quick sort。 2. 用merge sort要每次用快慢指针找中点!把一个链表从中间分开成两个链表,分别排序,然后再merge到一起。class Solution {publi
2017-02-06 14:32:51 117
原创 Leetcode 147. Insertion Sort List
Sort a linked list using insertion sort.s思路: 1. insertion sort这个思路是这样的:遍历从第二个数据开始,第一个数据默认是排好序的,把第二个数据和第一个数比较插入,从而sorted的序列expanded by one, while the unsorted sequence is shrinked by one as well. 复杂度o(
2017-02-06 13:40:04 196
原创 Leetcode 144. Binary Tree Preorder Traversal
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 solution is trivial, could
2017-02-06 10:23:00 187
原创 Leetcode 143. Reorder List
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 {1,4,2,3}
2017-02-06 10:01:55 149
原创 Leetcode 142. Linked List Cycle II
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?s思路: 1. 如何用快慢指
2017-02-06 09:05:58 95
原创 Leetcode 141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?s思路: 1. 经典的题目!在这里突然想总结一下链表的操作:链表元素插入和删除;链表的单向移动。为实现链表元素插入和删除,可能的方法包括dummy node和pointer-to-
2017-02-06 08:49:30 167
原创 Leetcode 139. Word Break
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assum
2017-02-06 08:18:50 166
原创 Leetcode 138. Copy List with Random Pointer
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.s思路: 1. 如果正常的链表,要拷贝就容易:从左往右访问,遇到一个节点,就
2017-02-06 07:10:28 1211
原创 Leetcode 137. Single Number II
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you i
2017-02-06 02:42:41 139
原创 Leetcode 136. Single Number
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 extra me
2017-02-05 14:33:34 159
原创 Leetcode 134. Gas Station
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 its nex
2017-02-05 14:28:27 175
原创 Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = “aab”, Return[ [“aa”,”b”], [“a”,”
2017-02-05 12:50:06 248
原创 Leetcode 130. Surrounded Regions
Given a 2D board containing ‘X’ and ‘O’ (the letter 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 XX
2017-02-05 10:52:57 168
原创 Leetcode 129. 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 of al
2017-02-05 09:10:36 219
原创 Leetcode 127. Word Ladder
Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a time.
2017-02-05 08:25:27 475
原创 Leetcode 125. Valid Palindrome
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 palin
2017-02-05 06:13:23 194
原创 Leetcode 122. 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.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and
2017-02-05 03:19:16 125
原创 Leetcode 121. 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.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), desi
2017-02-05 02:45:52 110
原创 Leetcode 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7],
2017-02-04 14:28:42 135
原创 Leetcode 119. Pascal's Triangle II
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?s思路: 1. 假设我们需要k=5,我们已经得到
2017-02-04 13:40:20 110
原创 Leetcode 118. Pascal's Triangle
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]]s思路: 1. 可以先产生这个框架:比如:5个vector,每个vec
2017-02-04 13:13:51 89
原创 Leetcode 117. Populating Next Right Pointers in Each Node II
Follow up for problem “Populating Next Right Pointers in Each Node”.What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant extra space.
2017-02-04 12:55:58 115
原创 Leetcode 116. Populating Next Right Pointers in Each Node
Given a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is no next right node,
2017-02-04 09:38:06 143
原创 Leetcode 114. Flatten Binary Tree to Linked List
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 \ 2 \ 3
2017-02-04 09:02:56 257
原创 Leetcode 113. Path Sum II
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 / \ 4 8
2017-02-04 05:22:52 138
原创 Leetcode 112. 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 sum = 2
2017-02-04 04:45:06 218
原创 Leetcode 111. 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.s思路: 1. 最短路径。和找最长路径的方法一样啊。。。recursive,二选一
2017-02-03 14:32:17 168
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人