LeetCode
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].
void moveZeroes(vector<int>& nums) {
int n = nums.size();
vector<int>res;
for (int i = 0; i<n; i++)
{
if (nums[i] != 0)
res.push_back(nums[i]);
}
int m = n - res.size();
vector<int>temp(m, 0);
for (int j = 0; j<m; j++)
{
res.push_back(temp[j]);
}
nums = res;
}
2.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.
vector<int> findDisappearedNumbers(vector<int>& nums) { vector<int>res;
for(int i=0;i<nums.size();++i)
{
int temp=abs(nums[i])-1;
nums[temp]=(nums[temp]>0)?-nums[temp]: nums[temp];
}
for(int i=0;i<nums.size();++i)
{
if(nums[i]>0)res.push_back(i+1);
}
return res;
}
3.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.
int majorityElement(vector<int>& nums) {
int n=nums.size();
if(n<=0)return 0;
sort(nums.begin(),nums.end());
return nums[n/2];
}
4.Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
int maxSubArray(vector<int>& nums) {
int res=INT_MIN,cursum=0;
for(int num:nums){
cursum=max(cursum+num,num);
res=max(res,cursum);
}
return res;
}
分治法
int maxSubArray(vector<int>& nums) {
if(nums.empty()) return 0;
int len=nums.size();
return helper(nums,0,len-1);
}
int helper(vector<int>& nums,int left,int right){
if(left>=right)return nums[left];
int mid=left+(right-left)/2;
int lmax=helper(nums,left,mid-1);
int rmax=helper(nums,mid+1,right);
int mmax=nums[mid],t=mmax;
for(int i=mid-1;i>=left;i--){
t+=nums[i];
mmax=max(mmax,t);
}
t=mmax;
for(int i=mid+1;i<=right;i++){
t+=nums[i];
mmax=max(mmax,t);
}
return max(mmax,max(lmax,rmax));
}
5.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, and you may not use the same element twice.
vector<int> twoSum(vector<int>& num, int target) {
unordered_map<int,int>mapping;
vector<int>res;
for(int i=0;i<num.size();i++){
mapping[num[i]]=i;
}
for(int i=0;i<num.size();i++){
const int gap=target-num[i];
if(mapping.find(gap)!=mapping.end() && mapping[gap]>i){
res.push_back(i);
res.push_back(mapping[gap]);
break;
}
}
return res;
}
6.Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.
You need to find the shortest such subarray and output its length.
int findUnsortedSubarray(vector<int>& nums) {
vector<int> num2=nums;
sort(num2.begin(),num2.end());
int begin=-1,end=-2;
for(int i=0;i<num2.size();i++){
if(num2[i]!=nums[i]){
if(begin==-1)
begin=i;
end=i;
}
}
return end-begin+1;
}
7.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).
vector<int> productExceptSelf(vector<int>& nums) {
int n=nums.size();
vector<int>res(n,1);
for(int i=1;i<n;++i){
res[i]=res[i-1]*nums[i-1];
}
int right=1;
for(int i=n-1;i>=0;--i){
res[i]*=right;
right*=nums[i];
}
return res;
}
8.string reverse 2
solution1
string reverseStr(string s, int k) {
int len=s.size(),cnt=len/k;
for(int i=0;i<=cnt;i++){
if(i%2==0){
if(i*k+k<len)
reverse(s.begin()+i*k,s.begin()+i*k+k);
else
reverse(s.begin()+i*k,s.end());
}
}
return s;
}
solution2
string reverseStr(string s, int k) {
for(int i=0;i<s.size();i+=2*k){
reverse(s.begin()+i,min((s.begin()+i+k),s.end()));
}
return s;
}
9.First Unique Character in a String
int firstUniqChar(string s) {
unordered_map<char,int>map;
for(char c:s) map[c]++;
for(int i=0;i<s.size();i++)
if(map[s[i]]==1)return i;
return -1;
}
10. Implement strStr()
solution1
int strStr(string haystack, string needle) {
int len2=needle.size();
int len1=haystack.size();
int n=0;
if(len2==0)return 0;
if(len1<len2)return -1;
for(int i=0;i<len1-len2+1;i++){
int j=0;
for(;j<len2;j++){
if(haystack[i+j]!=needle[j]) break;
}
if(j==len2) return i;
}
return -1;
}
KMS algorithm
11. Longest Common Prefix
string longestCommonPrefix(vector<string>& strs) {
if(strs.empty())return "";
string res="";
for(int j=0;j<strs[0].size();j++){
char c=strs[0][j];
for(int i=1;i<strs.size();i++){
if(j>=strs[i].size()||strs[i][j]!=c)
return res;
}
res.push_back(c);
}
return res;
}