Type something to search...

Python list/dict functions for leetcode

List

  • len(array) : length of array
    • list.index(obj)
  • enumerate(array) : adds counter at the beginning.
    • for count, item in enumerate(['x','y','z']) # 0x 1y 2z
  • range(0,len(mylist)1,2)
    • range(0,3), is 0,1,2
    • for i in reversed(range(len(A)))
  • nums.append(val), nums.remove(val), nums.reverse()
    • nums.extend(nums2)
    • list.insert(index, obj)
    • list.pop() default pop index is -1, remove the last one
  • nums[:]
    • copy the value within function: b[:]=a
      • .copy is equals to b[:]
      • temp = self.original[:]
      • copy.deepcopy could copy nested list
      • import copy , copy.deepcopy(a)
  • min(a,b)
  • nums.sort()
    • list.sort() change in place
    • sorted() return a new sorted list, leaving original unaffected
      • a.sort(key=lambda: x: str(x), reverse = False)
  • zip(alist,blist) : return a zip object which is an iterator of tuples, paired together. Lengths depends on the shorter input list.
    • usually use tuple(), list(), set() on result.
    • list(zip(['x','y','z'], [3,4,5])) = [('x', 3), ('y', 4), ('z', 5)];
    • letter, number = zip(*result_list) # unzip
  • matrix
    • test = [[0 for i in range(m)] for j in range(n)]
      • watch out , 32, mn, then the corner is test[n-1][m-1]
      • lee62
    • make one: dp = [[0] \* n for \_ in range(n)]
    • transpose a two dimension list
      • Trans = [[row[i] for row in board] for i in range(len(board[0]))]

dict

  • for k, v in dic.items()
    • dict = {'a': 1, 'b': 2, 'b': '3'}
    • dic1 == dic2
    • dic.values()
  • d = dict(collections.Counter(s)) : s 3 times, a 1 time
    • c.most_common(3), according to frequency, first 3 value,count 's list
    • c.items() , a list with value,count
    • c.elements: ['a', 'a', 'a', 'a', 'b', 'b']
  • collections.defaultdict(int) # default 0
    • senti_dic = collections.defaultdict(lambda: 0)
    • defaultdict(lambda: 'Vanilla')
    • dict(collections.Counter(nums1)): key is item in num1, value is the count.

e.g.1 with zip, enumerate

134 !(2019-05-28) Gas station 2 #array #2lee

There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations. Return the starting gas station’s index if you can travel around the circuit once in the clockwise direction, otherwise return -1.

Note: If there exists a solution, it is guaranteed to be unique. Both input arrays are non-empty and have the same length. Each element in the input arrays is a non-negative integer.

Example 1:

Input: gas = [1,2,3,4,5] cost = [3,4,5,1,2] Output: 3

Explanation: Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 Travel to station 4. Your tank = 4 - 1 + 5 = 8 Travel to station 0. Your tank = 8 - 2 + 1 = 7 Travel to station 1. Your tank = 7 - 3 + 2 = 6 Travel to station 2. Your tank = 6 - 4 + 3 = 5 Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3. Therefore, return 3 as the starting index.

Code:

 def canCompleteCircuit2(self, gas: List[int], cost: List[int]) -> int:
     tmp = [x - y for x, y in zip(gas, cost)]
     print(tmp+tmp)
     cur = 0
     ans = 0
     for i, val in enumerate(tmp + tmp):
         cur += val
         if cur < 0:
             ans = i + 1
             if ans >= len(gas):
                 return -1
             cur = 0
     return ans

e.g.2 with dict(collection.Counter(array))

Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Example 1: Input: [2,2,1] Output: 1

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        dic = dict(collections.Counter(nums))
        for i in dic:
            if dic[i] == 1:
                return i

Related Posts

Leetcode: Array questions with Python

134.Gas StationThere are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to trave

read more

leetcode questions: Backtracking

Template def backtrack(candidate): if find_solution(candidate): output(candidate) return # iterate all possible candidates. for next_candidate in list_of_candid

read more

leetcode questions: BFS

BFS Template /** * Return the length of the shortest path between root and target node. */ int BFS(Node root, Node target) { Queue<Node> queue; // store all nodes which are waiting

read more

leetcode questions: Binary Search

bisect.bisect_left(a, x) https://dynalist.io/d/RWIGNj7DLlzkBed-3ZqhuBg_#z=cbr2Mkrig9KhE6Lxfwhm31IS O(log n) greater than or equal to the targeted value. If all elements are less than x, return `

read more

leetcode questions: Bit

common tricks x & (-x) to isolate/get the rightmost 1-bit This operation reverts all bits of x except the rightmost 1-bit. Hence, x and -x have just one bit in common - the rightmost 1-bit

read more

leetcode questions: DFS

DFS Templates Template 1: /* * Return true if there is a path from cur to target. */ boolean DFS(Node cur, Node target, Set<Node> visited) { return true if cur is target; for (

read more

leetcode questions: dynamic programming

Intro Good video: Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges - YouTube DP is a style of coding where you store t

read more

leetcode: mini spanning tree, single src shortest path, topological

Minimum spanning tree A minimum spanning tree is a spanning tree with the minimum possible total edge weight in a “weighted undirected graph”. [Min Cost to Connect All Points - LeetCode](https://

read more

Java frequently used data structures and methods for leetcode

Array Common declarations: int[] a = new int[100]; int[] b = {4, 1, 5}; int[] c = new int[] {1, 2, 3};Two-dimensional arrays: int[][] grid = {{1, 2}, {3, 4}};int[][] jag

read more

Leetcode: LinkedList in Python

create a dummy nodeMerge two linked lists.# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Nonecla

read more

Python String functions for leetcode

print(f'best RF score: {grid.best_score_:.3f}') str.index(sub[,start[,end]]) str.strip() remove spaces at the beginning and the end strip(str-not-want)lower() `print(str.upp

read more

leetcode: Trie

Basic A Trie is a special form of a Nary tree. Typically, a trie is used to store strings. Each Trie node represents a string (a prefix). Each node might have several children nodes while the pat

read more