Skip to content
A bird sitting on a nest of eggs. GitHub Twitter

algorithm solving tips for python users

Set

class Solution(object):
    def containsDuplicate(self, nums):
        hset = set()
        for idx in nums:
            if idx in hset:
                return True
            else:
                hset.add(idx)

When to use

Basically set is utilized when...

  1. you look for a value in array real quick
  2. you remove duplicated value

Methods

  • add Add element to set.

Convert upper-case to lower-case and vice versa

  • string.lower()
  • string.upper()

Reverse

Let's say you'd like to replace "reverse" with "esrever"

reverse == reverse[::-1]

Check the type of given object

char.isalpha()

Return true if the target is alphabet

char.isdigit()

Return true if the target is num

isalnum()

Return true if the target is alphabet or num

Vocabulary

  • 1-indexed array An array starting from index 1, not 0

Select the last index of an array

stack = ["a", "b", "f"]
print(stack[-1]) # f

Operator

// 2

it divides the number by 2 and rounds down the result to the nearest integer.

Memory

Node

Update the next pointer of a node does not mean you created extra space for that. cur ノードや head ノードは、リンクリストの既存のノードを指し示しているだけで、これら自体は新しいメモリを消費していません。これらは単に既存のノードへの参照(ポインタ)であり、リンクリストの各ノードと共に既に割り当てられているメモリを指しています。