for i in range(0,rowLen): for j in range(0,columnLen): if matrix[i][j] == 0: if rowList.count(i) == 0: rowList.append(i) if columnList.count(j) == 0: columnList.append(j) for i in range(0,len(rowList)): matrix[rowList[i]] = [0 for i in range(0,columnLen)] for i in range(0,len(columnList)): columnIndex = columnList[i] for j in range(0,rowLen): matrix[j][columnIndex] = 0 return matrixArr = [[ [0,1,2,0], [3,4,5,2], [1,3,1,5] ],[ [1,1,1], [1,0,1], [1,1,1] ]] s = Solution1() for i in range(0,len(matrixArr)): PrintUtil.print_2d_num_array(matrixArr[i]) print("--------------") s.setZeroes(matrixArr[i]) PrintUtil.print_2d_num_array(matrixArr[i]) print("--------------")
num = 1 for i in range(0, round): # 上边 for j in range(0, n - 1 - 2 * i): matrix[i][i + j] = num num = num + 1 # 右边 for j in range(0, n - 1 - 2 * i): matrix[i + j][n - 1 - i] = num num = num + 1 # 下边 for j in range(0, n - 1 - 2 * i): matrix[n - 1 - i][n - 1 - i - j] = num num = num + 1 # 左边 for j in range(0, n - 1 - 2 * i): matrix[n - 1 - i - j][i] = num num = num + 1 # 如果n为奇数,还需要在最中间放最后一个数字 if n % 2 == 1: matrix[n // 2][n // 2] = num return matrix s = Solution1() nArr = [2, 3, 4] for i in range(0, len(nArr)): matrix = s.generateMatrix(nArr[i]) PrintUtil.print_2d_num_array(matrix)
from src.session1.common.ListBuilder import ListBuilder from src.session1.common.PrintUtil import PrintUtil from src.session1.common.ListNode import ListNode class Solution1: def deleteDuplicates(self, head): if head is None or head.next is None: return head fakeHead = ListNode(0) fakeHead.next = head curr = fakeHead while curr.next is not None: nextNode = curr.next if nextNode.next is not None and nextNode.next.val == nextNode.val: while nextNode.next is not None and nextNode.next.val == nextNode.val: nextNode = nextNode.next curr.next = nextNode.next else: curr = curr.next return fakeHead.next s = Solution1() arrs = [[1, 2, 3, 3, 4, 4, 5], [1, 1, 1, 2, 3]] for i in range(0, len(arrs)): listBuilder = ListBuilder(arrs[i]) PrintUtil.print_list(listBuilder.get_head()) print("----------------------") PrintUtil.print_list(s.deleteDuplicates(listBuilder.get_head())) print("----------------------")
from src.session1.common.ListBuilder import ListBuilder from src.session1.common.PrintUtil import PrintUtil from src.session1.common.ListNode import ListNode import heapq class Solution2: def mergeKLists(self, lists): fakeHead = ListNode(0) curr = fakeHead heap = [] for lst in lists: while lst: heapq.heappush(heap, lst.val) lst = lst.next while heap: curr.next = ListNode(heapq.heappop(heap)) curr = curr.next return fakeHead.next s = Solution2() numsArr = [[1, 4, 5], [1, 3, 4], [2, 6]] lists = [] for nums in numsArr: listBuilder = ListBuilder(nums) lists.append(listBuilder.get_head()) PrintUtil.print_list(s.mergeKLists(lists))
from src.session1.common.ListBuilder import ListBuilder from src.session1.common.PrintUtil import PrintUtil class Solution1: def swapPairs(self, head): if head is None or head.next is None: return head keep = head.next.next second = head.next head.next = self.swapPairs(keep) second.next = head return second s = Solution1() arr = [1, 2, 3, 4, 5] listBuilder = ListBuilder(arr) PrintUtil.print_list(s.swapPairs(listBuilder.get_head()))
def backTracking(self, curr, l1, l2): if l1 is None and l2 is None: curr.next = None elif l1 is None: curr.next = l2 elif l2 is None: curr.next = l1 else: if l1.val <= l2.val: keep = l1.next l1.next = None curr.next = l1 l1 = keep else: keep = l2.next l2.next = None curr.next = l2 l2 = keep curr = curr.next self.backTracking(curr, l1, l2) s = Solution1() nums1 = [1, 2, 4] nums2 = [1, 3, 4] listBuilder1 = ListBuilder(nums1) listBuilder2 = ListBuilder(nums2) listRet = s.mergeTwoLists(listBuilder1.get_head(), listBuilder2.get_head()) PrintUtil.print_list(listRet)
from src.session1.common.ListBuilder import ListBuilder from src.session1.common.ListNode import ListNode from src.session1.common.PrintUtil import PrintUtil class Solution1: def deleteDuplicates(self, head): if (head is None) or (head.next is None): return head map = {} fakeHead = ListNode(0) fakeHead.next = head curr = fakeHead while curr.next is not None: if curr.next.val not in map: map[curr.next.val] = 1 curr = curr.next else: keep = curr.next.next curr.next = keep return fakeHead.next s = Solution1() arr = [1,1] listBuilder = ListBuilder(arr) head = listBuilder.get_head() PrintUtil.print_list(s.deleteDuplicates(head))
from src.session1.common.PrintUtil import PrintUtil class Solution1: def sortColors(self, nums): numsLen = len(nums) zeroPos = 0 twoPos = numsLen - 1 for i in range(0, numsLen): while nums[i] == 2 and i < twoPos: self.swap(nums, i, twoPos) twoPos -= 1 while nums[i] == 0 and i > zeroPos: self.swap(nums, i, zeroPos) zeroPos += 1 def swap(self, nums, posA, posB): temp = nums[posA] nums[posA] = nums[posB] nums[posB] = temp numsArr = [[2, 0, 2, 1, 1, 0], [2, 0, 2, 1, 0, 1, 0, 0, 0, 2, 1, 0, 1]] s = Solution1() for i in range(0, len(numsArr)): s.sortColors(numsArr[i]) PrintUtil.print_num_array(numsArr[i]) print()
from src.session1.common.PrintUtil import PrintUtil class Solution1: def plusOne(self, digits): arrLen = len(digits) ret = [] increase = 1 for i in range(arrLen - 1, -1, -1): num = digits[i] + increase if num >= 10: ret.append(num % 10) increase = 1 else: ret.append(num) increase = 0 if increase == 1: ret.append(increase) ret.reverse() return ret s = Solution1() digitsArr = [[4, 3, 2, 1], [1, 1, 9], [9]] for i in range(0, len(digitsArr)): ret = s.plusOne(digitsArr[i]) PrintUtil.print_num_array(ret) print()
moveStep = listLen - realK - 1 curr = head for i in range(0, moveStep): curr = curr.next keep = curr.next curr.next = None curr = keep while curr.next is not None: curr = curr.next curr.next = head return keep def getListLen(self, head): ret = 1 curr = head if head is None: return 0 while curr.next is not None: curr = curr.next ret = ret + 1 return ret s = Solution1() arr = [1, 2, 3, 4, 5] kArr = [1, 2, 3, 4, 5, 6] for i in range(0, len(kArr)): listBuilder = ListBuilder(arr) PrintUtil.print_list(s.rotateRight(listBuilder.get_head(), kArr[i])) print("----------------")
# 两个等长链表相加 def add_same_len(self,l1,l2): increase = 0 fakeHead = ListNode(0) curr = fakeHead while (l1 is not None) and (l2 is not None): sum = l1.val + l2.val + increase if sum >= 10: increase = sum // 10 value = sum % 10 else: value = sum increase = 0 curr.next = ListNode(value) curr = curr.next l1 = l1.next l2 = l2.next if increase == 1: curr.next = ListNode(1) return fakeHead.next s = Solution1() nums1 = [9,1,6] nums2 = [0] listBuilder1 = ListBuilder(nums1) l1 = listBuilder1.get_head() listBuilder2 = ListBuilder(nums2) l2 = listBuilder2.get_head() PrintUtil.print_list(s.addTwoNumbers(l1,l2))