
LeetCode
文章平均质量分 60
yyfly22
行走在孤独小径上的程序猿一只
展开
-
LeetCode: Container With Most Wate
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, 0). Fin原创 2017-02-14 21:07:11 · 302 阅读 · 0 评论 -
LeetCode: Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers原创 2016-09-20 00:14:28 · 226 阅读 · 0 评论 -
LeetCode: 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?原创 2016-09-06 17:41:19 · 227 阅读 · 0 评论 -
LeetCode: 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 place with原创 2016-09-06 18:34:25 · 191 阅读 · 0 评论 -
LeetCode: 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?/** * Definition for singly-linked list. * struct ListNode { * int val;原创 2016-09-06 17:37:13 · 260 阅读 · 0 评论 -
LeetCode: 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 reach the原创 2016-09-19 12:02:51 · 243 阅读 · 0 评论 -
LeetCode: Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.题目解析: 到达当前位置(i, j)只有两条路经要么是从左边(i, j- 1)过来,要么从原创 2016-09-19 11:12:02 · 193 阅读 · 0 评论 -
LeetCode: Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y原创 2016-09-06 12:55:22 · 206 阅读 · 0 评论 -
LeetCode: Kth Largest Element in an Array
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.原创 2016-08-26 17:54:31 · 403 阅读 · 0 评论 -
LeetCode: 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原创 2016-09-17 20:19:55 · 186 阅读 · 0 评论 -
LeetCode: Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integerk. Define a pair (u,v) which consists of one element from the first array and one element from the second a原创 2016-08-25 10:13:43 · 257 阅读 · 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, in原创 2016-09-16 12:31:43 · 337 阅读 · 0 评论 -
LeetCode: 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 number原创 2016-09-15 11:42:03 · 201 阅读 · 0 评论 -
LeetCode: Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n =原创 2016-09-14 23:48:49 · 215 阅读 · 0 评论 -
LeetCode: Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?题目解析:第n个台阶只可能两种方式达到,从n-1阶走一步原创 2016-09-14 23:31:37 · 201 阅读 · 0 评论 -
LeetCode: Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.源代码:/** * Definition原创 2016-09-02 16:55:51 · 254 阅读 · 0 评论 -
LeetCode: 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原创 2016-09-07 22:48:04 · 242 阅读 · 0 评论 -
LeetCode: Count Primes
Description:Count the number of prime numbers less than a non-negative number, n.Credits:Special thanks to @mithmatt for adding this problem and creating all test cases.Hint:Let's原创 2016-09-07 23:08:25 · 258 阅读 · 0 评论 -
LeetCode: Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j]and the difference between i and j is at most k.原创 2016-09-08 22:06:33 · 238 阅读 · 0 评论 -
LeetCode: 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: The solution原创 2017-02-14 16:12:10 · 269 阅读 · 0 评论 -
LeetCode: 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 have exact原创 2017-02-13 22:37:35 · 285 阅读 · 0 评论 -
LeetCode: 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: The solution set must not contain原创 2017-02-13 22:15:11 · 208 阅读 · 0 评论 -
LeetCode: Teemo Attacking
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning tim原创 2017-02-13 10:37:44 · 347 阅读 · 0 评论 -
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原创 2017-02-13 10:26:14 · 207 阅读 · 0 评论 -
LeetCode: Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).Example 1:Input: [3, 2,原创 2017-02-11 21:14:18 · 208 阅读 · 0 评论 -
LeetCode: Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consecutiv原创 2017-02-11 20:28:59 · 194 阅读 · 0 评论 -
LeetCode: Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.Example:nums = [1, 2, 3]target = 4The pos原创 2017-02-11 20:16:10 · 278 阅读 · 0 评论 -
LeetCode: Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.Note:Each of the array原创 2017-02-09 18:18:24 · 271 阅读 · 0 评论 -
LeetCode: Target Sum
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.Find out原创 2017-02-09 18:12:38 · 290 阅读 · 0 评论 -
LeetCode: Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Now your job is to find the total Hamming distance between all pairs of the giv原创 2017-02-09 11:03:57 · 413 阅读 · 0 评论 -
LeetCode: Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note:0 ≤ x,原创 2017-02-09 10:42:11 · 254 阅读 · 0 评论 -
LeetCode: Combination Sum
Given a set of candidate numbers (C)(without duplicates) and a target number (T), find all unique combinations inC where the candidate numbers sums to T.The same repeated number may be chosen原创 2017-02-07 16:54:44 · 243 阅读 · 0 评论 -
LeetCode: Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inC where the candidate numbers sums to T.Each number in C may only be used once in the combinatio原创 2017-02-07 16:53:35 · 235 阅读 · 0 评论 -
Leetcode: Combination Sum III
Find all possible combinations of k numbers that add up to a numbern, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Example 1:Input:原创 2017-02-07 16:51:33 · 232 阅读 · 0 评论 -
LeetCode: 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 the原创 2016-09-02 16:44:47 · 224 阅读 · 0 评论 -
LeetCode: Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5源代码:/** * Definition for原创 2016-09-02 16:38:56 · 177 阅读 · 0 评论 -
LeetCode: Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(m原创 2016-08-23 18:27:31 · 173 阅读 · 0 评论 -
LeetCode:Kth Smallest Element in a Sorted Matrix
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order, not原创 2016-08-20 17:23:01 · 265 阅读 · 0 评论 -
LeetCode: Reverse Linked List
Reverse a singly linked list.源代码1:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */c原创 2016-08-30 13:55:23 · 185 阅读 · 0 评论 -
LeetCode: Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements.For example,Given [1,1,1,2,2,3] and k = 2, return [1,2].Note: You may assume k is always valid, 1 ≤ k ≤ number原创 2016-08-26 09:31:56 · 237 阅读 · 0 评论