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.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution {
public:
vector<int> twoSum(vector<int> &nums,int target){
unordered_map<int,int>mapping;
vector<int> a;
for(int i=0;i<nums.size();i++)
mapping[nums[i]]=i;
for(int i=0;i<nums.size();i++)
{
unsigned int result=target-nums[i];
if(mapping.find(result)!=mapping.end()&&mapping[result]>i)
{ a.push_back(i);
a.push_back(mapping[result]);
}
}
return a;
}
};
Remove Duplicates from Sorted Array
Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.
Example 1:
Given 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 returned length.
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.empty())return 0;
int index =0 ;
for(int i=1;i<nums.size();i++)
{
if(nums[index]!=nums[i])
nums[++index]=nums[i];
}
return index+1;
}
};
Remove Element
Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn't matter what you leave beyond the returned length.
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int result=0;
int k=nums.size();
for(int i=k-1;i>=0;i--)
if(nums[i]==val)
nums.erase(nums.begin()+i);
result=nums.size();
return result;
}
};
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.
Example 1:
Input: [1,3,5,6], 5
Output: 2
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
for(int i=0;i<nums.size();i++){
if(nums[i]==target)
return i;
if(nums[i]>target)
return i;
int a=nums.back();
if(a<target)
return nums.size();
}
}
};
Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
- The number of elements initialized in nums1 and nums2 are m and n respectively.
- You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
Example:
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n)
{
int end = m;
int start = 0;
for( int i = 0; i < nums2.size(); ++i )
{
while (nums1[start] <= nums2[i] && start < end )
start++;
for (int k = end; k >= start; --k)
{
nums1[k] = nums1[k - 1];
}
nums1[start] = nums2[i];
end++;
}
}
};
Pascal's Triangle
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> v;
for(int i=1;i<=numRows;i++)
{
vector<int> r(i,1);
if(i>2&&i<=numRows)
{
for(int j=1;j<i-1;j++)
{
r[j]=v[i-2][j-1]+v[i-2][j];
}
}
v.push_back(r);
}
return v;
}
};
Plus One
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
digits.back()+=1;
int i;
for(i=digits.size()-1;i>0;i--)
{
while(digits[i]>=10)
{
digits[i]=digits[i]%10;
digits[i-1]+=1;
}
}
while(digits[0]>=10)
{
digits[0]=digits[0]%10;
digits.insert(digits.begin(),1);
}
return digits;
}
};
Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
Note:
- Your returned answers (both index1 and index2) are not zero-based.
- You may assume that each input would have exactly one solution and you may not use the same element twice.
Example:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
unordered_map<int,int>mapping;
vector<int> a;
for(int i=0;i<numbers.size();i++)
mapping[numbers[i]]=i;
for(int i=0;i<numbers.size();i++)
{
unsigned int result=target-numbers[i];
if(mapping.find(result)!=mapping.end()&&mapping[result]>i)
{ a.push_back(i+1);
a.push_back(mapping[result]+1);
}
}
return a;
}
};
Rotate Array
Given an array, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Input:[1,2,3,4,5,6,7]
and k = 3 Output:[5,6,7,1,2,3,4]
Explanation: rotate 1 steps to the right:[7,1,2,3,4,5,6]
rotate 2 steps to the right:[6,7,1,2,3,4,5]
rotate 3 steps to the right:[5,6,7,1,2,3,4]
class Solution {
public:
void rotate(vector<int>& nums, int k) {
if(k>0){
if(nums.size()>k)
{
nums.insert(nums.begin(),nums.end()-k,nums.end());
nums.erase(nums.end()-k,nums.end());
}
if(nums.size()<k)
{
int a=k%nums.size();
nums.insert(nums.begin(),nums.end()-a,nums.end());
nums.erase(nums.end()-a,nums.end());
}
}
}
};
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.
Example 1:
Input: [3,2,3]
Output: 3
class Solution {
public:
int majorityElement(vector<int>& nums) {
if(!nums.size()) return 0;
map<int,int> table;
for(int each : nums) {
++table[each];
if(table[each]>nums.size()/2)
return each;
}
return 0;
}
};
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.
Example 1:
Input: [1,2,3,1]
Output: true
class Solution
{
public:
bool containsDuplicate(vector<int>& nums)
{
int size = nums.size();
if(size <= 1) return false;
sort(nums.begin(), nums.end());
for(int i = 0; i < size-1; ++i)
{
if(nums[i] == nums[i+1]) return true;
}
return false;
}
};
Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n
, find the one that is missing from the array.
Example 1:
Input: [3,0,1]
Output: 2
class Solution {
public:
int missingNumber(vector<int>& nums) {
sort(nums.begin(),nums.end());
for(int i=0;i<nums.size();i++)
if(i!=nums[i])
return i;
return nums.size();
}
};
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.
Example:
Input:[0,1,0,3,12]
Output:[1,3,12,0,0]
class Solution {
public:
void moveZeroes(vector<int>& nums) {
if(nums.size()<2) return;
int count = 0;
for(int i=0; i<nums.size(); i++){
if(!nums[i]) count++;
else swap(nums[i-count], nums[i]);
}
return;
}
};
void moveZeroes(vector<int>& nums) {
int end=nums.size();
if(end<2) return;
for(int i=0;i<end;)
{
if(nums[i]!=0)
i++;
if(nums[i]==0)
{
nums.erase(nums.begin()+i);
end--;
nums.push_back(0);
}
}
}
Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
Example 1:
Input: [3, 2, 1]
Output: 1
Explanation: The third maximum is 1.
class Solution {
public:
int thirdMax(vector<int>& nums) {
sort(nums.begin(),nums.end());
int count=1;int p=0;
for(int i=nums.size()-1;i>0;i--)
{
if(nums[i-1]!=nums[i])
count++;
if(count==3)
p=i-1;
}
if(count<3)
return nums[nums.size()-1];
else
return nums[p];
}
};
Positions of Large Groups
In a string S
of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like S = "abbxxxxzyy"
has the groups "a"
, "bb"
, "xxxx"
, "z"
and "yy"
.
Call a group large if it has 3 or more characters. We would like the starting and ending positions of every large group.
The final answer should be in lexicographic order.
Example 1:
Input: "abbxxxxzzy"
Output: [[3,6]]
Explanation: "xxxx" is the single
large group with starting 3 and ending positions 6.
class Solution {
public:
vector<vector<int>> largeGroupPositions(string S) {
vector<vector<int>> vec;
int i;
int count=1;
for(i=0;i<S.size();i++)
{
if(S[i]==S[i+1])
count++;
if(S[i]!=S[i+1]||i==S.size()-1)
{
if(count>=3)
{
vector<int> v(2,0);
v[0]=i-count+1;
v[1]=i;
vec.push_back(v);
}
count=1;
}
}
return vec;
}
};