LeetCode刷題——5th

難度:簡單/Easy

序號與題目:27——移除元素

給定一個數組 nums 和一個值 val,你需要原地移除所有數值等於 val 的元素,返回移除後數組的新長度。

不要使用額外的數組空間,你必須在原地修改輸入數組並在使用 O(1) 額外空間的條件下完成。

元素的順序可以改變。你不需要考慮數組中超出新長度後面的元素。

示例 1:

給定 nums = [3,2,2,3], val = 3,

函數應該返回新的長度 2, 並且 nums 中的前兩個元素均爲 2。

你不需要考慮數組中超出新長度後面的元素。
示例 2:

給定 nums = [0,1,2,2,3,0,4,2], val = 2,

函數應該返回新的長度 5, 並且 nums 中的前五個元素爲 0, 1, 3, 0, 4。

注意這五個元素可爲任意順序。

你不需要考慮數組中超出新長度後面的元素。

思考:把與給定值不同的元素放到數組的前面即可,與此同時進行的是統計與給定值不同元素的個數,當遍歷完數組後,與給定值不同的元素就在數組的前面了,返回這些元素的個數,就可以通過引用輸出這幾個元素了

實現:

C

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

C++

class Solution {
public:
    int removeElement(vector<int>& nums, int val) 
    {
        int c=0;
        for(int i=0;i<nums.size();i++)
        {
            if(nums[i]!=val)
            {
                nums[c]=nums[i];
                c++;
            }
        }
        return c;
    }
};

Java

class Solution {
    public int removeElement(int[] nums, int val) 
    {
        int c=0;
        for(int i=0;i<nums.length;i++)
        {
            if(nums[i]!=val)
            {
                nums[c]=nums[i];
                c++;
            }
        }
        return c;
    }
}

Python

class Solution:
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        c=0;
        length=len(nums)
        for i in range(length):
            if nums[i]!=val:nums[c]=nums[i];c+=1
        return c

序號與題目:28——實現strStr()

實現 strStr() 函數。

給定一個 haystack 字符串和一個 needle 字符串,在 haystack 字符串中找出 needle 字符串出現的第一個位置 (從0開始)。如果不存在,則返回  -1。

示例 1:

輸入: haystack = "hello", needle = "ll"
輸出: 2
示例 2:

輸入: haystack = "aaaaa", needle = "bba"
輸出: -1
說明:

當 needle 是空字符串時,我們應當返回什麼值呢?這是一個在面試中很好的問題。

對於本題而言,當 needle 是空字符串時我們應當返回 0 。這與C語言的 strstr() 以及 Java的 indexOf() 定義相符。

思考:當needle是空字符串時我們應當返回 0,即needle的長度爲0,返回爲0。當needle的長度大於haystack的長度時,顯然haystack中沒有needle,返回-1。考慮其他情況,先從haystack和needle的第一個字符(haystack[0]和needle[0])依次往後匹配,如匹配失敗,則將haystack開始匹配的位置向後移1個單位,重新匹配(haystack[1]和needle[0]),直到匹配成功位置。假如匹配到needle的最後一個index,那就是成功了,返回i(開始的序號)若到最後也沒有匹配成功,返回-1。

實現:

C

int strStr(char* haystack, char* needle) 
{
    int lenh = strlen(haystack);
    int lenn = strlen(needle);
    if(lenn==0)
        return 0;
    if(lenh<lenn)
        return -1;
    for(int i = 0;  i<=lenh-lenn; i++)     //lenh-lenn防止(i+j)溢出
    {
        for(int j = 0; j<lenn; j++)
        {
            if(haystack[i+j] != needle[j])
            {
                break;                    //跳出第2個for循環,跳到第1個for循環
            }
            if(j == lenn-1)
            {
                return i;
            }
        }
    }
    return -1;
}

C++

class Solution {
public:
    int strStr(string haystack, string needle) 
    {
        int lenh = haystack.size();
        int lenn = needle.size();
        if(lenn==0)
            return 0;
        if(lenh<lenn)
            return -1;
        for(int i = 0;  i<=lenh-lenn; i++)
        {
            for(int j = 0; j<lenn; j++)
            {
                if(haystack[i+j] != needle[j])
                {
                    break;
                }
                if(j == lenn-1)
                {
                    return i;
                }
            }
        }
        return -1;
    }
};

Java

class Solution {
    public int strStr(String haystack, String needle) 
    {
        if(needle.length()==0)
            return 0;
        if(haystack.length()<needle.length())
            return -1;
        for (int i = 0; i < haystack.length()-needle.length()+1; i++) 
		    if (haystack.substring(i, i+needle.length()).equals(needle))
                return i;
	    return -1;
    }
}

Python

class Solution:
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        if not needle:
            return 0

        for i in range(len(haystack) - len(needle) + 1):
            if haystack[i:(i+len(needle))] == needle:
                return i
        return -1

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章