随笔分类 -  leetcode

题解
摘要:class Solution { public int bulbSwitch(int n) { return (int)Math.sqrt(n); // 刚开始灯都是关的,所以按奇数次会打开。比如n = 12; // 会在 第 1,12 2,6 3 ,4 轮被按,所以会被关闭 // 所以序号的因子个 阅读全文
posted @ 2020-09-18 15:19 Sexyomaru 阅读(111) 评论(0) 推荐(0)
摘要:class Solution { private TrieNode root = new TrieNode(); public int minimumLengthEncoding(String[] words) { int res = 0; Arrays.sort(words,(o1,o2)->o2 阅读全文
posted @ 2020-09-15 10:51 Sexyomaru 阅读(188) 评论(0) 推荐(0)
摘要:class Solution { public String removeDuplicateLetters(String s) { int n = s.length(); if(n <= 1) return s; Stack<Character> stack = new Stack<>(); for 阅读全文
posted @ 2020-09-10 16:04 Sexyomaru 阅读(154) 评论(0) 推荐(0)
摘要:class Solution { int[] pa; int[] pb; public int find(int[] p, int x) { if(p[x] != x) p[x] = find(p,p[x]); return p[x]; } public int maxNumEdgesToRemov 阅读全文
posted @ 2020-09-08 10:01 Sexyomaru 阅读(238) 评论(0) 推荐(0)
摘要:方法一:动态规划 class Solution { public: int minCost(string s, vector<int>& cost) { int n = s.size(),INF = 1e9; vector<vector<int>> f(n,vector<int>(27,INF)); 阅读全文
posted @ 2020-09-07 11:24 Sexyomaru 阅读(237) 评论(0) 推荐(0)
摘要:class Solution { int[] p; int[] s; public int largestIsland(int[][] grid) { if(grid.length == 0) return 0; int n = grid.length, m = grid[0].length; p 阅读全文
posted @ 2020-09-02 11:03 Sexyomaru 阅读(143) 评论(0) 推荐(0)
摘要:方法一:树状数组 class NumArray { int[] nums; int[] bitArr; int n; public NumArray(int[] nums) { n = nums.length; this.nums = nums; bitArr = new int[n+1]; for 阅读全文
posted @ 2020-09-01 15:36 Sexyomaru 阅读(164) 评论(0) 推荐(0)
摘要:class Solution { public List<Integer> findMinHeightTrees(int n, int[][] edges) { List<Integer> res = new ArrayList<>(); if (n == 1) { res.add(0); retu 阅读全文
posted @ 2020-09-01 15:23 Sexyomaru 阅读(160) 评论(0) 推荐(0)
摘要:class Solution { public void moveZeroes(int[] nums) { int n = nums.length; for(int i = 0, j = 0; i < n; i++) { if(nums[i] != 0) { int t = nums[i]; num 阅读全文
posted @ 2020-08-29 15:39 Sexyomaru 阅读(117) 评论(0) 推荐(0)
摘要:class Solution { public: vector<int> diffWaysToCompute(string input) { vector<int> res; int n = input.size(); for(int i = 0; i < n; i++) { char c = in 阅读全文
posted @ 2020-08-28 20:58 Sexyomaru 阅读(166) 评论(0) 推荐(0)
摘要:方法一:记忆化dfs class Solution { boolean[][] visited; public boolean containsCycle(char[][] grid) { int m = grid.length, n = grid[0].length; visited = new 阅读全文
posted @ 2020-08-27 17:42 Sexyomaru 阅读(219) 评论(0) 推荐(0)
摘要:分析: 方法一:贪心,首先将每个数的位置用map保存,遍历偶数索引的数,异或1找到它的情侣,如果不在当前索引i+1的位置上,就用map找到这个数,交换到i+1的位置上,并且更新i+1位置上的数在map中的位置 class Solution { public int minSwapsCouples(i 阅读全文
posted @ 2020-08-27 15:30 Sexyomaru 阅读(163) 评论(0) 推荐(0)
摘要:class Solution { LinkedList<String> res = new LinkedList<>(); Map<String,PriorityQueue<String>> map = new HashMap<>(); public List<String> findItinera 阅读全文
posted @ 2020-08-27 10:13 Sexyomaru 阅读(169) 评论(0) 推荐(0)
摘要:class Solution { public boolean judgePoint24(int[] nums) { double[] a = new double[]{nums[0],nums[1],nums[2],nums[3]}; return find(a); } public boolea 阅读全文
posted @ 2020-08-26 15:05 Sexyomaru 阅读(105) 评论(0) 推荐(0)
摘要:class Solution { List<Integer> temp = new ArrayList<Integer>(); List<List<Integer>> ans = new ArrayList<List<Integer>>(); public List<List<Integer>> f 阅读全文
posted @ 2020-08-25 19:47 Sexyomaru 阅读(97) 评论(0) 推荐(0)
摘要:分析:十叉树的遍历 class Solution { public List<Integer> lexicalOrder(int n) { List<Integer> res = new ArrayList<>(); for(int i = 1; i < 10; i++) { dfs(n,res,i 阅读全文
posted @ 2020-08-25 15:06 Sexyomaru 阅读(146) 评论(0) 推荐(0)
摘要:分析: class Solution { public int subarrayBitwiseORs(int[] A) { Set<Integer> set = new HashSet<>(); Set<Integer> cur = new HashSet<>(); for(int num : A) 阅读全文
posted @ 2020-08-20 15:47 Sexyomaru 阅读(133) 评论(0) 推荐(0)
摘要:class Solution { public int getLengthOfOptimalCompression(String s, int k) { int n = s.length(); int[][] dp = new int[n+1][k+1]; // dp[i][j]:考虑前i个字符最多 阅读全文
posted @ 2020-08-20 15:41 Sexyomaru 阅读(414) 评论(0) 推荐(0)
摘要:class Solution { public int findNumberOfLIS(int[] nums) { int n = nums.length; int[] dp = new int[n]; Arrays.fill(dp,1); // dp[i] 以i结尾的最大长度 int[] coun 阅读全文
posted @ 2020-08-19 19:09 Sexyomaru 阅读(89) 评论(0) 推荐(0)
摘要:class Solution { public List<Integer> largestDivisibleSubset(int[] nums) { List<Integer> res = new ArrayList<>(); int n = nums.length; if(n == 0) retu 阅读全文
posted @ 2020-08-19 17:10 Sexyomaru 阅读(122) 评论(0) 推荐(0)