# Leetcode解题笔记（Array）

• [TODO]
2016-08-12更新

1. 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.

Note:
You are not suppose to use the library’s sort function for this problem.

A rather straight forward solution is a two-pass algorithm using counting sort.

First, iterate the array counting number of 0’s, 1’s, and 2’s, then overwrite array with total number of 0’s, then 1’s and followed by 2’s.

Could you come up with an one-pass algorithm using only constant space?

/*solution1 4ms use counting sort*/
class Solution {
public:
void sortColors(vector<int>& nums) {
int i=0,j=0,k=0;
vector<int> zero;
vector<int> one;
vector<int> two;
for(i=0;i<nums.size();i++){
switch(nums[i]){
case 0:
zero.push_back(nums[i]);
break;
case 1:
one.push_back(nums[i]);
break;
case 2:
two.push_back(nums[i]);
break;
}
}
for(i=0;i<zero.size();i++)
nums[i]=zero[i];
for(i,j=0;j<one.size();i++,j++)
nums[i]=one[j];
for(i,k=0;k<two.size();i++,k++)
nums[i]=two[k];
}
};

1. nums[cur]==0 应该将其与low指向的元素交换，因为nums[low]始终 ==1或low ==cur,所以交换后的nums[cur]已经到位,只需cur++，low++
2. nums[cur]==1 已经就位，cur++即可
3. nums[cur]==2 应该将其与high指向的元素作交换，但是high指向的元素的值不确定，所以只能让high–，cur不变

1 0 2 2 1 0
^         ^
L         H
M

Mid != 0 || 2
Mid++

1 0 2 2 1 0
^ ^       ^
L M       H

Mid == 0
Swap Low and Mid
Mid++
Low++

0 1 2 2 1 0
^ ^     ^
L M     H

Mid == 2
Swap High and Mid
High--

0 1 0 2 1 2
^ ^   ^
L M   H

Mid == 0
Swap Low and Mid
Mid++
Low++

0 0 1 2 1 2
^ ^ ^
L M H

Mid == 2
Swap High and Mid
High--

0 0 1 1 2 2
^ ^
L M
H


class Solution {
public:
void sortColors(vector<int>& nums) {
int low=0,high=nums.size()-1,cur=0;
while(cur<=high){
if(nums[cur]==0)
swap(nums[cur++],nums[low++]);
else if(nums[cur]==1)
cur++;
else if(nums[cur]==2)
swap(nums[cur],nums[high--]);
}
}
};

73.Set Matrix Zeroes

2016-08-11更新

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].

Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)

• 既然要算出某一元素以外其他元素的乘积，那么我们可以先算出所有元素的乘积，最后再除以不需要的元素即可

• 对于[0,1]这样的只有两个元素且其中一个元素的值为0的数组，我们发现只要交换一下两个元素然后返回即可。

• 对于长度大于2的数组，又要再分两种情况：
1. 长度大于2且只含有一个0，例如[9 0 3]这样的数组，我们发现只需要把0所在的位置赋值为其他元素的乘积，而其他位置依然为0，返回即可。
2. 长度大于2但含有不止一个0，例如[9 0 0 3]这样的数组，我们容易证明对于这样的数组，要返回的数组的所有元素应该都为0。

Corner case讨论完毕，下面我们看代码:

/*solution1 60ms*/
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int i=0,res=1,count=0,zero_pos=0;
vector<int> output(nums.size(),0);
for(i=0;i<nums.size();i++)
res=res*nums[i];
if(!res){
if(nums.size()==2){
swap(nums[0],nums[1]);
return nums;
}
else if(nums.size()>2){
res=1;
/*计算除0以外所有元素的乘积，并记录0的个数和位置*/
for(i=0;i<nums.size();i++){
if(nums[i]==0){
count++;
zero_pos=i;
continue;
}
res=res*nums[i];
}
if(count==1){
output[zero_pos]=res;
return output;
}
if(count>1)
return output;
}
}
for(i=0;i<nums.size();i++)
output[i]=res/nums[i];
return output;
}
};

/*solution3 60ms*/
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> arr1(nums.size(),1);
vector<int> arr2(nums.size(),1);
for(int i=1;i<arr1.size();i++)
arr1[i]=arr1[i-1]*nums[i-1];
for(int i=arr2.size()-2;i>=0;i--)
arr2[i]=arr2[i+1]*nums[i+1];
for(int i=0;i<arr1.size();i++)
arr1[i]=arr1[i]*arr2[i];
return arr1;
}
};

/*solution3 60ms*/
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> arr1(nums.size(),1);
int tmp=1;
for(int i=1;i<arr1.size();i++)
arr1[i]=arr1[i-1]*nums[i-1];
for(int i=nums.size()-1;i>=0;i--){
arr1[i]=arr1[i]*tmp;
tmp=tmp*nums[i];
}
return arr1;
}
};

228.Summary Ranges

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”].

/*solution1 0ms*/
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int i=0,size=nums.size();
vector<string> res;
if(nums.empty())
return res;
while(i<size){
int j=1;
while(i+j<size&&nums[i+j]-nums[i]==j)
j++;
res.push_back(j<=1?to_string(nums[i]):to_string(nums[i])+"->"+to_string(nums[i+j-1]));
i+=j;
}
return res;
}
};

2016-08-10更新

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.

Hint:

How many majority elements could it possibly have?

class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int curIndex1=0,curIndex2=0,count1=0,count2=0;
int size=nums.size(),i=0,tri=size/3;
vector<int>res;
for(i=0;i<nums.size();i++){
if(nums[i]==nums[curIndex1])
count1++;
else if(nums[i]==nums[curIndex2])
count2++;
else if(!count1){
count1=1;
curIndex1=i;
}
else if(!count2){
count2=1;
curIndex2=i;
}
else{
count1--;
count2--;
}
}
count1=0;
count2=0;
for(i=0;i<nums.size();i++){
if(nums[i]==nums[curIndex1])
count1++;
else if(nums[i]==nums[curIndex2])
count2++;
}
if(count1>tri)
res.push_back(nums[curIndex1]);
if(count2>tri)
res.push_back(nums[curIndex2]);
return res;
}
};

2016-08-09更新

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 exist in the array.

class Solution {
public:
int majorityElement(vector<int>& nums) {
if(nums.size()<2)
return nums[0];
int i=0,j=0,size=nums.size(),count=0;
int half=size/2;;
for(i=0;i<size;i++){
count=0;/*外循环每进行一次count计数变量应该重新置0*/
for(j=0;j<size;j++){
if(nums[j]==nums[i]){
count++;
if(count>half)
return nums[i];
}
}
}
return -1;
}
};

/*solution1 O(n^2) 20ms*/
class Solution {
public:
int majorityElement(vector<int>& nums) {
if(nums.size()<2)
return nums[0];
int i=0,j=0,size=nums.size(),count=0;
int half=size/2;;
for(i=0;i<size;i++){
count=0;/*外循环每进行一次count计数变量应该重新置0*/
/*遇到有重复元素的，我们仅以最后一个元素做参照，来对该元素出现次数进行统计*/
while(nums[i]==nums[i+1])
i++;
for(j=0;j<size;j++){
if(nums[j]==nums[i]){
count++;
if(count>half)
return nums[i];
}
}
}
return -1;
}
};

1.sort

/*solution2 sort 40ms*/
class Solution {
public:
int majorityElement(vector<int>& nums) {
sort(nums.begin(),nums.end());
return nums[nums.size()/2];
}
};


2.hash-table

hash-table的思路比较直接，维护一个unodered-map

/*solution4 use hash-table time-complexity O(n) memory-complexity O(n)*/
class Solution {
public:
int majorityElement(vector<int>& nums) {
if(nums.size()<2)
return nums[0];
unordered_map<int,int> num_count;
int i=0,size=nums.size();
for(i=0;i<size;i++){
if(num_count.find(nums[i])==num_count.end())
num_count[nums[i]]=0;
else
num_count[nums[i]]++;
if(num_count[nums[i]]>=size/2)
return nums[i];
}
return -1;
}
};


3.majority voting algorithm

The algorithm is carried out in two steps:

Eliminate all elements except one.
Iterating through the array of numbers, maintain a current candidate and a counter initialized to 0. With the current element x in iteration, update the counter and (possibly) the candidate: if the counter is 0, set the current candidate to x and the counter to 1. If the counter is not 0, increment or decrement the counter based on whether x is the current candidate.
Determine if the remaining element is a valid majority element.
With the candidate acquired in step 1, iterate through the array of numbers and count its occurrences. Determine if the result is more than half of the sequence’s length. If so, the candidate is the majority. Otherwise, the sequence doesn’t contain a majority.

1. 不断删除数组中的元素直到仅剩下一个
2. 判断剩下的那一个是否真的是majority

class Solution {
public:
int majorityElement(vector<int>& nums) {
int curIndex=0,count=0,i=0;
for(i=0;i<nums.size();i++){
nums[curIndex]==nums[i]?count++:count--;
if(!count){
curIndex=i;
count=1;
}
}
return nums[curIndex];
}
};


2016-08-08更新

1. Find Minimum in Rotated Sorted Array II

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 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.

The array may contain duplicates.

Find Minimum in Rotated Sorted Array I算法的平均时间复杂度为O(log n），而在Find Minimum in Rotated Sorted Array II中，由于有重复元素的存在，那么会对时间复杂度造成影响，在最坏的情况下，比如整个数组由同一个元素组成时，这个算法就不得不一直对high的值进行调整，所以最坏的情况下时间复杂度为O(n）

class Solution {
public:
int findMin(vector<int>& nums) {
int low=0,size=nums.size(),high=size-1,mid=0;
while(low<high){
mid=low+(high-low)/2;
if(nums[low]<nums[high])
return nums[low];
if(nums[mid]>nums[high])
low=mid+1;
else if(nums[mid]<nums[high])
high=mid;
else
high--;
}
return nums[low];
}
};

2016-08-07更新

1. Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a 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.

More practice:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).

class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
if(nums.empty())
return 0;
int i=0,j=0,size=nums.size(),sum=0,min_size=INT_MAX;
for(i=0;i<size;i++){
while(sum<s){
sum+=nums[++j];
}
min_size=min(min_size,j-i+1);
}
return min_size==INT_MAX ? 0:min_size;
}
};

2016-08-06更新

1. Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

• Integers in each row are sorted in ascending from left to right.
• Integers in each column are sorted in ascending from top to bottom.

For example,

Consider the following matrix:

[
[1,   4,  7, 11, 15],
[2,   5,  8, 12, 19],
[3,   6,  9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

/*solution1 264ms*/
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix.empty()||matrix[0].empty())
return false;
int row=0,col=matrix[0].size()-1,size=matrix.size();
while(row<size&&col>=0){
if(matrix[row][col]==target)
return true;
else if(matrix[row][col]>target)
col--;
else if(matrix[row][col]<target)
row++;
}
return false;
}
};

/*solution2 164ms*/
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int row = matrix.size();
int col = matrix[0].size();
int i = row-1,j =0;
while(i>=0&&j<col){
if(target>matrix[i][j])
j++;
else if(target<matrix[i][j])
i--;
else
return true;
}
return false;
}
};
1. Find the Duplicate Number

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 than O(n2).
4. There is only one duplicate number in the array, but it could be repeated more than once.

2016-08-07

class Solution {
public:
int findDuplicate(vector<int>& nums) {
int low=0,size=nums.size(),high=size-1,mid=0,count=0,i=0;
while(low<=high){
count=0;
mid=low+(high-low)/2;
for(i=0;i<size;i++){
if(nums[i]<=mid)
count++;
}
if(count>mid)
high=mid-1;
else
low=mid+1;
}
return low;
}
};


2016-08-05更新

1. Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
For example,

Consider the following matrix:

[
[1,   3,  5,  7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]

Given target = 3, return true.

/*solution1
Traverse the 2D matrix*/
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix.empty()||matrix[0].empty())
return false;
int i=0,j=0;
int row=matrix.size(),col=matrix[0].size();
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(matrix[i][j]==target)
return true;
return false;
}
};

/*solution2
use binary-search in rows*/
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix.empty()||matrix[0].empty())
return false;
int row=matrix.size(),i=0;
int low=0,size=matrix[0].size(),high=size-1,mid=0;
for(i=0;i<row;i++){
low=0;
high=size-1;
while(low<=high){
mid=low+(high-low)/2;
if(matrix[i][mid]==target)
return true;
if(matrix[i][mid]<target)
low=mid+1;
else
high=mid-1;
}
}
return false;
}
};


class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix.empty()||matrix[0].empty())
return false;
int low=0,size=matrix.size(),high=size-1,mid=0;
while(low<=high){
mid=low+(high-low)/2;
if(matrix[mid][0]==target)
return true;
if(matrix[mid][0]>target)
high=mid-1;
else
low=mid+1;
}
int row=high;

if(row<0)
return false;
low=0;
high=matrix[0].size()-1;
mid=0;
while(low<=high){
mid=low+(high-low)/2;
if(matrix[row][mid]==target)
return true;
if(matrix[row][mid]>target)
high=mid-1;
else
low=mid+1;
}
return false;
}
};

2016-08-03更新

1. Plus One

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

/*solution1*/
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int i=0,size=digits.size(),carry=1;
for(i=size-1;i>=0;i--){
int digit=(digits[i]+carry)%10;
carry=(digits[i]+carry)/10;
digits[i]=digit;
if(carry==0)
return digits;
}
vector<int> res(size+1,0);
res[0]=1;
return res;
}
};


/*solution2*/
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int i=0,size=digits.size();
for(i=size-1;i>=0;i--){
if(digits[i]==9)
digits[i]=0;
else{
digits[i]++;
return digits;
}
}
digits[0]=1;
digits.push_back(0);
return digits;
}
};

2016-08-01更新

1. Search for a Range

Given a sorted array of integers, 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].

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> res(2,-1);
if(nums.empty())
return res;
int low=0,size=nums.size(),high=nums.size()-1,mid=0;
while(low<high){
mid=low+(high-low)/2;
if(nums[mid]>target)
high=mid-1;
else if(nums[mid]==target)
high=mid;
else
low=mid+1;
}
if(nums[low]==target)
res[0]=low;

low=0,size=nums.size(),high=size-1,mid=0;
while(low+1<high){
mid=low+(high-low)/2;
if(nums[mid]<target)
low=mid+1;
else if(nums[mid]==target)
low=mid;
else
high=mid-1;
}
if(nums[low+1]==target&&size>1)
res[1]=low+1;
else if(nums[low]==target)
res[1]=low;
else
res[1]=-1;
return res;
}
};

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 previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, …, n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

// Forward declaration of isBadVersion API.

class Solution {
public:
int low=0,high=n-1,mid=0;
while(low<=high){
mid=low+(high-low)/2;
high=mid-1;
else
low=mid+1;
}
return low;
}
};

2016-07-30更新

1. Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Here are few examples.

[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

/*solution1*/
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if(nums.empty()||target<nums[0])
return 0;
int low=0,size=nums.size(),high=size-1,mid=0,i=0;
while(low<=high){
mid=low+(high-low)/2;
if(nums[mid]==target)
return mid;
if(nums[mid]>target)
high=mid-1;
else
low=mid+1;
}
// if not found,let varible i go back to find an exact position
for(i=size;i>=0&&nums[i-1]>target;i--);
return i;
}
};

/*solution2*/
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if(nums.empty()||target<nums[0])
return 0;
int low=0,size=nums.size(),high=size-1,mid=0,i=0;
while(low<=high){
mid=low+(high-low)/2;
if(nums[mid]==target)
return mid;
if(nums[mid]>target)
high=mid-1;
else
low=mid+1;
}
return low;
}
};

1. Search 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).

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.

1. 若A[mid]

class Solution {
public:
int search(vector<int>& nums, int target) {
int size=nums.size(),low=0,high=size-1,mid=0;
while(low<=high){
mid=low+(high-low)/2;
if(target==nums[mid])
return mid;
if(nums[mid]<nums[high]){
if(target>nums[mid]&&target<=nums[high])
low=mid+1;
else
high=mid-1;
}
else{
if(target>=nums[low]&&target<nums[mid])
high=mid-1;
else
low=mid+1;
}
}
return -1;
}
};
1. Search in Rotated Sorted Array II

Follow up for “Search in Rotated Sorted Array”:
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

class Solution {
public:
bool search(vector<int>& nums, int target) {
int size=nums.size(),low=0,high=size-1,mid=0;
while(low<=high){
mid=low+(high-low)/2;
if(target==nums[mid])
return true;
if(nums[mid]<nums[high]){
if(target>nums[mid]&&target<=nums[high])
low=mid+1;
else
high=mid-1;
}
else if(nums[mid]>nums[high]){
if(target>=nums[low]&&target<nums[mid])
high=mid-1;
else
low=mid+1;
}
else
high--;
}
return false;
}
};

2016-07-28更新
11. Container With Most Water

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.

/*solution1，36ms*/
class Solution {
public:
int maxArea(vector<int>& height) {
int size=height.size();
int start=0,end=size-1;
int container=0,max_value=0;
while(start<end){
container=min(height[start],height[end])*(end-start);
max_value=max(container,max_value);
if(height[start]<height[end])
start++;
else
end--;
}
return max_value;
}
};

/*solution2,24ms*/
class Solution {
public:
int maxArea(vector<int>& height) {
int size=height.size();
int start=0,end=size-1;
int container=0,max_value=0,het=0;
while(start<end){
het=min(height[start],height[end]);
container=het*(end-start);
max_value=max(container,max_value);
/*如果不大于当前height，则start继续向前移动*/
while(height[start]<=het&&start<end)
start++;
while(height[end]<=het&&start<end)
end--;
}
return max_value;
}
};

1. 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 the array.

/*solution1*/
class Solution {
public:
int findMin(vector<int>& nums) {
int size=nums.size();
int i=0,min=0;
for(i=0;i<size;i++){
if(nums[i]<nums[min])
min=i;
}
return nums[min];
}
};

1. [0,1,2,3,4,5,6,7]这种情况的特征是最左边的值小于最右边，所以最左边的一定是minimum，直接返回。
2. [4,5,6,7,0,1,2,3]这种情况的特征是中间的数大于最右边的数，所以minimum一定出现在右半区中，调整到右半区继续查找。
3. [5,6,7,0,1,2,3,4]这种情况的特征是中间的数小于最左边的数，所以minimum一定出现在左半区中，调整到左半区继续查找。

/*solution2*/
class Solution {
public:
int findMin(vector<int>& nums) {
int size=nums.size(),low=0,high=size-1,mid=0;
while(low<high){
if(nums[low]<nums[high])
return nums[low];
mid=(low+high)/2;
if(nums[mid]>nums[high])
low=mid+1;
else
high=mid;
}
return nums[low];
}
};

2016-07-27更新
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.

 For example, given array S = {-1 2 1 -4}, and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

1. 这道题要求的是closet而不是equal，所以left要把所有可能试一遍。
2. 题目明确说明答案是唯一确定的，不需要考虑重复问题
3. 需要多维护一个min变量来记录每次循环时的最小值

class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int size=nums.size(),left=0,min=nums[0]+nums[1]+nums[2],mid=0,right=0;
if(size<3)
return 0;
sort(nums.begin(),nums.end());
for(left=0;left<size-2;left++){
int tmp=target-nums[left];
mid=left+1;
right=size-1;
while(mid<right){
if(abs(tmp-nums[mid]-nums[right])<abs(target-min))
min=nums[left]+nums[mid]+nums[right];
if(tmp==nums[mid]+nums[right])
return min;
else if(nums[mid]+nums[right]<tmp)
mid++;
else
right--;
}
}
return min;
}
};
1. 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.

For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
[-1,  0, 0, 1],
[-2, -1, 1, 2],
[-2,  0, 0, 2]
]

solution1 用时92ms
- [ TODO To be faster]

/*solution 1*/
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>>res;
int left=0,mid1=0,mid2=0,right=0,tmp_mid2=0,tmp_right=0,tmp_mid1=0,tmp_left=0;
int size=nums.size();
sort(nums.begin(),nums.end());
if(size<4)
return res;
for(left=0;left<size-3;left++){
int tmp_left=target-nums[left];
/*去除left造成的重复*/
if(left > 0 && nums[left] == nums[left-1])
continue;
for(mid1=left+1;mid1<size-2;mid1++){
int tmp=tmp_left-nums[mid1];
mid2=mid1+1;
right=size-1;
/*去除mid1造成的重复*/
if(mid1 > left+1 && nums[mid1] == nums[mid1-1])
continue;
while(mid2<right){
if(nums[mid2]+nums[right]==tmp){
tmp_mid2=nums[mid2];
tmp_right=nums[right];
while(mid2 < right && nums[++mid2] == tmp_mid2);/*去除mid2造成的重复*/
while(mid2 < right && nums[--right] == tmp_right);
}
else if(nums[mid2]+nums[right]<tmp)
mid2++;
else
right--;
}
}
}
return res;
}
};

2016-07-26更新

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: (Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c))The solution set must not contain duplicate triplets.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]

1. 以数组形式返回
2. 返回结果不能重复
3. 返回结果以升序排列

for(left=0;left<size&&nums[left]<=0;left++)

while(mid<right){
if(nums[mid]+nums[right]==tmp){
int tmp_left=nums[left],tmp_right=nums[right],tmp_mid=nums[mid];
vector<int>triplet(3,0);
triplet[0]=nums[left];
triplet[1]=nums[mid];
triplet[2]=nums[right];
res.push_back(triplet);
}
else if(nums[mid]+nums[right]<tmp)
mid++;
else
right--;
}

for(left=0;left<size&&nums[left]<=0;left++){
tmp=0-nums[left];
mid=left+1;
right=size-1;
while(mid<right){
if(nums[mid]+nums[right]==tmp){
int tmp_left=nums[left],tmp_right=nums[right],tmp_mid=nums[mid];
vector<int>triplet(3,0);
triplet[0]=nums[left];
triplet[1]=nums[mid];
triplet[2]=nums[right];
res.push_back(triplet);
while(mid<right&&nums[++mid]==tmp_mid);/*防止mid造成重复*/
while(mid<right&&nums[--right]==tmp_right);/*防止right造成重复*/
}
else if(nums[mid]+nums[right]<tmp)
mid++;
else
right--;
}
while(left+1<size&&nums[left]==nums[left+1])/*防止*/
left++;
}

2016-07-25更新

189.Rotate Array
- [ TODO 189 use constant space to finish it]
Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

Note:

Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.

class Solution {
public:
void rotate(vector<int>& nums, int k) {
if(k<=0)
return;
int size=nums.size(),i=0;
vector<int>nums2(nums.begin(),nums.end());
for(i=0;i<size;i++){
nums[(i+k)%size]=nums2[i];
}
}
};

solution2：逆置

class Solution {
public:
void rotate(vector<int>& nums, int k) {
int size=nums.size();
k=k%size;
reverse(nums.begin(),nums.begin()+size-k);/*reverse的两个参数只包含左不包含右*/
reverse(nums.begin()+size-k,nums.end());
reverse(nums.begin(),nums.end());
}
};

217.Contains Duplicate I

Given an array of integers, find if the array contains any duplicates.

Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if(nums.size()==0||nums.size()==1)
return false;
sort(nums.begin(),nums.end());
int i=0;
for(i;i<nums.size();i++){
if(nums[i]==nums[i+1])
return true;
}
return false;
}
};

219.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.

class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int,int> hash_table;
int i=0;
for(i=0;i<nums.size();i++){
if(hash_table.find(nums[i])!=hash_table.end()&&i-hash_table[nums[i]]<=k)
return true;
hash_table[nums[i]]=i;
}
return false;
}
};
• [ TODO Contains Duplicate III]

2016-07-24更新
27. Remove Element

Given an array and a value, remove all instances of that value in place and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.

class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int new_len=0,i=0,size=nums.size();
for(i=0;i<nums.size();i++){
if(nums[i]!=val){
nums[new_len++]=nums[i];
}
}
return new_len;
}
};
1. 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]
]

，对于第k（k > 2）层第n（n > 1 && n < k）个元素A[k][n]有

A[k][n] = A[k-1][n-1] + A[k-1][n]

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> val;
val.resize(numRows);
int i=0,j=0;
for(i=0;i<numRows;i++){
val[i].resize(i+1);
val[i][0]=1;
val[i][val[i].size()-1]=1;
for(j=1;j<val[i].size()-1;j++)
val[i][j]=val[i-1][j-1]+val[i-1][j];
}
return val;
}
};
• [ TODO 119. Pascal’s Triangle II ]

2016-07-23更新

1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

Example:Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

1. 最基础最容易想到，也就是遍历数组，一个一个加起来看是否等于target，速度极慢，用C写的程序运行时间超过了200ms，代码如下:

/*first solution,very slow*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target) {
int i=0,j=0,k=0;
int *arr=(int *)malloc(2*sizeof(int));;
for(i=0;i<numsSize;i++){
for(j=i+1;j<numsSize;j++){
if(nums[i]+nums[j]==target){
arr[0]=i;
arr[1]=j;
}
}
}
return arr;
}


2.数组中的元素与索引为一个一一对应的关系，为了提高查找效率，考虑引入了hash table，从头到尾对数组进行遍历，一旦有元素的值等于target-nums[i]，则返回其下标即可，代码如下:

/*second solution,faster,using hash table*/
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
unordered_map<int,int> hash;
int i=0,numToFind=0;
for(i=0;i<nums.size();i++){
numToFind=target-nums[i];
if(hash.find(numToFind)!=hash.end()){
result.push_back(hash[numToFind]);
result.push_back(i);
}
hash[nums[i]]=i;
}
return result;
}
};
1. 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 constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size()==0)
return 0;
if(nums.size()<2)
return 1;
int i=0,new_len=1;
for(i=1;i<nums.size();i++){
if(nums[i]!=nums[i-1]){
nums[new_len]=nums[i];
new_len++;
}
}
return new_len;
}
};
1. Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.

class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i=0,j=0,k=0;
vector<int> temp;
while(i<m&&j<n){
if(nums1[i]<nums2[j]){
temp.push_back(nums1[i]);
i++;
}
else{
temp.push_back(nums2[j]);
j++;
}
}
while(i<m){
temp.push_back(nums1[i]);
i++;
}
while(j<n){
temp.push_back(nums2[j]);
j++;
}
for(i=0;i<temp.size();i++)
nums1[i]=temp[i];
}
};
1. Move Zeroes

Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.

1.遍历数组，将数组中所有非0值通过赋值放到前面来，并且记录0的个数，然后在最后把0补进去。见代码:

class Solution {
public:
void moveZeroes(vector<int>& nums) {
int j = 0;
// move all the nonzero elements advance
for (int i = 0; i < nums.size(); i++) {
if (nums[i] != 0) {
nums[j++] = nums[i];
}
}
for (;j < nums.size(); j++) {
nums[j] = 0;
}
}
};

2.利用vector内置函数

class Solution {
public:
void moveZeroes(vector<int>& nums) {
fill(remove(nums.begin(),nums.end(),0),nums.end(),0);
}
};