LeetCode26/27/28 三道easy Remove Duplicates from Sorted Array / Remove Element / Implement strStr( )

26Remove 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;
    }
};



27Remove  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 of nums 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;
    }
};


28Implement 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().

AC 代码,哈希表的优化有空再改,ORZ...
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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值