算法题备忘账单

328 阅读3分钟

一、输入输出

1. Python

import sys 
for line in sys.stdin:
    a = line.split()
    print(int(a[0]) + int(a[1]))

2. JavaScript

while(line=readline()){
    var lines = line.split(' ');
    var a = parseInt(lines[0]);
    var b = parseInt(lines[1]);
    print(a+b);
}

二、数据结构

1. 链表

function ListNode(x){
    this.val = x;
    this.next = null;
}

2. 二叉树

function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
}

2.1 二叉树遍历

中序遍历:左跟右(3-5-6-7-8-9-10-11-12-13-14-15-18-20-25)

先序遍历:根左右(11-7-5-3-6-9-8-10-15-13-12-14-20-18-25)

后序遍历:左右根(3-6-5-8-10-9-7-12-14-13-18-25-20-15-11)

2.2 二叉搜索树

二叉搜索树(BST)是二叉树的一种,但是它只允许你在左侧节点存储比父节点小的值,在右侧节点存储比父节点大或者等于父节点的值。

2.3 平衡二叉树

平衡二叉树是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

3. 哈希表

三、常用API

1. Python

1 数字

import math
#向上取整
print(math.ceil(2.1))

#向下取整
print(math.floor(2.9))

#四舍五入
def round(n):
  return math.ceil(n) if (n - math.floor(n)) >= 0.5 else math.floor(n)
print(round(2.5))

2 字符串

api 作用
len() 计算长度
capitalize(),lower(), upper() 大小写
center(s), ljust(s), rjust(s) 补全
count(s) 出现次数
find(s),lfind(s),rfind(s) 查找位置
split() 分割成列表
replace(old, new [, max]) 替换字符串
strip(), lstrip(), rstrip() 去空格
#center(s), ljust(s), rjust(s)
'aaa'.center('b') #'baaab'

#capitalize(), lower(), upper()
'AAA'.lower() #'aaa'

#count()
'aaa'.count('a') #3

#find()
'abc'.find('a') #0

#split()
'a b c'.split() #['a','b','c']

3 列表

api 作用
len() 计算长度
append(x),insert(I,x),pop(i),remove(x) 增删
sort(reverse=False) 排序
reverse() 反转列表
max(),min() 最大最小
count() 计数
extend(l) 列表合并
#append(x), insert(i,x), pop(i), remove(x)
[1,2,3].append(4) #[1,2,3,4]
[1,2,3].insert(1,1.5) #[1, 1.5, 2, 3]
[1,2,3].pop(1) #[1,3]
['a','b','c'].remove('b') #['a', 'c']

#sort(reverse=False)
[1,2,3].sort(reverse=True) #[3,2,1]

#reverse()
[1,2,3].reverse() #[3,2,1]

#index
[1,2,3].index(2) #1

#count
[1,2,3].count(2) #1

#join
''.join(['a','b','c']) #abc

高阶函数

def f(x):
    return x * x
map(f, [1, 2, 3]) #[1,4,9]

from functools import reduce
def add(x, y):
    return x + y
reduce(add, [1, 3, 5]) #9

def is_odd(n):
    return n % 2 == 1
filter(is_odd, [1, 2, 4]) // [1]

4 字典

api 作用
dict.keys() 取键
dict.values() 取值

2. JavaScript

1 Array 数组常用方法

let a = [1, 2, 3]
a.splice(1, 1, 4, 5) // [2]
a // [1, 4, 5, 3]
["a", "b", "c"].slice(1,2)  // ["b"]

["a", "b", "c"].join('-')   // "a-b-c"
["a", "b", "c"].concat("d")   // ["a", "b", "c", "d"]

[3,1,2].sort((a,b) => a-b)  // [1, 2, 3]
["a", "b", "c"].reverse()   // ["c", "b", "a"]

push(), pop(), unshift(), shift()

[3, 10, 18, 20].some(val => val>18)   // true
[3, 10, 18, 20].every(val => val>18)    // false
[3, 10, 18, 20].filter(val => val>18)   // [20]

[1,2,3].forEach(val => val+1)   // undefined
[1,2,3].map(val => val+1)  // [2, 3, 4]

arr.reduce(callback,[initialValue])
[1, 2, 3].reduce(function(prev, cur, index, arr) {
    console.log(prev, cur, index, arr);
    return prev + cur;
},1)  // 7

[1,2,3].indexOf(2)  // 1

2 String 字符串常用方法

"hello".indexOf('h')  // 0
"hello".match("e")  // ["e", index: 1, input: "hello", groups: undefined]   String.match(regexp)
"hello".search("h") //0   String.search(regexp)

"hello".charAt(1)   // "e"
"hello".concat("world")   // "helloworld"
"hello".replace("o","ooo")  // "hellooo"

"abc".slice(1,2)  // "b"
"abc".split("") // ["a", "b", "c"]
"hello".substr(2,3) // "llo"
"hellow".substring(2,3) // "l"

toLowerCase(), toUpperCase()

3 Object

Object.keys({a:1,b:2})  // ["a", "b"]
Object.values({a:1,b:2})  // [1, 2]