Leetcode 分享筆記

290. Word Pattern

題目

leetcode
b

python


class Solution(object):
    def wordPattern(self, pattern, s):
        """
        :type pattern: str
        :type s: str
        :rtype: bool
        """
        # 切割文字成陣列
        list_pattern = [str(word) for word in pattern]
        list_s = s.split(' ')
        
        # 將切割好的文字陣列轉成數字陣列,不同的文字對應道不同的數字,用Dict表示
        i = 0
        temp_pattern = {}
        int_list_pattern = []
        for s in list_pattern:
            if s not in temp_pattern:
                temp_pattern[s] = i
                int_list_pattern.append(temp_pattern[s])
                i = i + 1
            else:
                int_list_pattern.append(temp_pattern[s])

        # 將切割好的文字陣列轉成數字陣列,不同的文字對應道不同的數字,用Dict表示
        j = 0
        temp_s = {}
        int_list_a = []
        for y in list_s:
            if y not in temp_s:
                temp_s[y] = j
                int_list_a.append(temp_s[y])
                j = j + 1
            else:
                int_list_a.append(temp_s[y])
        
        # 比對兩個數字陣列是否順序一致,如不一致回傳False,反之回傳True
        if(int_list_pattern == int_list_a):
            return True
        else:
            return False
class Solution(object):
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # 一個比對的int,一個陣列放最大值的數值
        total = 0
        ans = 0
        j = 0
        while j >= 0:
            for i in range(j, len(nums)):
                total = total + nums[j]
                if total > ans:
                    ans = total
            j = j + 1
        return ans