leetcode on 111qqz的小窝
https://111qqz.github.io/tags/leetcode/
Recent content in leetcode on 111qqz的小窝Hugo -- gohugo.iozh-cnFri, 14 Apr 2017 12:25:16 +0000leetcode162. Find Peak Element (O(lgn)复杂度寻找峰值)
https://111qqz.github.io/2017/04/leetcode-162/
Fri, 14 Apr 2017 12:25:16 +0000https://111qqz.github.io/2017/04/leetcode-162/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 return the index to any one of the peaks is fine. You may imagine that num[-1] = num[n] = -∞. For example, in array [1, 2, 3, 1], 3 is aleetcode 152. Maximum Product Subarray (最大连续子序列乘积，dp)
https://111qqz.github.io/2017/04/leetcode-152-maximum-product-subarray/
Fri, 14 Apr 2017 11:33:30 +0000https://111qqz.github.io/2017/04/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 product = 6. 思路：由于有正，有负，还有0.。。所以比最大子串之和要复杂一些。。。 dp[leetcode 228. Summary Ranges
https://111qqz.github.io/2017/04/leetcode-228/
Fri, 14 Apr 2017 10:51:39 +0000https://111qqz.github.io/2017/04/leetcode-228/Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 题意：把连续的数连续表示 思路：模拟。注意有负数，注意有-2147483648这种数据。 本来还想着，可能leetcode 209. Minimum Size Subarray Sum (尺取法)
https://111qqz.github.io/2017/04/leetcode-209-minimum-size-subarray-sum/
Thu, 13 Apr 2017 13:50:02 +0000https://111qqz.github.io/2017/04/leetcode-209-minimum-size-subarray-sum/Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal length under the problem constraint 思路：尺取即可。。好久没写，竟然leetcode 229. Majority Element II （O(1)空间找出现次数大于n/3的元素）
https://111qqz.github.io/2017/04/leetcode-229-majority-element-ii/
Thu, 13 Apr 2017 12:41:33 +0000https://111qqz.github.io/2017/04/leetcode-229-majority-element-ii/Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 题意：给你n个数，要求找出出现此处大于n/3的。。。 思路：之前做过一个找出n个数出leetcode 75. Sort Colors
https://111qqz.github.io/2017/04/leetcode-75-sort-colors/
Thu, 13 Apr 2017 12:02:02 +0000https://111qqz.github.io/2017/04/leetcode-75-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 0, 1, and 2 to represent the color red, white, and blue respectively. 题意：一个数组，由0,1,2组成，现在leetcode 11. Container With Most Water (two pointer)
https://111qqz.github.io/2017/04/leetcode-11-container-with-most-water-two-pointer/
Thu, 13 Apr 2017 10:13:01 +0000https://111qqz.github.io/2017/04/leetcode-11-container-with-most-water-two-pointer/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). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. 题意leetcode 16. 3Sum Closest (k-sum问题，two pointer)
https://111qqz.github.io/2017/04/leetcode-16-3sum-closest/
Thu, 13 Apr 2017 09:45:45 +0000https://111qqz.github.io/2017/04/leetcode-16-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 exactly one solution. 思路： 排序，然后two pointer,复杂度 O(n^2) /* *********************************************** Author :111qqz Created Timeleetcode 18. 4Sum (k-sum问题，two pointer)
https://111qqz.github.io/2017/04/leetcode-18-4sum/
Thu, 13 Apr 2017 09:34:15 +0000https://111qqz.github.io/2017/04/leetcode-18-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 set must not contain duplicate quadruplets. 思路： O(n^2)枚举两个元素，变成2-leetcode 15. 3Sum (k-sum问题，two pointer)
https://111qqz.github.io/2017/04/leetcode-15-3sum/
Thu, 13 Apr 2017 08:21:52 +0000https://111qqz.github.io/2017/04/leetcode-15-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 duplicate triplets. 思路：排序O(nlgn)，然后枚举一个元素O(nleetcode 216. Combination Sum III Add to List (枚举子集，限定集合大小，和为定值）
https://111qqz.github.io/2017/04/leetcode-216-combination-sum-iii-add-to-list/
Thu, 13 Apr 2017 07:55:09 +0000https://111qqz.github.io/2017/04/leetcode-216-combination-sum-iii-add-to-list/Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. 题意：1..9个数，从中选择k个，和为n，要求输出所有满足题意的集合。leetcode 60. Permutation Sequence (求第k个排列)
https://111qqz.github.io/2017/04/leetcode-60-permutation-sequence/
Thu, 13 Apr 2017 07:24:41 +0000https://111qqz.github.io/2017/04/leetcode-60-permutation-sequence/The set [1,2,3,…,_n_] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): 1. `"123"` 2. `"132"` 3. `"213"` 4. `"231"` 5. `"312"` 6. `"321"` Given n and k, return the _k_th permutation sequence. Note: Given n will be between 1leetcode 47. Permutations II (生成全排列，有重复元素)
https://111qqz.github.io/2017/04/leetcode-47-permutations-ii/
Thu, 13 Apr 2017 07:14:03 +0000https://111qqz.github.io/2017/04/leetcode-47-permutations-ii/Given a collection of numbers that might contain duplicates, return all possible unique permutations.__ 思路：和leet code 46 类似，最后用set去个重即可。。 /* *********************************************** Author :111qqz Created Time :2017年04月13日 星期四 15时00分48秒leetcode 46. Permutations (生成全排列，无重复元素)
https://111qqz.github.io/2017/04/leetcode-46-permutations/
Thu, 13 Apr 2017 06:59:43 +0000https://111qqz.github.io/2017/04/leetcode-46-permutations/Given a collection of distinct numbers, return all possible permutations. 思路：调用n-1次 leetcode 31 解题报告 中提到的算法即可。。。 /* *********************************************** Author :111qqz Created Time :2017年04月13日 星期四 14时49分34秒 File Name :46.cpp ************************************************leetcode 31. Next Permutation (in-place 生成下一个全排列)
https://111qqz.github.io/2017/04/leetcode-31-next-permutation-in-place/
Thu, 13 Apr 2017 06:47:55 +0000https://111qqz.github.io/2017/04/leetcode-31-next-permutation-in-place/Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memory. Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column. 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5leetcode 33. Search in Rotated Sorted Array (无重复数的旋转数组找定值)
https://111qqz.github.io/2017/04/leetcode-33-search-in-rotated-sorted-array/
Thu, 13 Apr 2017 06:29:37 +0000https://111qqz.github.io/2017/04/leetcode-33-search-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). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. 思路：找规律。。。leetcode 34. Search for a Range (二分，找到一段值为tar的区间)
https://111qqz.github.io/2017/04/leetcode-34-search-for-a-range/
Thu, 13 Apr 2017 05:34:45 +0000https://111qqz.github.io/2017/04/leetcode-34-search-for-a-range/Given an array of integers sorted in ascending order, 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 the array, return [-1, -1]. For example, Given [5, 7, 7, 8, 8, 10] and target value 8, return [3, 4]. 思路：二分。。。 我好leetcode 39. Combination Sum (dfs，求所有的组合，和为定值，每个数可以重复用)
https://111qqz.github.io/2017/04/leetcode-39-combination-sum/
Thu, 13 Apr 2017 02:47:49 +0000https://111qqz.github.io/2017/04/leetcode-39-combination-sum/Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: * All numbers (including target) will be positive integers. * The solution set must not contain duplicate combinations. 题意：给n个数，求所有leetcode 40. Combination Sum II (枚举子集，和为定值)
https://111qqz.github.io/2017/04/leetcode-40-combination-sum-ii/
Wed, 12 Apr 2017 16:23:29 +0000https://111qqz.github.io/2017/04/leetcode-40-combination-sum-ii/* Total Accepted: **106670** * Total Submissions: **329718** * Difficulty: **Medium** * Contributor: **LeetCode** 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 the combination. Note: * All numbers (including target) will be positive integers. * The solution set must not contain duplicate combinations.leetcode 495. Teemo Attacking
https://111qqz.github.io/2017/04/leetcode-495-teemo-attacking/
Wed, 12 Apr 2017 16:00:57 +0000https://111qqz.github.io/2017/04/leetcode-495-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 time duration per Teemo’s attacking, you need to output the total time that Ashe is in poisoned condition. You may assume that Teemo attacks at the very beginning of a specific time point, and makes Asheleetcode 442. Find All Duplicates in an Array（找出出现两次的元素）
https://111qqz.github.io/2017/04/leetcode-442-find-all-duplicates-in-an-array/
Wed, 12 Apr 2017 15:21:23 +0000https://111qqz.github.io/2017/04/leetcode-442-find-all-duplicates-in-an-array/Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? 思路：还是一个映射，如果某个位置要映射的时候已经为负leetcode 48. Rotate Image (旋转方阵(in place))
https://111qqz.github.io/2017/04/leetcode-48-rotate-image/
Wed, 12 Apr 2017 13:22:33 +0000https://111qqz.github.io/2017/04/leetcode-48-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? 题意：给一个n*n的方阵，要求顺时针旋转90度。 思路：(x,y)->(y,n-1-leetcode 54. Spiral Matrix (矩阵蛇形取数)
https://111qqz.github.io/2017/04/leetcode-54-spiral-matrix/
Tue, 11 Apr 2017 12:07:54 +0000https://111qqz.github.io/2017/04/leetcode-54-spiral-matrix/Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 思路：。。。再次让我回想起高一的暑假。。。。 /* *********************************************** Author :111qqz Created Time :2017年04月11日 星期二 19时42分leetcode 55. Jump Game (dp)
https://111qqz.github.io/2017/04/leetcode-55-jump-game-dp/
Tue, 11 Apr 2017 11:39:15 +0000https://111qqz.github.io/2017/04/leetcode-55-jump-game-dp/Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 思路:dp[i]表示能否到达位置i…无脑dp即可。。。 /* *********************************************** Author :111qqz Created Time :2017年04月11日 星期二leetcode 56. Merge Intervals (模拟，求相交区间)
https://111qqz.github.io/2017/04/leetcode-56-merge-intervals/
Tue, 11 Apr 2017 11:30:32 +0000https://111qqz.github.io/2017/04/leetcode-56-merge-intervals/Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 思路：扫一遍即可。。 /* *********************************************** Author :111qqz Created Time :2017年04月11日 星期二 19时15分30秒 File Name :56.cpp ************************************************ */ /** * Definition for an interval. * struct Interval {leetocde 59. Spiral Matrix II (模拟)
https://111qqz.github.io/2017/04/59-spiral-matrix-ii/
Tue, 11 Apr 2017 11:07:52 +0000https://111qqz.github.io/2017/04/59-spiral-matrix-ii/Given an integer n, generate a square matrix filled with elements from 1 to _n_2 in spiral order. 思路：仿佛回到高一的那个暑假。。。 /* *********************************************** Author :111qqz Created Time :2017年04月11日 星期二 18时52分15秒 File Name :59.cpp ************************************************ */leetocde 63. Unique Paths II
https://111qqz.github.io/2017/04/63-unique-paths-ii/
Tue, 11 Apr 2017 10:50:57 +0000https://111qqz.github.io/2017/04/63-unique-paths-ii/Follow up for “Unique Paths”: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middle of a 3x3 grid as illustrated below. [ [0,0,0], [0,1,0], [0,0,0] ] The total number of unique paths is 2. 题意：从leetcode 64. Minimum Path Sum (二维dp)
https://111qqz.github.io/2017/04/leetcode-64-minimum-path-sum/
Mon, 10 Apr 2017 02:35:20 +0000https://111qqz.github.io/2017/04/leetcode-64-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. Note: You can only move either down or right at any point in time. 数字三角形。。。。从坐上到右下问最短路径。。每次只能向leetcode 73. Set Matrix Zeroes (矩阵置0，乱搞)
https://111qqz.github.io/2017/04/leetcode-73-set-matrix-zeroes/
Mon, 10 Apr 2017 01:16:43 +0000https://111qqz.github.io/2017/04/leetcode-73-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__n) space is probably a bad idea. A simple improvement uses O(m + n) space, but still not the best solution. Could you devise a constant space solution? 直接放leetcode 238. Product of Array Except Self (乱搞)
https://111qqz.github.io/2017/04/leetcode-238-product-of-array-except-self/
Sun, 09 Apr 2017 11:50:05 +0000https://111qqz.github.io/2017/04/leetcode-238-product-of-array-except-self/Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6]. Follow up: Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexityleetcode 79. Word Search (dfs)
https://111qqz.github.io/2017/04/leetcode-79-word-search-dfs/
Fri, 07 Apr 2017 06:59:26 +0000https://111qqz.github.io/2017/04/leetcode-79-word-search-dfs/Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. 思路：dfs即可。记得要回溯一下…leetcode 80 Remove Duplicates from Sorted Array II （有序数组去除重复元素）
https://111qqz.github.io/2017/04/leetcode-80-remove-duplicates-from-sorted-array-ii/
Wed, 05 Apr 2017 13:36:44 +0000https://111qqz.github.io/2017/04/leetcode-80-remove-duplicates-from-sorted-array-ii/Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length. Subscribe to see which companies asked this question. 题意：一个有序数组，leetcode 81. Search in Rotated Sorted Array II (有重复元素的旋转数组找给定值)
https://111qqz.github.io/2017/04/leetcode-81-search-in-rotated-sorted-array-ii/
Wed, 05 Apr 2017 13:17:51 +0000https://111qqz.github.io/2017/04/leetcode-81-search-in-rotated-sorted-array-ii/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). Write a function to determine if a given target is in the array. The array may contain duplicates. 好像阿里一面的时候问过。。。 思路：肯定leetcode 289. Game of Life (模拟)
https://111qqz.github.io/2017/04/leetcode-289-game-of-life/
Wed, 05 Apr 2017 12:03:28 +0000https://111qqz.github.io/2017/04/leetcode-289-game-of-life/According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.” Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article): 1. Anyleetcode 90. Subsets II (枚举子集)
https://111qqz.github.io/2017/04/leetcode-90-subsets-ii/
Wed, 05 Apr 2017 10:45:02 +0000https://111qqz.github.io/2017/04/leetcode-90-subsets-ii/Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, If nums = [1,2,2], a solution is: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 思路： 复习（？）一下 枚举子集的三种写法 （还有种更飘逸的&heleetcode 287. Find the Duplicate Number (floyd判圈算法找重复元素)
https://111qqz.github.io/2017/04/leetcode-287-find-the-duplicate-number-floyd/
Wed, 05 Apr 2017 07:31:49 +0000https://111qqz.github.io/2017/04/leetcode-287-find-the-duplicate-number-floyd/Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: 1. You **must not** modify the array (assume the array is read only). 2. You must use only constant, _O_(1) extra space. 3. Your runtime complexity should be less thanleetcode 532. K-diff Pairs in an Array （找差为k的数对）
https://111qqz.github.io/2017/04/leetcode-532-k-diff-pairs-in-an-array/
Wed, 05 Apr 2017 06:53:02 +0000https://111qqz.github.io/2017/04/leetcode-532-k-diff-pairs-in-an-array/Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k. Example 1: Input: [3, 1, 4, 1, 5], k = 2 Output: 2 Explanation: There are two 2-diff pairs inleetcode 448. Find All Numbers Disappeared in an Array(寻找所有消失的元素）
https://111qqz.github.io/2017/04/leetcode-448-find-all-numbers-disappeared-in-an-array/
Wed, 05 Apr 2017 06:19:06 +0000https://111qqz.github.io/2017/04/leetcode-448-find-all-numbers-disappeared-in-an-array/Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space. Example: Input: [4,3,2,7,8,2,3,1] Output: [5,6] 思路：