26. Remove Duplicates from Sorted Array (就是STL里面的move)
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.
Example 2:
Given nums = [0,0,1,1,1,2,2,3,3,4],
Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy) int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller. // using the length returned by your function, it prints the first len elements. for (int i = 0; i < len; i++) { print(nums[i]); }
AC代码:(就是前面有多少个重复的数字,你往前挪多少个坑就好了- - )
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int count = 0;
for(int i = 1; i < nums.size(); i ++){
if(nums[i] == nums[i-1])
++count;
else
nums[i-count] = nums[i];
}
return nums.size()-count;
}
};
27. 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 = [1,1,2], 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.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2, Your function should return length = 5, with the first five elements ofnums being modified to 0, 1, 3, 0, and 4.
Note that the order of those five elements can be arbitrary.
It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy) int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller. // using the length returned by your function, it prints the first len elements. for (int i = 0; i < len; i++) { print(nums[i]); }
AC代码:(就是前面有多少个重复的数字,你往前挪多少个坑就好了- - )
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int len = nums.size();
int count = 0;
for(int i = 0; i < len; i++){
if(nums[i] == val)
count++;
else
nums[i-count] = nums[i];
}
return len - count;
}
};
28. Implement strStr( )
这道题就每次比较n个字母,O( N* M ), 但是用哈希表其实可以优化成 O ( N )。
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
Clarification:
What should we return when needle
is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle
is an empty string. This is consistent to C's strstr() and Java's indexOf().
class Solution {
public:
int strStr(string haystack, string needle) {
if(needle == "")
return 0;
int i = 0; int flag = 0;
int len = needle.size();
int totalLen = haystack.size();
while(totalLen >= len){
if(cmp(haystack, needle, i , len)){
flag = 1;
break;
}
totalLen --;
i ++;
}
if((i < haystack.size()) && flag == 1)
return i;
else
return -1;
}
bool cmp(string & hay, string & needle, int i, int len){
int index = 0;
while(len--){
if(hay[i++] != needle[index++])
return false;
}
return true;
}
};