人生第一次写博客,开个好头吧,本意是写给自己复习用的。
Leetcode刷题:https://leetcode.com/problemset/algorithms/
Problem 1(Easy)
Title:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
Solution 1(Brute Force): 先找到第一个数x,再用target-x遍历数组剩下的部分,找不到则报错。
Sub problem: 时间复杂度和空间复杂度http://blog.csdn.net/xiaoxiaopengbo/article/details/51583386
1. 时间频度: 一个算法中的语句执行次数称为语句频度或时间频度。记为T(n)。
2. Ο(1)表示基本语句的执行次数是一个常数,一般来说,只要算法中不存在循环语句,其时间复杂度就是Ο(1)。其中Ο(log2n)、Ο(n)、 Ο(nlog2n)、Ο(n2)和Ο(n3)称为多项式时间,而Ο(2n)和Ο(n!)称为指数时间。计算机科学家普遍认为前者(即多项式时间复杂度的算法)是有效算法,把这类问题称为P(Polynomial,多项式)类问题,而把后者(即指数时间复杂度的算法)称为NP(Non-Deterministic Polynomial, 非确定多项式)问题。
3. 空间复杂度:S(n)定义为该算法所耗费的存储空间
Solution 2:哈希表one-pass(hashmap和hashtable),重难点是遍历Map;补码、反码、原码;
时间复杂度由O(n2)变成O(n)。
Solution 3:哈希表one-pass,一边检查是否有组成两个数的sum达到要求,一边把num的数值放入map
Problem 9(easy)
Title:
Solution 1