1、Contains Duplicate
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.
C++
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
set<int> newnums(nums.begin(),nums.end());
if(newnums.size()==nums.size())
return false;
else
return true;
}
};
2、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.
C++
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int size=digits.size();
for(int i=size-1;i>=0;i--){
digits[i]+=1;
if(digits[i]<10){
break;
}
else if(digits[i]%10==0){
digits[i]=0;
//进位
if(i==0)
digits.insert(digits.begin(),1);
}
}
return digits;
}
};
3、Shortest Word Distance
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = "coding", word2 = "practice", return 3.
Given word1 = "makes", word2 = "coding", return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
C++
int shortestDistance(vector<string>& words, string word1, string word2){
int size=words.size()
int pos1=-1;int pos2=-1; int res=size+1;
for(int i=1;i<size;i++){
if(words[i-1]==word1) pos1=i-1;
if(words[i-1]==word2) pos2=i-1;
if(words[i]==word1 && pos2>-1) res=min(res, i-pos2);
if(words[i]==word2 && pos1>-1) res=min(res, i-pos1);
}
return res;
}
4、Rotate Array
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.
Hint:
Could you do it in-place with O(1) extra space?
Related problem: Reverse Words in a String II
C++
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n=nums.size();
if(n==1 || k==n)
return;
if(k>n) k=k%n;
for(int i=0;i<k;i++){
int tmp=nums.back();
nums.pop_back();
nums.insert(nums.begin(),tmp);
}
}
};
5、Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
C++
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
vector<int> index;
for(int i=0;i<nums.size();i++){
if(nums[i]==val)
index.push_back(i);
}
while(!index.empty()){
int idx=index.back();
nums.erase(nums.begin()+idx);
index.pop_back();
}
return nums.size();
}
};
6、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.
C++
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int pos = 0;
for (int i = 0; i < nums.size(); ++i) {
if (i == 0 || nums[i] != nums[pos - 1])
nums[pos++] = nums[i];
}
return pos;
}
};
7、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"].
C++
class Solution {public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> res;
string s;
if(nums.empty())
return res;
int start=nums[0],end=nums[0];
for (int i = 1; i <= nums.size(); ++i) {
if (i<nums.size() && nums[i] == end + 1)
end = nums[i];
else {
if (start == end)
s = to_string(start);
else
s = to_string(start) + "->" + to_string(end);
res.push_back(s);
if (i < nums.size())
start = end = nums[i];
}
}
return res;
}
};
8、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]
]
C++
class solution{
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int> > res;
vector<int> tmp;
if(numRows==0)
return res;
tmp.push_back(1);
res.push_back(tmp);
if(numRows==1)
return res;
tmp.push_back(1);
res.push_back(tmp);
if(numRows==2)
return res;
for(int i=2;i<numRows;i++){
vector<int> c(i+1,1);
for(int j=1;j<i;j++){
c[j]=res[i-1][j]+res[i-1][j-1];
res.push_back(c);
}
}
return res;
}
}
9、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?
C++
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<vector<int> > res;
vector<int> r;
r.push_back(1);
res.push_back(r);
if (rowIndex==0){return res[0];}
r.push_back(1);
res.push_back(r);
if (rowIndex==1){return res[1];}
int numRows=rowIndex+1;
for (int i=2;i<numRows;i++){
vector<int> c(i+1,1);
for (int j=1;j<i;j++){
c[j]= res[i-1][j]+res[i-1][j-1];
}
res.push_back(c);
}
return res[rowIndex];
}
};
10、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.
C++1:
class Solution {
public:
void moveZeroes(vector<int>& nums) {
vector<int> tmp;
for(int i=0;i<nums.size();i++){
if(nums[i]==0)
tmp.push_back(i);
}
int zeros=tmp.size();
while(!tmp.empty()){
int t=tmp.back();
nums.erase(nums.begin()+t);
tmp.pop_back();
}
for(int i=0;i<zeros;i++){
nums.insert(nums.end(),0);
}
}
};
C++2:
class Solution {
public:
void moveZeroes(vector<int>& nums) {
for (int i = 0, j = 0; i < nums.size(); ++i) {
if (nums[i]) {
swap(nums[i], nums[j++]);
}
}
}
};
11、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.
C++
class solution{
public:
void mergesort(vector<int>&nums1, int m,vector<int>&nums2, int n)
{
int cout=m+n-1;
m--;
n--;
while(m>=0 && n>=0){
nums1[count--]=nums1[m]>nums2[n]?nums1[m--]:nums2[n--];
}
while(n>=0) nums1[count--]=nums2[n--];
}
}
12、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.
C++
class Solution {
public:
int majorityElement(vector<int>& nums) {
int majorityIndex = 0;
for(in cnt=1, i=1;i<nums.size();i++){
nums[majorityIndex]==nums[i]?cnt++;cnt--;
if(cnt==0){
cnt=1;
majorityIndex=i;
}
}
return nums[majorityIndex];
}
};
13、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> lookup;
for (int i = 0; i < nums.size(); ++i) {
if (lookup.find(nums[i]) == lookup.end()) {
lookup[nums[i]] = i;
} else {
// It the value occurs before, check the difference.
if (i - lookup[nums[i]] <= k) {
return true;
}
// Update the index of the value.
lookup[nums[i]] = i;
}
}
return false;
}
};