<jupyter_start><jupyter_text>Day 1   66. 加一<jupyter_code># python 取巧解法
class Solution:
    def plusOne(self, digits):
        s = ''
        for i in digits:
            s += str(i)
        num = int(s)
        num += 1
        s = str(num)
        ans = [int(i) for i in s]
        return ans   # 需要用 return 
        
m = Solution()
digits = [1,2,3]
m.plusOne(digits)
# 正常解法
class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        for i in range(len(digits)-1, -1, -1):
            if digits[i] is not 9:
                digits[i] += 1
                return digits
            else:
                digits[i] = 0
                # 防止 9999 这种情况
                if digits[0] is 0:
                    digits.insert(0, 1)
                    return digits<jupyter_output><empty_output><jupyter_text>724. 寻找数组的中心下标<jupyter_code>class Solution:
    def pivotIndex(self, nums: List[int]) -> int:
        lst_l = [0] * len(nums)
        for i in range( len(nums)-1 ):
            lst_l[i+1] = lst_l[i] + nums[i]
        lst_r = [0] * len(nums)
        for i in range(len(nums)-1, 0, -1):
            lst_r[i-1] = lst_r[i] + nums[i]
        for i in range(len(nums)):
            if lst_l[i] == lst_r[i]:
                return i 
        return -1
# 优化后解法
class Solution:
    def pivotIndex(self, nums: List[int]) -> int:
        total = sum(nums)
        sum_l = 0
        for i in range( len(nums) ):
            # 左边和等于右边和 sum_l = total -nums[i] - sum_l
            if 2 * sum_l + nums[i] == total:
                return i 
            sum_l += nums[i]
        return -1<jupyter_output><empty_output><jupyter_text>189. 轮转数组<jupyter_code># 方法1
class Solution:
    def rotate(self, nums, k):
        """
        Do not return anything, modify nums in-place instead.
        """
        for i in range(k):
            x = nums.pop()
            nums.insert(0,x)
    
m = Solution()
nums = [1,2,3,4,5,6,7]
k = 3
m.rotate(nums, k)
nums
nums = [1,2,3,4,5,6,7]
k = 3

# 方法2
n=len(nums)
k=k%n
def swap(l,r):
    while(l<r):
        nums[l],nums[r]=nums[r],nums[l]
        l=l+1
        r=r-1
swap(0,n-k-1)
print(nums)
swap(n-k,n-1)
print(nums)
swap(0,n-1)
print(nums)
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n=len(nums)
        k=k%n
        def swap(l,r):
            while(l<r):
                nums[l],nums[r]=nums[r],nums[l]
                l=l+1
                r=r-1
        swap(0,n-k-1)
        swap(n-k,n-1)
        swap(0,n-1)
        
# 方法3 
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n=len(nums)
        k=k%n  #这里很关键，不然会出错 如 nums = [1,2] k=3
        nums[:] = nums[-k:] + nums[:-k]<jupyter_output><empty_output><jupyter_text>Day 2  48. 旋转图像<jupyter_code>matrix = [[1,2,3],[4,5,6],[7,8,9]]

for item in zip(*matrix):
    matrix[]   print(item[::-1])
matrix = [[1,2,3],[4,5,6],[7,8,9]]

matrix = [ list(x[::-1]) for x in zip(*matrix)]
matrix
matrix = [[1,2,3],[4,5,6],[7,8,9]]

matrix[::-1]
# 方法1
class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        matrix[:] = [ list(x[::-1]) for x in zip(*matrix)]
# 方法1 变种
class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        matrix[:] = zip(*matrix[::-1])<jupyter_output><empty_output><jupyter_text>54. 螺旋矩阵<jupyter_code>matrix = [[1,2,3],[4,5,6],[7,8,9]]


m = len(matrix)
n = len(matrix[0])
row_strat = 0
row_end = m - 1 
col_start = 0
col_end = n - 1 

lst = []
while row_strat < row_end  and col_start < col_end:
    for i in range(col_start, col_end):
        lst.append(matrix[row_strat][i])
    for i in range(row_strat, row_end):
        lst.append( matrix[i][col_end] )
    for i in range(col_end, col_start, -1):
        lst.append( matrix[row_end][i]  )
    for i in range(row_end, row_strat, -1):
        lst.append( matrix[i][col_start]  )
    row_strat += 1
    row_end -= 1
    col_start += 1
    col_end -= 1
    # 只有一行数据 
    if row_strat == row_end:
        for i in range(col_start, col_end+1):
            lst.append( matrix[row_strat][i]  )
    # 只有一列数据
    elif col_start == col_end:
        for i in range(row_strat, row_end+1):
            lst.append( matrix[i][col_start]  )
            
print(lst)
# 通过答案
class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        m = len(matrix)
        n = len(matrix[0])
        row_strat = 0
        row_end = m - 1 
        col_start = 0
        col_end = n - 1 

        lst = []
        while row_strat < row_end  and col_start < col_end:
            for i in range(col_start, col_end):
                lst.append(matrix[row_strat][i])
            for i in range(row_strat, row_end):
                lst.append( matrix[i][col_end] )
            for i in range(col_end, col_start, -1):
                lst.append( matrix[row_end][i]  )
            for i in range(row_end, row_strat, -1):
                lst.append( matrix[i][col_start]  )
            row_strat += 1
            row_end -= 1
            col_start += 1
            col_end -= 1
            # 只有一行数据 
        if row_strat == row_end:
            for i in range(col_start, col_end+1):
                lst.append( matrix[row_strat][i]  )
        # 只有一列数据
        elif col_start == col_end:
            for i in range(row_strat, row_end+1):
                lst.append( matrix[i][col_start]  )
                    
        return  lst<jupyter_output><empty_output><jupyter_text>498. 对角线遍历<jupyter_code>"""
输入：mat = [[1,2,3],[4,5,6],[7,8,9]]
输出：[1,2,4,7,5,3,6,8,9]
"""
# 很好的想法！
class Solution:
    def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
        if len(mat) == 0:
            return []
        M,N, ans = len(mat), len(mat[0]), []
        for cur_line in  range(M+N-1):
            # 发现 行 起点，终点
            row_begin = 0 if cur_line+1 <= N else cur_line+1-N 
            row_end = M-1 if cur_line+1 >= M else cur_line
            # 奇数行，往左下走
            if cur_line % 2==1:
                for i in range(row_begin, row_end+1):
                    ans.append( mat[i][cur_line-i] )
            else:
                for i in range(row_end,row_begin-1, -1):
                    ans.append( mat[i][cur_line-i] )
        return ans<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14970333181195802, 'fraction_numerical': 0.027993305948577515, 'mean_word_length': 1.859504132231405, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '483876', 'license': 'MIT', 'n_tokens_mistral': 2705, 'n_tokens_neox': 2612, 'n_words': 555, 'path': 'MyCode/Task2.ipynb', 'repo_name': 'pengfei-chen/LeetCode-Py', 'size': '6573'}