- 🛒收集各类大厂校招、社招算法题,欢迎关注我【思考的陈】❤
- 📖本文含题目链接、面试来源、通关代码
- 🔥如果感觉文章还不错的话,请👍点赞关注支持一下
- 📚以【面试官面试】的形式来分享技术系列专栏
- 《Redis系列》
- 《MySQL系列》
- 《Kafka系列》
- 《ZooKeeper系列》
- 《业务设计系列》
字节跳动·算法题
-
class Solution { public void rotate(int[] nums, int k) { int[] ans = Arrays.copyOfRange(nums, 0, nums.length); for (int i = 0; i < nums.length; i++) { nums[(i + k) % nums.length] = ans[i]; } } }
-
class Solution { public int climbStairs(int n) { int a = 1, b = 2; if (n <= 2) return n; int c = 0; for (int i = 0; i <= n - 3; i++) { c = a + b; a = b; b = c; } return c; } }
-
class Solution { int[] nums; public int findKthLargest(int[] nums, int k) { this.nums = nums; sort(0, nums.length - 1); return nums[k - 1]; } private void sort(int l, int r) { if (l >= r) return; int luckyIndex = new Random().nextInt(r - l + 1) + l; swap(luckyIndex, r); int curIndex = l; for (int i = l; i < r; i++) { if (nums[i] > nums[r]) swap(curIndex++, i); } swap(curIndex, r); sort(l, curIndex - 1); sort(curIndex + 1, r); } private void swap(int a, int b) { int temp = nums[a]; nums[a] = nums[b]; nums[b] = temp; } }
阿里巴巴·算法题
-
class Solution { ListNode ret = null; public ListNode reverseList(ListNode head) { if (head == null) return head; dfs(null, head); return ret; } private void dfs(ListNode pre, ListNode cur) { if (cur == null) { ret = pre; return; } ListNode next = cur.next; cur.next = pre; dfs(cur, next); } }
快手·算法题
-
class Solution { TreeNode ret = null; public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { haveRoot(root, p, q); return ret; } private boolean haveRoot(TreeNode root, TreeNode p, TreeNode q) { if (root == null) return false; boolean l = haveRoot(root.left, p, q); boolean r = haveRoot(root.right, p, q); if (((root.val == p.val || root.val == q.val) && (l || r)) || (l && r)) { ret = root; return true; } return root.val == p.val || root.val == q.val || l || r; } }
-
class Solution { public int longestCommonSubsequence(String text1, String text2) { int m = text1.length(), n = text2.length(); int[][] dp = new int[m + 1][n + 1]; for (int i = 1; i <= m; i++) { char c1 = text1.charAt(i - 1); for (int j = 1; j <= n; j++) { char c2 = text2.charAt(j - 1); if (c1 == c2) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[m][n]; } }
-
class Solution { public void flatten(TreeNode root) { List<TreeNode> list = new ArrayList<>(); dfs(list, root); for (int i = 0; i < list.size() - 1; i++) { TreeNode cur = list.get(i); cur.left = null; cur.right = list.get(i + 1); } } private void dfs(List<TreeNode> list, TreeNode root) { if (root != null) { list.add(root); dfs(list, root.left); dfs(list, root.right); } } }
美团·算法题
-
class Solution { public ListNode rotateRight(ListNode head, int k) { if (head == null || k == 0) return head; ListNode cur = head; ListNode pre = null; int count = 0; while (true) { count++; if (cur.next == null) { if (k % count == 0) return head; cur.next = head; pre = cur; break; } cur = cur.next; } ListNode forCur = head; for (int i = 0; i < count; i++) { if ((i + k) % count == 0) { pre.next = null; return forCur; } pre = forCur; forCur = forCur.next; } return null; } }
今日头条·算法题
-
class Solution { public int maxProfit(int[] prices) { int min = Integer.MAX_VALUE, ret = 0; for (int p : prices) { min = Math.min(min, p); ret = Math.max(ret, p - min); } return ret; } }
-
class Solution { public int maxProfit(int[] prices) { int ret = 0; for (int i = 1; i < prices.length; i++) { ret += Math.max(0, prices[i] - prices[i - 1]); } return ret; } }
-
class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> ret = new ArrayList<>(); if (root == null) return ret; Deque<TreeNode> deque = new LinkedList<>(); deque.offer(root); int size = 1; while (size != 0) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < size; i++) { TreeNode cur = deque.poll(); list.add(cur.val); if (cur.left != null) deque.offer(cur.left); if (cur.right != null) deque.offer(cur.right); } ret.add(list); size = deque.size(); } return ret; } }