* 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 …
阅读更多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 …
阅读更多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?
思路:还是一个映射,如果某个位置要映射的时候已经为负了,就说明之前映射过该位置,那么该位置对应的元素就是出现了两个的元素。
阅读更多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-x);
阅读更多Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
思路:。。。再次让我回想起高一的暑假。。。。
1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月11日 星期二 19时42分05秒 4File Name :54.cpp 5************************************************ */ 6 …
阅读更多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即可。。。
1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月11日 星期二 19时33分51秒 4File Name :55.cpp 5 …
阅读更多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].思路:扫一遍即可。。
1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月11日 星期二 19时15分30秒 4File Name :56.cpp 5 …
阅读更多Given an integer n, generate a square matrix filled with elements from 1 to _n_2 in spiral order.
思路:仿佛回到高一的那个暑假。。。
1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月11日 星期二 18时52分15秒 4File Name :59.cpp 5************************************************ */ 6class Solution { 7 8 …
阅读更多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
1and0respectively in the grid.For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
1[ 2 [0,0,0], 3 [0,1,0 …
阅读更多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.
数字三角形。。。。从坐上到右下问最短路径。。每次只能向下或者向右。。。
阅读更多Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
**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 …
阅读更多Given an array of n integers where n > 1,
nums, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnums[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: …
阅读更多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即可。记得要回溯一下…
1/* …
阅读更多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 being1,1,2,2and3. It doesn’t matter what you leave beyond the new length.
阅读更多Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e.,
0 1 2 4 5 6 7might become4 5 6 7 0 1 2).Write a function to determine if a given target is in the array.
The array may contain duplicates.
好像阿里一面的时候问过。。。
思路:肯定是二分。。。不过由于有重复元素。。。所以很恶心。。。
阅读更多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 …
阅读更多/* ***********************************************
1Author :111qqz 2Created Time :2017年04月05日 星期三 16时49分57秒 3File Name :106.cpp 4************************************************ */ 5/** 6 * Definition for a binary tree node. 7 * struct TreeNode { 8 * int val; 9 * TreeNode *left; 10 * TreeNode * …
阅读更多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 …
阅读更多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:
1Input: [3, 1, 4, 1, 5], k = 2 2Output: 2 3Explanation: There …
阅读更多