<filename>Week 1/id_455/LeetCode_189_455.py
#
# @lc app=leetcode.cn id=189 lang=python3
#
# [189] 旋转数组
#

# @lc code=start

# 方法一
'''
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        k %= n
        for _ in range(k):
            nums.insert(0, nums.pop())
'''
#方法二 
'''
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        k %= n
        nums[:] = nums[-k:] + nums[:-k]
'''
#方法三 翻转
'''
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        k %= n
        nums[:] = nums[::-1]
        nums[:k] = nums[:k][::-1]
        nums[k:] = nums[k:][::-1]
'''
#方法四  环状替换
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        k %= n
        count = 0
        start = 0

        while count < n:
            previous = nums[start]
            curr = start
            while True:
                nex = (curr + k) % n
                previous ,nums[nex] = nums[nex] ,previous
                curr = nex
                count += 1
                if curr == start:
                    break
            start += 1


       
                
        


# @lc code=end


<filename>Week 1/id_455/LeetCode_189_455.py
#
# @lc app=leetcode.cn id=189 lang=python3
#
# [189] 旋转数组
#

# @lc code=start

# 方法一
'''
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        k %= n
        for _ in range(k):
            nums.insert(0, nums.pop())
'''
#方法二 
'''
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        k %= n
        nums[:] = nums[-k:] + nums[:-k]
'''
#方法三 翻转
'''
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        k %= n
        nums[:] = nums[::-1]
        nums[:k] = nums[:k][::-1]
        nums[k:] = nums[k:][::-1]
'''
#方法四  环状替换
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        k %= n
        count = 0
        start = 0

        while count < n:
            previous = nums[start]
            curr = start
            while True:
                nex = (curr + k) % n
                previous ,nums[nex] = nums[nex] ,previous
                curr = nex
                count += 1
                if curr == start:
                    break
            start += 1


       
                
        


# @lc code=end


# # @lc app=leetcode.cn id=189 lang=python3 # # [189] 旋转数组 # # @lc code=start # 方法一 class Solution:     def rotate(self, nums: List[int], k: int) -> None:         """         Do not return anything, modify nums in-place instead.         """         n = len(nums)         k %= n         for _ in range(k):             nums.insert(0, nums.pop()) #方法二 class Solution:     def rotate(self, nums: List[int], k: int) -> None:         """         Do not return anything, modify nums in-place instead.         """         n = len(nums)         k %= n         nums[:] = nums[-k:] + nums[:-k] #方法三 翻转 class Solution:     def rotate(self, nums: List[int], k: int) -> None:         """         Do not return anything, modify nums in-place instead.         """         n = len(nums)         k %= n         nums[:] = nums[::-1]         nums[:k] = nums[:k][::-1]         nums[k:] = nums[k:][::-1] #方法四  环状替换 Do not return anything, modify nums in-place instead. # @lc code=end