2023年4月29日 星期六

4/31 每日一題

 Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

 

Example 1:

Input: haystack = "sadbutsad", needle = "sad"
Output: 0
Explanation: "sad" occurs at index 0 and 6.
The first occurrence is at index 0, so we return 0.

Example 2:

Input: haystack = "leetcode", needle = "leeto"
Output: -1
Explanation: "leeto" did not occur in "leetcode", so we return -1.

 

Constraints:

  • 1 <= haystack.length, needle.length <= 104
  • haystack and needle consist of only lowercase English characters.

import re
haystack = "sadbutsad"
needle = "sad"
if re.search(needle,haystack):
    for i in re.finditer(needle,haystack):
        #finditer是一個可迭代資料,可以找出「每次」匹配到的索引
        print(i.span())  #回傳一組tuple
if re.search(needle,haystack):
    print(re.search(needle,haystack).start())
    #start()回傳開始的索引,但他只會找到第一個



---------------
終端機

PS C:\Users\K\OneDrive\桌面\AUTOKI> & C:/Users/K/AppData/Local/Programs/Python/Python311/python.exe c:/Users/K/OneDrive/桌面/AUTOKI/tes.py (0, 3) (6, 9) 0 PS C:\Users\K\OneDrive\桌面\AUTOKI>

標籤:

4/30 每日一題

 Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:

  • Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.
  • Return k.

Custom Judge:

The judge will test your solution with the following code:

int[] nums = [...]; // Input array
int val = ...; // Value to remove
int[] expectedNums = [...]; // The expected answer with correct length.
                            // It is sorted with no values equaling val.

int k = removeElement(nums, val); // Calls your implementation

assert k == expectedNums.length;
sort(nums, 0, k); // Sort the first k elements of nums
for (int i = 0; i < actualLength; i++) {
    assert nums[i] == expectedNums[i];
}

If all assertions pass, then your solution will be accepted.

 

Example 1:

Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).

 

Constraints:

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100


class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        n=len(nums)
        k=0
        while k < n:
            if nums[k] == val:  #遇到val時取出val
                nums.pop(k)    
                n -= 1          #被取出時, 總數少一
                nums.append("_") #填補被取出的空缺
            else:
                k +=1    #如果不是時,指針往下一項移動
        return k

標籤:

4/29每日一題 (重複地用_代替)

給定一個按非遞減順序nums排序的整數數組,就地刪除重複項,使每個唯一元素只出現一次元素的相對順序應該保持不變然後返回中唯一元素的數量nums

nums考慮to be的獨特元素的數量k,要被接受,您需要做以下事情:

  • 更改數組nums,使 的第一個k元素nums按照它們最初出現的順序包含唯一元素nums的其餘元素nums與 的大小一樣不重要nums
  • 返回k

自定義法官:

法官將使用以下代碼測試您的解決方案:

int[] nums = [...]; // 輸入數組
int[] expectedNums = [...]; // 長度正確的預期答案

int k = removeDuplicates(nums); // 調用你的實現

斷言 k == expectedNums.length;
對於 (int i = 0; i < k; i++) {
    斷言 nums[i] == expectedNums[i];
}

如果所有斷言都通過,那麼你的解決方案將被接受

 

示例 1:

輸入: nums = [1,1,2]
輸出: 2, nums = [1,2,_]
解釋:你的函數應該返回 k = 2,nums 的前兩個元素分別為 1 和 2。
除了返回的 k(因此它們是下劃線)之外,你留下什麼並不重要。

示例 2:

輸入: nums = [0,0,1,1,1,2,2,3,3,4]
輸出: 5, nums = [0,1,2,3,4,_,_,_,_, _]
解釋:你的函數應該返回k = 5,nums的前五個元素分別是0、1、2、3和4。
除了返回的 k(因此它們是下劃線)之外,你留下什麼並不重要。

 

約束:

  • 1 <= nums.length <= 3 * 104
  • -100 <= nums[i] <= 100
  • nums是按非降序排列的。


class Solution:
    def removeDuplicates(self, nums):
        n=len(nums)
        k=0
#這邊使用有個陷阱  不使用for迴圈搭配remove
#因為當第一個元素被刪除後,原本該元素後面的元素會被自動往前移動一位,
#所以下一輪的迴圈中,原本在第三個位置的元素會被當成第二個元素被處理。
#因此使用for迴圈遍歷nums時只會對每個元素執行一次,即使重複也只會刪除一次

        while k < n:
#檢查第1,檢查第1、第2如果重複就pop取出,不重複就在繼續檢查第1、2、3項
            if nums[k] in nums[:k] :
               
                nums.pop(k)
                n -= 1
                nums.append("_")
                print(f'看看插在第幾項{nums.index("_")}')
                print(f"現在檢查到第幾項={k},n還剩多少{n}")
            else:
                k += 1

        return k,nums
tes=Solution()
print(tes.removeDuplicates([0,0,1,1,1,2,2,3,3,4]))
-------------
終端機
PS C:\Users\K\OneDrive\桌面\AUTOKI> & C:/Users/K/AppData/Local/Programs/Python/Python311/python.exe c:/Users/K/OneDrive/桌面/AUTOKI/tes.py ython/Python311/python.exe c:/Users/K/OneDrive/桌面/AUTOKI/tes.py 現在檢查到第幾項=1,n還剩多少9 現在檢查到第幾項=2,n還剩多少8 現在檢查到第幾項=2,n還剩多少7 現在檢查到第幾項=3,n還剩多少6 現在檢查到第幾項=4,n還剩多少5 (5, [0, 1, 2, 3, 4, '_', '_', '_', '_', '_'])




標籤: