四大数据结构:栈、队列、set、map
线性数据结构:数组 栈、队列 堆 链表
树形结构:数、二叉树 二叉搜索树 哈希表 并查集 字母树 布隆母过滤器 LRUcache
经典的编程、树的遍历、贪心算法、回溯和递归、深度优先广度优先、分治算法、动态规划算法、二分查找、图
时间和空间复杂度
5.理论讲解—数组和链表
数组
内容中连续的一端存储区域
内存管理器可以实现访问任何一个下标的元素,都是O(1)
查询元素:O(1)
插入元素:平均O(n)
删除元素:平均O(n)
要想改善数组这样的长时间,链表应运而生
链表
分为单链表和双链表
适合于:
1.插入和删除操作非常多
2.你不知道有多少个元素在
本质上就是一个一个的元素,用指针链接起来
查找元素:O(n)
插入元素:O(1)
删除元素:O(1)
6.面试题—反转一个单链表
http://leetcode.com/problems/reverse-linked-list/
206. Reverse Linked List
Easy
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
http://leetcode.com/problems/swap-nodes-in-pairs
24. Swap Nodes in Pairs
Medium
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list’s nodes, only nodes itself may be changed.
Example:
Given 1->2->3->4, you should return the list as 2->1->4->3.
http://leetcode.com/problems/linked-list-cycle
141. Linked List Cycle
Easy
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
Follow up:
Can you solve it using O(1) (i.e. constant) memory?
方法一:硬做
方法二:set、判重O(n)
方法三:快慢指针 快指针走两步、慢指针走一步,快慢指针相遇O(n)