随笔分类 - 算法有关
摘要:这题和之前做的一个滴滴魔法石题很像。解题步骤也基本一致 由于最低运力肯定是大于等于最大货物重量,小于等于所有货物相加的重量。所以直接二分查找 复杂度应该是NlogN class Solution { public int shipWithinDays(int[] weights, int D) {
阅读全文
摘要:其实就是栈操作能否按照给定顺序输出 public static String getResult(int[] res){ if (res.length <= 2){ return "Yes"; } Deque<Integer> de = new LinkedList<>(); int s = 1;
阅读全文
摘要:愚蠢的做法如下(我也差不多是这样做的。。。) public class Random23 { static int countWays(int p, int q, int r, int last) { if (p < 0 || q < 0 || r < 0) return 0; if (p == 1
阅读全文
摘要:其实工作了几年的我,工作内容涉及到算法的地方并不多,我也不是科班出生,对算法的了解顶了天也就是个冒泡排序吧。因为自己是java方向。半路出家的自己这几年自己为了更容易找到工作,遂苦心钻研jdk,各种框架底层以及中间件原理,所以找个混温饱的工作感觉还挺容易。但是今年初在面试一家中型互联网公司时,最终面
阅读全文
摘要:221 最大正方形 我的解法如下 class Solution { public int maximalSquare(char[][] matrix) { int[][] dp = new int[matrix.length][matrix[0].length]; //x,y代表xy为右上角的最大正
阅读全文
摘要:684 冗余连接 不得不说并查集真的是一种很巧妙的思维了。。。 int[] visit; public int[] findRedundantConnection(int[][] edges) { visit = new int[edges.length+1]; for (int i = 0; i
阅读全文
摘要:130 被围绕的区域 boolean[][] flag; char[][] bod; int deep,width; public void solve(char[][] board) { if (board.length == 0){ return; } deep = board.length;
阅读全文
摘要:437 路径总和||| 我的解法如下,但复杂度很高,想想也知道应该不是好的解法。。。 class Solution { int sum; LinkedList<Integer> list; int result; public int pathSum(TreeNode root, int sum)
阅读全文
摘要:116 填充每个节点的下一个右侧节点 这个题如果没有空间复杂度要求可以如下 Map<Integer,Node> map; public Node connect(Node root) { map = new HashMap<>(); fill(root,1); return root; } publ
阅读全文
摘要:897 递增顺序查找树 老实说这题自己都不知道怎么做出来的。。然后写完提交然后成功了。。然后看自己写的东西看了好一会儿才明白。。汗。。做的头有点晕 class Solution { public TreeNode increasingBST(TreeNode root) { if (root==nu
阅读全文
摘要:404 按理说也可以递归做。 public static int sumOfLeftLeaves(TreeNode root) { int total = 0; LinkedList<TreeNode> stack = new LinkedList<>(); stack.push(root); wh
阅读全文
摘要:397 核心思想应该是尽量减少奇数操作 public static int integerReplacement(int n) { if (n == Integer.MAX_VALUE){ return 0; } int k = 0; int result = 0; while (n !=1){ S
阅读全文
摘要:78 下面是我的写法 应该比较啰嗦 public static List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<>(); for (int i = 0; i < nums.len
阅读全文
摘要:169 这算是个经典的问题了 不讲码德可以这样做 时间复杂度(nlogn) public int majorityElement(int[] nums) { Arrays.sort(nums); return nums[nums.length/2]; } 但是题目要求O(n)时间复杂度和O1空间复杂
阅读全文
摘要:44 情侣牵手 硬写,居然通过了。。 public static int minSwapsCouples(int[] row) { Map<Integer,Integer> map = new HashMap<>(); for (int i = 0; i < row.length; i++) { m
阅读全文
摘要:55 这题感觉自己犯蠢了 看了下评论恍然大悟、、 public boolean canJump(int[] nums) { int len = nums.length; if (len <= 1) return true; int maxDis = nums[0]; for (int i = 1;
阅读全文
摘要:122 其实更简单的做法是只要是前一个数字比后一个大就相加 public static int maxProfit(int[] prices) { int min = prices[0]; int max = prices[0]; int total = 0; for (int i = 1; i <
阅读全文
摘要:23 合并K个升序链表 首先最简单的当然是建个堆往里面加,优点是基本不需要思考。。。 public static ListNode mergeKLists(ListNode[] lists) { PriorityQueue<ListNode> pq = new PriorityQueue<>((k1
阅读全文
摘要:264 原本想的是从1开始遍历,计算每个数是否是丑数,然后用个set存下当前数是否是丑数,后面的数在除以2/3/5如果结果在set里可以找到的话就可以直接取这个结果数作为当前数的计算结果,避免重复运算。结果超时了。。。 然后看了下大佬的思路才知道是用三指针来解,核心思想就是每个丑数都肯定是2,3,5
阅读全文
摘要:703 第K大 class KthLargest { private PriorityQueue<Integer> heap ; private int k; public KthLargest(int k, int[] nums) { heap = new PriorityQueue<>(k,(k
阅读全文