剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
作者:互联网
pre, nex = 0, 0 while pre < len(nums): if nums[pre] % 2 == 1: nums[pre], nums[nex] = nums[nex], nums[pre] nex += 1 pre += 1 return nums ''' l, r = 0, len(nums) - 1 while l < r: while l < r and nums[l] % 2 == 1: l += 1 while l < r and nums[r] % 2 == 0: r -= 1 nums[l], nums[r] = nums[r], nums[l] return nums l, r = 0, len(nums) - 1 while l < r: if nums[l] % 2 == 1: l += 1 continue elif nums[r] % 2 == 0: r -= 1 continue nums[l], nums[r] = nums[r], nums[l] return nums
标签:pre,return,21,nums,Offer,len,偶数,while,nex 来源: https://www.cnblogs.com/FANKIKI/p/15072214.html