code安排电影院座位
华为校招主管面
原题链接:https://leetcode.cn/problems/cinema-seat-allocation/description/
12345678910111213141516171819202122232425262728293031323334353637class Solution { private int left = 0b11110000; private int middle = 0b11000011; private int right = 0b00001111; public int maxNumberOfFamilies(int n, int[][] reservedSeats) { HashMap<Integer,Integer>hash = new HashMap<>(); for(int i = 0; i < reservedSeats.length ; i++){ int row = reserved ...
算法模板-动态规划
序列DPleetcode 674 最长连续递增序列原题链接:https://leetcode.cn/problems/longest-continuous-increasing-subsequence/description/
12345678910111213141516171819202122/** f[i] 以i结尾 长度最长的连续递增子序列 本题要求的是 最长连续递增子序列 连续 说明 f[i] 的状态只能由f[i - 1]转移而来 */class Solution { public int findLengthOfLCIS(int[] nums) { int n = nums.length; int [] f = new int [n]; Arrays.fill(f,1); int ans = 1; for(int i = 1 ; i < n ; i++){ if(nums[i] > nums[i -1] ) ...
code最小路径和
leetcode 64 最小路径和
原题链接https://leetcode.cn/problems/minimum-path-sum/
123456789101112131415161718192021222324252627class Solution { public int minPathSum(int[][] grid) { int n = grid.length; int m = grid[0].length; int [][] dp = new int [n][m]; dp[0][0] = grid[0][0]; for(int i = 0; i < n ; i++){ for(int j = 0 ; j < m ; j++){ if(i == 0 && j == 0){ continue; } ...
code岛屿数量
原题链接:https://leetcode.cn/problems/number-of-islands/
法1: BFS1234567891011121314151617181920212223242526272829303132333435363738394041class Solution { public static int [] dx = new int[]{0,-1,0,1}; public static int [] dy = new int[]{-1,0,1,0}; public static int n,m; public int numIslands(char[][] grid) { n = grid.length; m = grid[0].length; int ans = 0; for (int i = 0 ; i < n ; i++){ for (int j = 0; j < m ...
code二叉树的序列化与反序列化
原题链接:https://leetcode.cn/problems/serialize-and-deserialize-binary-tree/
法1 dfs写法1 重构时使用cur进行标记 会快一些
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Codec { // Encodes a tree to a single string. /* * 前序遍历 序列化编译为字符串 * ...
java集合
集合基本使用集合与数组
ArrayListArrayList成员方法
代码示例
12345678910111213141516171819202122232425262728293031323334353637383940414243import java.util.ArrayList;public class Main { public static void main(String[] args) { // 1. 创建集合的对象 // 泛型 限定集合中存储数据的类型 // JDK7 之前的写法// ArrayList<String> list = new ArrayList<String>(); // JDK7 后的写法 // 此时我们创建的是Arraylist的对象 而对象ArrayList是java已经写好的一个类 // 这个类在底层做了一些处理 // 打印对象不是地址值 而是集合中存储数据内容 // 在 ...
算法模板BFS
基础知识:常见的BFS用来解决什么问题?(1) 简单图(有向无向皆可)的最短路径长度,注意是长度而不是具体的路径(2)拓扑排序 (3) 遍历一个图(或者树)BFS基本模板(需要记录层数或者不需要记录层数)多数情况下时间复杂度空间复杂度都是O(N+M),N为节点个数,M为边的个数
面试推荐题单https://zhuanlan.zhihu.com/p/349940945
基于树的BFS:不需要专门一个set来记录访问过的节点leetcode 102 二叉树的层序遍历原题链接:https://leetcode.cn/problems/binary-tree-level-order-traversal/
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * ...
leetcode318最长长度乘积
原题链接:https://leetcode.cn/problems/maximum-product-of-word-lengths/description/
解法1: 简单位运算模拟12345678910111213141516171819202122232425262728class Solution { public int maxProduct(String[] words) { HashMap<Integer,Integer> hash = new HashMap<>(); for(var ss : words){ int t = 0; for(int i = 0 ; i < ss.length() ; i++){ int u = ss.charAt(i) - 'a'; t |= (1 << u); } ...
code通过删除字母匹配到字典序里最长单词(高频华为出现多次!!!)
华为一面\华为主管面 手撕代码题目
原题链接:https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting/description/
1234567891011121314151617181920212223242526272829303132333435363738394041/** 贪心 + 双指针 删除某些字母得到 = 该单词为字符串s的子序列 且 是有序的 按照长度排序 那么最先匹配完成的就是就是符合答案的 题目又要求字典序最小 那就再按照字典序进行排序 */class Solution { public String findLongestWord(String s, List<String> dictionary) { /* * compareTo 相等返回 0 * 小于返回 -1 * 大于返回 1 * */ Collections ...
code最长上升子序列
字节校招面试 手撕代码题
原题链接https://www.acwing.com/problem/content/897/
1234567891011121314151617181920212223242526272829303132333435363738import java.util.*;/** 动态规划 f[i] 表示 结尾为i 数值严格单调递增的子序列的最长度值为f[i]* f[i] = max(f[j] + 1, f[i])* */public class Main { public static final int N = 1010; public static void main(String[] args) { int [] q = new int[N]; int n = 0; Scanner sc = new Scanner(System.in); n = sc.nextInt(); for(int i = 0 ; i < n ; i++){ ...