26. 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 by modifying the input array in-place with O(1) extra memory.
Example:
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 new length.
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int len = nums.size();
if(len <= 1) return len;
int p = 1;
int v = 0;
for(int i = 1; i < len; i++)
{
if(nums[i] != nums[v])
{
v = i;
nums[p++] = nums[i];
}
}
nums.resize(p);
return p;
}
};
//法2: STL
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
return unique(nums.begin(), nums.end()) - nums.begin();
}
};
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 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:
Given 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) {
nums.erase(remove(nums.begin(), nums.end(), val), nums.end());
return nums.size();
}
};
28
.
Implement strStr()
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll" Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba" Output: -1
class Solution {
public:
int strStr(string haystack, string needle) {
return haystack.find(needle);
}
};
29
.
Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
//重复倍增减法
class Solution {
public:
int divide(int dividend, int divisor) {
int o = 1;
if((dividend < 0 && divisor > 0) || (dividend >= 0 && divisor < 0)) o = -1;
long long a, b, c;
a = dividend; b = divisor;
a = abs(a);
b = abs(b);
c = b;
long long ans = 0, f = 1;
while(a >= c)
{
b = c; f = 1;
while(a >= b){a -= b;b += b;ans += f;f += f;}
}
ans *= o;
if(ans >= (1ll<<31)) ans = (1ll<<31)-1;
return ans;
}
};
30
.
Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9]
.
(order does not matter).
//check()函数有点乱,而且超级暴力
class Solution {
public:
bool check(string &s, int l, int r, vector<string> &words)
{
int len = words[0].length();
map<int, int> mp;
for(int i = 0; i < words.size(); i++)
{
int k = s.find(words[i], l);
while(mp[k] && k != -1)
{
k = s.find(words[i], k+len);
}
while((k - l) % len != 0 && k < r && k != -1)
{
int nx = k+len-((k-l)%len);
k = s.find(words[i], nx);
while(mp[k] && k != -1) k = s.find(words[i], k+len);
}
if(k == r && len != 1) return false;
if(k == -1 || k > r ) return false;
mp[k] = 1;
}
return true;
}
vector<int> findSubstring(string s, vector<string>& words) {
int len = s.length(), x = words[0].length()*words.size();
vector<int> ans;
for(int i = 0; i + x - 1 < len; i++)
{
if(check(s, i, i+x-1, words)) ans.push_back(i);
}
return ans;
}
};