笔试算法中常用API
Map java.util.HashMap<k,v>
// 创建集合对象
Map<Integer,String> map = new HashMap<Integer,String>();
// 添加、获取、删除 元素
map.put(1,"hello");
map.get(1);
map.remove(1);
// 获取键集
Set<Integer> keySet = map.keySet();
for (Integer integer : keySet) {
System.out.println(integer);
}
// 获取所有entry对象并遍历
Set<Map.Entry<Integer,String>> entrySet = map.entrySet();
for(Map.Entry<Integer.String> entry : entrySet){
entry.getKey();
entry.getValue();
}
int hashcode = map.hashcode();
// 返回集合中的映射数
int count = map.size();
LinkedList java.util.LinkedList
// 双端队列适用于队列和栈
Deque<Integer> deque= new LinkedList<Integer>;
deque.offerFirst(3);
deque.offerLast(4);
deque.pollFirst();
deque.pollLast();
deque.peekFirst();
deque.peekLast();
二分查找
// 二分查找的最基础和最基本的形式
int binarySearch(int[] nums, int target){
if(nums == null || nums.length == 0)
return -1;
int left = 0, right = nums.length - 1;
while(left <= right){
// Prevent (left + right) overflow
int mid = left + (right - left) / 2;
if(nums[mid] == target){ return mid; }
else if(nums[mid] < target) { left = mid + 1; }
else { right = mid - 1; }
}
// End Condition: left > right
return -1;
}
// 二分查找的高级模板,它用于查找需要访问数组中当前索引及其直接右邻居索引的元素或条件
int binarySearch(int[] nums, int target){
if(nums == null || nums.length == 0)
return -1;
int left = 0, right = nums.length;
while(left < right){
// Prevent (left + right) overflow
int mid = left + (right - left) / 2;
if(nums[mid] == target){ return mid; }
else if(nums[mid] < target) { left = mid + 1; }
else { right = mid; }
}
// Post-processing:
// End Condition: left == right
if(left != nums.length && nums[left] == target) return left;
return -1;
}
回溯法(深度优先遍历)
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
dfs(nums, path, res);
return res;
}
private void dfs(int[] nums,
List<Integer> path,
List<List<Integer>> res) {
if (path.size() == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if(path.contains(nums[i]){
path.add(nums[i]);
dfs(nums, path, res);
// 回溯的过程中,将当前的节点从 path 中删除
path.remove(path.size() - 1);
}
continue;
}
}
广度优先遍历
public List<List<Integer>> levelOrderBottom(TreeNode root) {
LinkedList<List<Integer>> levelOrder = new LinkedList<List<Integer>>();
if (root == null)
return levelOrder;
// 定义一个队列,用来存放节点
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
ArrayList<Integer> level = new ArrayList<Integer>();
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
level.add(node.val);
TreeNode left = node.left,right = node.right;
if (left != null)
queue.offer(left);
if (right != null)
queue.offer(right);
}
levelOrder.add(0,level);
}
return levelOrder;
}
动态规划
做题步骤
- 确定状态(按照最后一步以及子问题)
- 转移方程
- 初始条件(转移方程算不出来的)和边界情况(数组越界)
- 计算顺序
// 硬币组合,求最值
public int coinChange(int[] A, int M){
// 确定状态
int[] f = new int[M + 1];
int n = A.length;
// 初始条件
f[0] = 0;
int i,j;
// 有序
for(i = 1; i <= M; ++i){
f[i] = Integer.MAX_VALUE;
for (j = 0; j < n; ++j){
// 状态转移方程
if (i >= A[j] && f[i - A[j]] != Integer.MAX_VALUE)
f[i] = Math.min(f[i - A[j]] + 1, f[i]);
}
}
if (f[M] == Integer.MAX_VALUE)
f[M] = -1;
return f[M];
}
// 走右下角格子,计数
public int uniquePaths(int m, int n){
// 确定状态,到达该点的方式数
int[][] f = new int[m][n];
int i, j;
for (i = 0; i < m; ++i){ // 计算顺序
for (j = 0; j < n; ++j){
if (i == 0 || j == 0)
f[i][j] = 1; // 初始条件和边界情况
else
f[i][j] = f[i - 1][j] + f[i][j - 1]; //转移方程
}
}
return f[m - 1][n - 1];
}
// 青蛙跳石头,求存在性
public boolean canJump(int[] A){
int n = A.length;
// 确定状态
boolean[] f = new boolean[n];
f[0] = true; // 初始条件
// 计算顺序
for (int j = 1; j < n; ++j){
f[j] = false;
for (int i = 0; i < j; ++i){
// 状态转移方程
if (f[i] && i + A[i] >= j){
f[j] = true;
break;
}
}
}
return f[n - 1];
}
快速排序
public void Quicksort(int array[], int L, int R){
if (L > R)
return;
int left = L, right = R;
int pivot = array[left];
while(left < right){
while(left < right && array[right] >= pivot){
right --;
}
array[left] = array[right];
while(left < right && array[left] <= pivot){
left++;
}
array[right] = array[left];
}
array[left] = pivot;
Quicksort(array,L,right - 1);
Quicksort(array,right + 1, R);
}