Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Given an array, rotate the array to the right by k steps, where k is non-negative.
- class Solution:
- def rotate(self, nums, k):
- """
- :type nums: List[int]
- :type k: int
- :rtype: void Do not return anything, modify nums in-place instead.
- """
- k=k%len(nums)
- nums[:] = nums[-k:] + nums[:-k]
- ----------------------------------------------------------------------------
- Given an array of integers, find if the array contains any duplicates.
- Your function should return true if any value appears at least twice in the array,
- and it should return false if every element is distinct.
- class Solution:
- def containsDuplicate(self, nums):
- """
- :type nums: List[int]
- :rtype: bool
- """
- """"
- nums.sort()
- lastnum = None
- for num in nums:
- if lastnum != num:
- lastnum = num
- else:
- return True
- return False
- """
- return len(nums) != len(set(nums))
- ----------------------------------------------------------------------------
- # Given a non-empty array of integers, every element appears twice except for one. Find that single one.
- class Solution:
- def singleNumber(self, nums):
- """
- :type nums: List[int]
- :rtype: int
- """
- result = 0
- for num in nums:
- result ^= num
- return result
- ----------------------------------------------------------------------------
- # Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
- class Solution:
- def plusOne(self, digits):
- """
- :type digits: List[int]
- :rtype: List[int]
- """
- num = int(''.join(str(x) for x in digits))
- num = str(num + 1)
- result = []
- for digit in num:
- result.append(int(digit))
- return result
- """
- integer = int(''.join(map(str, digits))) + 1
- return list(map(int, str(integer)))
- """
- ----------------------------------------------------------------------------
- # Given an array nums, write a function to move all 0's to the end of it while
- # maintaining the relative order of the non-zero elements.
- class Solution:
- def moveZeroes(self, nums):
- """
- :type nums: List[int]
- :rtype: void Do not return anything, modify nums in-place instead.
- """
- for i in range(nums.count(0)):
- nums.append(nums.pop(nums.index(0)))
- ----------------------------------------------------------------------------
- # Given an array of integers, return indices of the two numbers such that they add up to a specific target
- class Solution:
- def twoSum(self, nums, target):
- """
- :type nums: List[int]
- :type target: int
- :rtype: List[int]
- """
- buff_dict = {}
- for i in range(len(nums)):
- if nums[i] in buff_dict:
- return [buff_dict[nums[i]], i]
- else:
- buff_dict[target - nums[i]] = i
- """
- comp = []
- for i in range(len(nums)):
- if nums[i] in comp:
- return [nums.index(target-nums[i]), i]
- else:
- comp.append(target-nums[i])
- """
- ----------------------------------------------------------------------------
- # You are given an n x n 2D matrix representing an image.
- # Rotate the image by 90 degrees (clockwise).
- class Solution:
- def rotate(self, matrix):
- """
- :type matrix: List[List[int]]
- :rtype: void Do not return anything, modify matrix in-place instead.
- """
- matrix.reverse()
- for i in range(len(matrix)):
- for j in range(i):
- matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
- ----------------------------------------------------------------------------
- # 3 SUM
- # Given an array nums of n integers, are there elements a, b, c in nums such
- # that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
- from collections import Counter
- class Solution:
- def threeSum(self, nums):
- """
- :type nums: List[int]
- :rtype: List[List[int]]
- """
- #Lets get a counter of the elements first
- freq = dict(Counter(nums))
- # Check if we have more than two 0 s for a three 0 sum of 0.
- if 0 in freq and freq[0] > 2:
- r = [
- [0, 0, 0]
- ]
- else :
- r = []
- # Seperate out the negative and non negative numbers in two lists
- neg = sorted(filter(lambda x: x < 0, freq))
- nneg = sorted(filter(lambda x: x >= 0, freq))
- for n in neg:
- for nn in nneg: #Find last number we should look for to get a 3 sum of 0.
- targ = -(n + nn)
- if targ in freq:
- #If the target number is a repeat of either of the first two we must
- # check that it shows up more than once in the list through our freq count.
- if targ in (n, nn) and freq[targ] > 1:
- r.append([n, targ, nn])
- # Make sure we dont get duplicate groups by making sure the target isnt meant to be a middle number.
- elif targ < n:
- r.append([n, targ, nn])
- elif targ > nn:
- r.append([n, targ, nn])
- return r
- ----------------------------------------------------------------------------
- # Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
- class Solution:
- def setZeroes(self, matrix):
- """
- :type matrix: List[List[int]]
- :rtype: void Do not return anything, modify matrix in-place instead.
- """
- r = len(matrix)
- c = len(matrix[0])
- rows = []
- columns = []
- for i in range(r):
- for j in range(c):
- if matrix[i][j]==0:
- rows.append(i)
- columns.append(j)
- for row in rows:
- matrix[row] = [0]*c
- for col in columns:
- for i in range(r):
- matrix[i][col] = 0
- ----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement