08 2020 档案
摘要: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
阅读全文
摘要: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
阅读全文
摘要:方法一:记忆化dfs class Solution { boolean[][] visited; public boolean containsCycle(char[][] grid) { int m = grid.length, n = grid[0].length; visited = new
阅读全文
摘要:分析: 方法一:贪心,首先将每个数的位置用map保存,遍历偶数索引的数,异或1找到它的情侣,如果不在当前索引i+1的位置上,就用map找到这个数,交换到i+1的位置上,并且更新i+1位置上的数在map中的位置 class Solution { public int minSwapsCouples(i
阅读全文
摘要:class Solution { LinkedList<String> res = new LinkedList<>(); Map<String,PriorityQueue<String>> map = new HashMap<>(); public List<String> findItinera
阅读全文
摘要:分析:先按照w+s排序,从前到后遍历即可 #include <bits/stdc++.h> using namespace std; const int N = 50050; typedef pair<int,int> PII; PII f[N]; int n; int main() { scanf
阅读全文
摘要:分析:先按区间左端点进行排序,然后遍历区间,找一个区间左右端点覆盖起始点,右端点尽量远,直到可以覆盖目标区间右端点 #include <bits/stdc++.h> using namespace std; const int N = 100010; struct Edge{int a, b;} e
阅读全文
摘要:class Solution { public boolean judgePoint24(int[] nums) { double[] a = new double[]{nums[0],nums[1],nums[2],nums[3]}; return find(a); } public boolea
阅读全文
摘要:分析:先将区间按左端点排序,然后建一个小堆存放以有组的右端点,然后遍历区间,当堆为空或堆中最小的右端点都大于当前区间的左端点, 则当前区间另分一组,否则就将当前区间划分给堆中最小左端点的那个区间,并更新这个区间的右端点,最后返回堆的大小 #include <bits/stdc++.h> using
阅读全文
摘要:分析:先按区间的右端点排序,然后从左到右遍历,当前区间左端点小于上一区间的右端点,总个数不变,否则总个数加一,并更新右端点 #include <bits/stdc++.h> using namespace std; const int N = 100010; struct Edge{int a, b
阅读全文
摘要:class Solution { List<Integer> temp = new ArrayList<Integer>(); List<List<Integer>> ans = new ArrayList<List<Integer>>(); public List<List<Integer>> f
阅读全文
摘要:分析:十叉树的遍历 class Solution { public List<Integer> lexicalOrder(int n) { List<Integer> res = new ArrayList<>(); for(int i = 1; i < 10; i++) { dfs(n,res,i
阅读全文
摘要:分析: class Solution { public int subarrayBitwiseORs(int[] A) { Set<Integer> set = new HashSet<>(); Set<Integer> cur = new HashSet<>(); for(int num : A)
阅读全文
摘要: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个字符最多
阅读全文
摘要: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
阅读全文
摘要:class Solution { public List<Integer> largestDivisibleSubset(int[] nums) { List<Integer> res = new ArrayList<>(); int n = nums.length; if(n == 0) retu
阅读全文
摘要:#include <bits/stdc++.h> using namespace std; const int N = 6060; int h[N], e[N], ne[N],idx; int w[N], f[N][2]; bool hasF[N]; int n; void add(int a, i
阅读全文
摘要:方法一: 背包模型 #include <bits/stdc++.h> using namespace std; const int N = 10010, mod = 1e9 + 7; int dp[N][N]; int n; int main() { scanf("%d",&n); for(int
阅读全文
摘要:class Solution { int[] color; Map<Integer,List<Integer>> map; public boolean possibleBipartition(int N, int[][] dislikes) { map = new HashMap<>(); col
阅读全文
摘要:方法一:记录0的个数减1的个数有没有出现过 class Solution { public int findMaxLength(int[] nums) { int n = nums.length; Map<Integer,Integer> map = new HashMap<>(); map.put
阅读全文
摘要:方法一:动态规划 分奇数和偶数的情况 class Solution { public int[] countBits(int num) { int[] dp = new int[num+1]; for(int i = 0; i <= num; i++) { if(i % 2 == 0) dp[i]
阅读全文
摘要:class Solution { Map<Integer, Integer> map = new HashMap(); public int minDays(int n) { if(n == 0) return 0; if(!map.containsKey(n)){ int ans = n; //
阅读全文
摘要:分析:在整个数域范围上二分查找最小磁力的大小 然后记录这个最小磁力可以在position上放置几个球,根据球的数量与m的大小来决定二分范围 class Solution { public int maxDistance(int[] position, int m) { int n = positio
阅读全文
摘要:class Solution { public int removeBoxes(int[] boxes) { int n = boxes.length; int[][][] dp = new int[n][n][n]; // dp[i][j][k] 从i到j的盒子j右面右连续的等于第j个盒子颜色有k
阅读全文
摘要:class Solution { int index = 0; public int calculate(String s) { int n = s.length(); Stack<Integer> stack = new Stack(); char pre = '+'; int num = 0;
阅读全文
摘要:class Solution { public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { int n = nums.length; TreeSet<Integer> set = new TreeSet<>();
阅读全文
摘要:class Solution { public List<List<Integer>> getSkyline(int[][] buildings) { List<List<Integer>> res = new ArrayList<>(); Node[] nodes = new Node[build
阅读全文
摘要:#include <bits/stdc++.h> using namespace std; const int N = 550, M = 100010; int h[N], e[M], ne[M], idx; int match[N]; bool st[N]; int n1, n2, m; void
阅读全文
摘要:#include <bits/stdc++.h> using namespace std; const int N = 100010; int h[N], e[N], ne[N], w[N], idx; int color[N]; int n, m; void add(int a, int b, i
阅读全文
摘要:#include <bits/stdc++.h> using namespace std; const int N = 200020; int p[N]; int n, m; struct Edge { int a, b, w; bool operator< (const Edge &W) cons
阅读全文
摘要:#include <bits/stdc++.h> using namespace std; const int N = 550, M = 100010, INF = 0x3f3f3f3f; int g[N][N], dist[N]; // dist存点到(最小生成树的点集合)的最小距离 bool s
阅读全文
摘要:分析: f[i, j, k]表示从i走到j的路径上除i和j点外只经过1到k的点的所有路径的最短距离。那么f[i, j, k] = min(f[i, j, k - 1), f[i, k, k - 1] + f[k, j, k - 1]。 因此在计算第k层的f[i, j]的时候必须先将第k - 1层的所
阅读全文
摘要:#include <bits/stdc++.h> using namespace std; const int N = 10010; int h[N], e[N], ne[N], w[N], idx; int dist[N], cnt[N]; bool st[N]; int n, m; void a
阅读全文
摘要:#include <bits/stdc++.h> using namespace std; const int N = 100010; int h[N], e[N], w[N], ne[N], idx; int dist[N]; bool st[N]; int m, n; void add(int
阅读全文
摘要:#include <bits/stdc++.h> using namespace std; const int N = 550, M = 10010; struct Edges{int a, b, w;}edges[M]; int dist[N], pre[N]; int m, n, k; int
阅读全文
摘要:#include <bits/stdc++.h> using namespace std; const int N = 1000010; int h[N], e[N], ne[N], w[N], idx; int dist[N]; bool st[N]; int m, n; void add(int
阅读全文
摘要:#include <bits/stdc++.h> using namespace std; const int N = 550, M = 10010; int g[N][N], dist[N]; bool st[N]; int m, n; int dijkstra() { memset(dist,0
阅读全文
摘要:分析:将木棍的两个端点(0和n)加到cost数组中,dp[i][j]表示把这段木棍的第i到j个点完成切割的最小成本,i和j都是cost数组的下标。 状态计算: 最后的合并位置是k dp[i][j] = min(dp[i, k] + dp[k, j] + cost) class Solution {
阅读全文
摘要:class Solution { public int maxNonOverlapping(int[] nums, int target) { int n = nums.length; int[] dp = new int[n+1]; // nums前i个数的最大满足条件子数组数目 Map<Inte
阅读全文
摘要:1540. K 次操作转变字符串 class Solution { public boolean canConvertString(String s, String t, int k) { if(s.length() != t.length()) return false; int n = s.le
阅读全文
摘要:class Solution { public int countBinarySubstrings(String s) { int n = s.length(); int res = 0; int pre = 0, cur = 1; for(int i = 1; i < n; i++) { if(s
阅读全文
摘要:方法一:dfs class Solution { public boolean isHappy(int n) { dfs(n); return flag; } boolean flag = false; Set<Integer> set = new HashSet<>(); public void
阅读全文
摘要:class Solution { public int rangeBitwiseAnd(int m, int n) { // 1.如果m与n二进制位数不同的话 由m到n每一位二进制位都出现过0,这里可以解释为: //由低位向高位进位时,低位会出现0,位数少的数所有位都会出现0 //比如从所有的三位二
阅读全文
摘要:class Solution { public String convertToTitle(int n) { if(n <= 0) return ""; StringBuilder sb = new StringBuilder(); while(n > 0) { n--; sb.append((ch
阅读全文
摘要:方法一:枚举前后缀,使用HashMap class Solution { public List<List<Integer>> palindromePairs(String[] words) { List<List<Integer>> res = new ArrayList<>(); int n =
阅读全文
摘要:class Solution { public int countGoodTriplets(int[] arr, int a, int b, int c) { int n = arr.length, res = 0; for(int i = 0; i < n - 2; i++) { for(int
阅读全文
摘要:方法一:滑动窗口 class Solution { public int[] smallestRange(List<List<Integer>> nums) { int size = nums.size(); Map<Integer,List<Integer>> map = new HashMap<
阅读全文

浙公网安备 33010602011771号