LeetCode #4(#26, #27, #28 )

26. 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.

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]);
}
//Solution
//总结:设置两个指针进行比对,注意要做判断不能越界
int removeDuplicates(int* nums, int numsSize){
    int i,cnt=0;
    for( i =0;i<numsSize;i++)
    {
        if(i+1<numsSize && nums[i]==nums[i+1]) continue;
        else nums[cnt++] = nums[i];
    }
    return cnt;
}

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 = [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.

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 containing 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 = removeElement(nums, val);

// 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]);
}
//Solution
//总结:设置变量,创建一个船新的数组即可

int removeElement(int* nums, int numsSize, int val){
    int cnt = 0;
    for(int i =0;i<numsSize;i++)
    {
        if(nums[i]!=val) nums[cnt++] = nums[i];
    }
    return cnt;
}


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

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

//Solution
//总结:首先想到是java的indexOf()查找子串,时间很快,空间很大;
//然后想想怎么用C写用了strncmp(),当然strstr也行,查找特定长度的子串比对

int strStr(char * haystack, char * needle){
    int len1 = strlen(haystack);
    int len2 = strlen(needle);
    if((len2==0&&len1==0) || len2==0 ) return 0; //这也‘几把’恶心 1.“” “” 2.“” “a”
    for(int i = 0;i<=len1 -len2;i++) //注意这了要等于  3.“a” “a”
    {
        if(haystack[i]==needle[0])
        {
            if(strncmp(haystack+i,needle,len2)==0)
            {
                return i;
            }
        }
    }
    return -1;
}

class Solution {
    public int strStr(String haystack, String needle) {
        var s = haystack.indexOf(needle); 
        return s;
    }
}
发布了72 篇原创文章 · 获赞 10 · 访问量 5840

猜你喜欢

转载自blog.csdn.net/weixin_44198992/article/details/105305514
今日推荐