07 2020 档案

摘要:分析: 这是道二维费用的背包问题,可以像01背包一样优化掉第一维的决策,选或不选这个袋子,然后需要像01背包一样倒序循环体积,和01背包的优化一样。 其次,这道题的难点是可以买多,只要买够就行,我们只需要对负数的体积取0就行,表示我们即使当前需要的饮料小于袋子中的饮料数量,我们依然可以购买,只要从0 阅读全文
posted @ 2020-07-31 15:07 Sexyomaru 阅读(140) 评论(0) 推荐(0)
摘要:分析:贪心加回溯 import java.util.*; public class Solution { /** * 返回最后要输出的答案 * @param n int整型 表示牛牛的数字 * @param m int整型 表示牛妹的数字 * @return int整型 */ public int 阅读全文
posted @ 2020-07-31 15:06 Sexyomaru 阅读(169) 评论(0) 推荐(0)
摘要:分析: 首先我们要思考如果让这个NP完全题目复杂度降低,那么可以优先考虑到使用位运算,状态压缩等解决思路。 然后接着思考,我们可以发现,我们所需要的不是整个方案,而只是方案最优解,所以我们只需要记录当前这个方案的最优解即可,那么我们考虑的状态,不久只有,在当前方案i中,目前抵达的点是j。 现在既然装 阅读全文
posted @ 2020-07-31 14:17 Sexyomaru 阅读(181) 评论(0) 推荐(0)
摘要:分析: 1. 所谓的状态压缩DP,就是用二进制数保存状态。为什么不直接用数组记录呢?因为用一个二进制数记录方便作位运算。 2. 本题等价于找到所有横放 1 X 2 小方格的方案数,因为所有横放确定了,那么竖放方案是唯一的。 3. 用f[i][j]记录第i-1列已经充满且第i列第j个状态。j状态位等于 阅读全文
posted @ 2020-07-31 11:05 Sexyomaru 阅读(213) 评论(0) 推荐(0)
摘要:class Solution { public int maxProduct(int[] nums) { int n = nums.length; int preMin = nums[0]; // i 之前最小值 int preMax = nums[0]; // i 之前最大值 int res = 阅读全文
posted @ 2020-07-30 16:04 Sexyomaru 阅读(60) 评论(0) 推荐(0)
摘要:class Solution { public int maximumSum(int[] arr) { int n = arr.length; int[][] dp = new int[n][2]; // dp[i][0]: 以i结尾未删 dp[i][1]: 以i结尾未删过 dp[0][0] = a 阅读全文
posted @ 2020-07-30 10:58 Sexyomaru 阅读(145) 评论(0) 推荐(0)
摘要:class Solution { public int longestOnes(int[] A, int K) { int n = A.length; int res = 0, rest = K; int l = 0, r = 0; while(r < n) { int num = A[r++]; 阅读全文
posted @ 2020-07-30 10:56 Sexyomaru 阅读(80) 评论(0) 推荐(0)
摘要:class Solution { public int numWays(int n, int k) { if(k == 0 || n == 0) return 0; if(n < 2) return k; int[] dp = new int[n]; dp[0] = k; //dp[i] 用来表示i 阅读全文
posted @ 2020-07-29 16:52 Sexyomaru 阅读(422) 评论(0) 推荐(0)
摘要:方法一:动态规划 时间复杂度O(n2) class Solution { public int wiggleMaxLength(int[] nums) { int n = nums.length; if(n == 0) return 0; int[][] dp = new int[n+1][2];/ 阅读全文
posted @ 2020-07-29 16:17 Sexyomaru 阅读(87) 评论(0) 推荐(0)
摘要:归并排序 class Solution { public ListNode sortList(ListNode head) { if(head == null || head.next == null) return head; ListNode fast = head,slow = head; w 阅读全文
posted @ 2020-07-27 15:36 Sexyomaru 阅读(83) 评论(0) 推荐(0)
摘要:class Solution { public void reorderList(ListNode head) { if(head == null || head.next == null || head.next.next == null) return; ListNode mid = findM 阅读全文
posted @ 2020-07-27 11:28 Sexyomaru 阅读(67) 评论(0) 推荐(0)
摘要:class Solution { public int countPairs(TreeNode root, int distance) { dfs(root,0,distance); return res; } private int res = 0; public List<Integer> df 阅读全文
posted @ 2020-07-27 10:55 Sexyomaru 阅读(379) 评论(0) 推荐(0)
摘要:1524. 和为奇数的子数组数目 class Solution { public int numOfSubarrays(int[] arr) { int n = arr.length; int sum = 0; int res = 0, odd = 0, even = 1; for(int i = 阅读全文
posted @ 2020-07-27 10:30 Sexyomaru 阅读(96) 评论(0) 推荐(0)
摘要:class Solution { public int candy(int[] ratings) { int n = ratings.length; int[] arr = new int[n]; Arrays.fill(arr,1); // 先每人分一个 for(int i = 1; i < n; 阅读全文
posted @ 2020-07-24 17:38 Sexyomaru 阅读(105) 评论(0) 推荐(0)
摘要:class Solution { public double new21Game(int N, int K, int W) { // 先判断 K + W 是否小于N,如果是的话,说明肯定能赢得游戏,返回 1.0,也就是 100% if (N >= K + W) { return 1.0; } dou 阅读全文
posted @ 2020-07-23 22:21 Sexyomaru 阅读(101) 评论(0) 推荐(0)
摘要:方法一:利用归并排序保存位置数组,交换时交换位置数组 class Solution { int[] count; public List<Integer> countSmaller(int[] nums) { int n = nums.length; count = new int[n]; int[ 阅读全文
posted @ 2020-07-23 21:31 Sexyomaru 阅读(186) 评论(0) 推荐(0)
摘要:方法一:Map加双向链表 class LRUCache { class Node { public int key, val; Node pre,next; public Node() {} public Node(int k, int v) { key = k; val = v; } } priv 阅读全文
posted @ 2020-07-23 21:13 Sexyomaru 阅读(114) 评论(0) 推荐(0)
摘要: 阅读全文
posted @ 2020-07-22 19:49 Sexyomaru 阅读(142) 评论(0) 推荐(0)
摘要:class Solution { public int maxEvents(int[][] events) { Arrays.sort(events, Comparator.comparingInt(o -> o[0])); PriorityQueue<Integer> queue = new Pr 阅读全文
posted @ 2020-07-22 15:54 Sexyomaru 阅读(142) 评论(0) 推荐(0)
摘要:class Solution { public int findTheLongestSubstring(String s) { int n = s.length(); int[] pos = new int[32]; // 11111最大为31,记录所有状态可能的情况开32就可以了 // pos[s 阅读全文
posted @ 2020-07-22 13:45 Sexyomaru 阅读(178) 评论(0) 推荐(0)
摘要:class Solution { public int countCornerRectangles(int[][] grid) { if(grid.length < 2 || grid[0].length < 2) return 0; int m = grid.length, n = grid[0] 阅读全文
posted @ 2020-07-21 16:37 Sexyomaru 阅读(142) 评论(0) 推荐(0)
摘要:class Solution { public int numSubmat(int[][] mat) { if(mat.length == 0 || mat[0].length == 0) return 0; int m = mat.length, n = mat[0].length; int[][ 阅读全文
posted @ 2020-07-21 16:36 Sexyomaru 阅读(178) 评论(0) 推荐(0)
摘要:分析:dp[i][j]表示右下角坐标为(i,j)的最大正方形边长,累加即可 class Solution { public int countSquares(int[][] matrix) { if(matrix.length == 0 || matrix[0].length == 0) retur 阅读全文
posted @ 2020-07-21 16:33 Sexyomaru 阅读(124) 评论(0) 推荐(0)
摘要:位运算性质:a&b不会大于a也不会大于b,会将二进制中的1变为0,但是0不会变为1 a|b不会小于a也不会小于b,会将二进制中的0变为1,但是1不会变为0 class Solution { public int closestToTarget(int[] arr, int target) { Set 阅读全文
posted @ 2020-07-21 15:48 Sexyomaru 阅读(130) 评论(0) 推荐(0)
摘要:参考https://space.bilibili.com/3203291/零神的题解 class Solution { public: int closestToTarget(vector<int>& arr, int target) { int ans = abs(arr[0] - target) 阅读全文
posted @ 2020-07-20 11:01 Sexyomaru 阅读(168) 评论(0) 推荐(0)
摘要:#include <iostream> #include <cstring> #include <cmath> using namespace std; const int N = 200010, M = 18; int n,m; int w[N],f[N][M]; void init() { fo 阅读全文
posted @ 2020-07-20 10:56 Sexyomaru 阅读(154) 评论(0) 推荐(0)
摘要:class Solution { public int maxProfit(int k, int[] prices) { int n = prices.length; if(n < 2) return 0; if(k >= n) { int res = 0; for(int i = 1; i < n 阅读全文
posted @ 2020-07-19 15:39 Sexyomaru 阅读(103) 评论(0) 推荐(0)
摘要:class Solution { public List<Integer> getRow(int rowIndex) { Integer[] res = new Integer[rowIndex+1]; Arrays.fill(res,1); for(int i = 1; i <= rowIndex 阅读全文
posted @ 2020-07-19 14:49 Sexyomaru 阅读(113) 评论(0) 推荐(0)
摘要:class Solution { public int numDistinct(String s, String t) { int m = s.length(), n = t.length(); int[][] dp = new int[m+1][n+1]; // dp[i][j]表示s前i个包含t 阅读全文
posted @ 2020-07-19 14:23 Sexyomaru 阅读(87) 评论(0) 推荐(0)
摘要:class Solution { public int maxCoins(int[] nums) { int n = nums.length + 2; int[] points = new int[n]; points[0] = 1; points[n-1] = 1; for(int i = 1; 阅读全文
posted @ 2020-07-19 13:33 Sexyomaru 阅读(134) 评论(0) 推荐(0)
摘要:方法一: 动态规划 class Solution { public boolean isInterleave(String s1, String s2, String s3) { int n1 = s1.length(), n2 = s2.length(), n3 = s3.length(); if 阅读全文
posted @ 2020-07-18 20:55 Sexyomaru 阅读(146) 评论(0) 推荐(0)
摘要:方法一:暴力搜索 class Solution { private int res = 0; public int pathSum(TreeNode root, int sum) { order(root,sum); return res; } public void order(TreeNode 阅读全文
posted @ 2020-07-16 20:09 Sexyomaru 阅读(144) 评论(0) 推荐(0)
摘要:判断二分图,使用染色法 方法一:BFS public boolean isBipartite(int[][] graph) { int n = graph.length; int[] color = new int[n]; // 染色数组 0表示为染色, 1 -1为不同色 for(int i = 0 阅读全文
posted @ 2020-07-16 15:07 Sexyomaru 阅读(136) 评论(0) 推荐(0)
摘要:方法一:分治,递归 class Solution { int index = 0; // 全局变量记录遍历到的位置 public String decodeString(String s) { int n = s.length(); StringBuilder sb = new StringBuil 阅读全文
posted @ 2020-07-15 20:32 Sexyomaru 阅读(144) 评论(0) 推荐(0)
摘要:方法一:不用统计前缀和,只需要统计前i个数的余数就可以,若之前和的余数和当前和的余数相等则子数组可以整除K,遍历一遍即可 class Solution { public int subarraysDivByK(int[] A, int k) { int n = A.length; Map<Integ 阅读全文
posted @ 2020-07-15 20:13 Sexyomaru 阅读(127) 评论(0) 推荐(0)
摘要:class Solution { public List<List<Integer>> verticalOrder(TreeNode root) { if(root == null) return new ArrayList<>(); Map<Integer,List<Integer>> map = 阅读全文
posted @ 2020-07-14 19:23 Sexyomaru 阅读(102) 评论(0) 推荐(0)
摘要:class Solution { public int longestConsecutive(TreeNode root) { dfs(root); return res; } private int res = 0; public int[] dfs(TreeNode root) { // 以ro 阅读全文
posted @ 2020-07-14 17:38 Sexyomaru 阅读(359) 评论(0) 推荐(0)
摘要:class Solution { public int maxPathSum(TreeNode root) { dfs(root); return res; } int res = Integer.MIN_VALUE; public int dfs(TreeNode root) { // 返回以当前 阅读全文
posted @ 2020-07-14 17:35 Sexyomaru 阅读(126) 评论(0) 推荐(0)
摘要:方法一: class Solution { public int longestConsecutive(int[] nums) { Set<Integer> set = new HashSet<>(); for(int num : nums) set.add(num); int res = 0; f 阅读全文
posted @ 2020-07-14 11:05 Sexyomaru 阅读(100) 评论(0) 推荐(0)
摘要:class Solution { public Node connect(Node root) { if(root==null) return root; if(root.left!=null && root.right!=null){ root.left.next=root.right; } if 阅读全文
posted @ 2020-07-14 10:43 Sexyomaru 阅读(107) 评论(0) 推荐(0)
摘要:方法一: class Solution { public String removeKdigits(String num, int k) { int n = num.length(); if(n <= k) return "0"; StringBuilder sb = new StringBuild 阅读全文
posted @ 2020-07-14 09:58 Sexyomaru 阅读(142) 评论(0) 推荐(0)
摘要:class Solution { public TreeNode deleteNode(TreeNode root, int key) { if(root == null) return null; if(root.val == key) { if(root.left == null) { retu 阅读全文
posted @ 2020-07-13 09:46 Sexyomaru 阅读(122) 评论(0) 推荐(0)
摘要:方法一:层序遍历,只要节点不连续,返回false class Solution { public boolean isCompleteTree(TreeNode root) { Queue<TreeNode> queue=new LinkedList<>(); TreeNode prev=root; 阅读全文
posted @ 2020-07-13 09:39 Sexyomaru 阅读(179) 评论(0) 推荐(0)
摘要:class Solution { public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) { Map<Integer,List<Node>> map = new HashMap 阅读全文
posted @ 2020-07-12 15:29 Sexyomaru 阅读(226) 评论(0) 推荐(0)
摘要:class Solution { public boolean winnerSquareGame(int n) { boolean[] dp = new boolean[n+1]; // dp[i] 表示有i个石子,alice先手是否可以赢 for(int i = 1; i * i <= n; i+ 阅读全文
posted @ 2020-07-12 14:30 Sexyomaru 阅读(129) 评论(0) 推荐(0)
摘要:分析:逆向dp class Solution { public int calculateMinimumHP(int[][] dungeon) { if(dungeon.length == 0 || dungeon[0].length == 0) return 0; int m = dungeon. 阅读全文
posted @ 2020-07-12 13:33 Sexyomaru 阅读(179) 评论(0) 推荐(0)
摘要:解法一: f[i][j]表示数列A的前i个和数列B的前j个且以B[i]结尾的最长公共上升子序列 f[i][j]=f[i−1][j] (a[i]≠b[j]) f[i][j]=max(f[i−1][j],f[i−1][t]+1) (a[i]=b[j] 且 1 <= t < j) #include <io 阅读全文
posted @ 2020-07-11 16:01 Sexyomaru 阅读(110) 评论(0) 推荐(0)
摘要:分析:暴力法是可以做的但是时间复杂度O(n2),竞赛选手很容易想到用线段树,树状数组来优化时间复杂度,这里贴几种容易理解的方法 方法一:归并排序,归并排序可以求逆序对,这是我们熟悉的,所以在归并排序的合并过程,我们可以求出右边小于当前数的有几个,这道题需要返回每个位置的右边小于它的元素数量,我们可以 阅读全文
posted @ 2020-07-11 13:26 Sexyomaru 阅读(133) 评论(0) 推荐(0)
摘要:class Solution { List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> levelOrder(TreeNode root) { dfs(root,1); return res; } public 阅读全文
posted @ 2020-07-10 11:27 Sexyomaru 阅读(102) 评论(0) 推荐(0)
摘要:方法一:递归 class Solution { public boolean isSymmetric(TreeNode root) { if(root == null) return true; return isSame(root.left,root.right); } public boolea 阅读全文
posted @ 2020-07-10 11:19 Sexyomaru 阅读(73) 评论(0) 推荐(0)
摘要:class Solution { public ListNode reverseBetween(ListNode head, int m, int n) { if(m == 1) { ListNode node = reverse(head,n); return node; } head.next 阅读全文
posted @ 2020-07-10 10:39 Sexyomaru 阅读(77) 评论(0) 推荐(0)
摘要:#include <iostream> using namespace std; int main() { int a, b , p; cin >> a >> b >> p; int res = 1 % p; while(b) { if(b&1) res = res * 1ll * a % p; a 阅读全文
posted @ 2020-07-09 11:55 Sexyomaru 阅读(157) 评论(0) 推荐(0)
摘要:知识点:格雷编码的生成过程, G(i) = i ^ (i/2); 如 n = 3: G(0) = 000, G(1) = 1 ^ 0 = 001 ^ 000 = 001 G(2) = 2 ^ 1 = 010 ^ 001 = 011 G(3) = 3 ^ 1 = 011 ^ 001 = 010 G(4 阅读全文
posted @ 2020-07-09 10:28 Sexyomaru 阅读(224) 评论(0) 推荐(0)
摘要:class Solution { public boolean isScramble(String s1, String s2) { int n = s1.length(); if(n == 0) return true; if(s1.equals(s2)) return true; int[] a 阅读全文
posted @ 2020-07-09 10:23 Sexyomaru 阅读(148) 评论(0) 推荐(0)
摘要:class Solution { public int respace(String[] dictionary, String sentence) { int n = sentence.length(); if(n == 0) return 0; Trie t = new Trie(); for(S 阅读全文
posted @ 2020-07-09 10:08 Sexyomaru 阅读(124) 评论(0) 推荐(0)
摘要:class Solution { public int countCornerRectangles(int[][] grid) { if(grid.length < 2 || grid[0].length < 2) return 0; int m = grid.length, n = grid[0] 阅读全文
posted @ 2020-07-08 21:32 Sexyomaru 阅读(263) 评论(0) 推荐(0)
摘要:class Solution { public int removeDuplicates(int[] nums) { int i = 0; for (int n : nums) if (i < 2 || n > nums[i-2]) // 几个元素重复都可以用相同的方法 nums[i++] = n; 阅读全文
posted @ 2020-07-08 21:30 Sexyomaru 阅读(83) 评论(0) 推荐(0)
摘要:class Solution { // 两种二分模版 public boolean searchMatrix(int[][] matrix, int target) { if(matrix.length == 0 || matrix[0].length == 0) return false; int 阅读全文
posted @ 2020-07-08 21:28 Sexyomaru 阅读(113) 评论(0) 推荐(0)
摘要:class Solution { public void setZeroes(int[][] matrix) { int m = matrix.length; if(m == 0) return; int n = matrix[0].length; boolean row0 = false, col 阅读全文
posted @ 2020-07-08 21:27 Sexyomaru 阅读(159) 评论(0) 推荐(0)
摘要:思路: 把当前目录压入栈中,遇到..弹出栈顶,最后返回栈中元素. class Solution { public String simplifyPath(String path) { String[] strs = path.split("/"); Stack<String> stack = new 阅读全文
posted @ 2020-07-08 21:25 Sexyomaru 阅读(107) 评论(0) 推荐(0)
摘要:class Solution { public boolean isNumber(String s) { s = s.trim(); int n = s.length(); char[] arr = s.toCharArray(); boolean numSeen = false, dotSeen 阅读全文
posted @ 2020-07-08 21:23 Sexyomaru 阅读(160) 评论(0) 推荐(0)
摘要:class Solution { public int mySqrt(int x) { if(x < 2) return x; int l = 0, r = x; while(l < r) { int mid = (l + r) >> 1; if(x / mid < mid) { r = mid; 阅读全文
posted @ 2020-07-08 21:21 Sexyomaru 阅读(148) 评论(0) 推荐(0)
摘要:本题也可以使用回溯,但耗时太久,这里介绍数学统计的方法 class Solution { public String getPermutation(int n, int k) { StringBuilder sb = new StringBuilder(); boolean[] st = new b 阅读全文
posted @ 2020-07-08 21:11 Sexyomaru 阅读(90) 评论(0) 推荐(0)
摘要:class Solution { List<String> res = new ArrayList<>(); public List<String> addOperators(String num, int target) { dfs(num,0,"",target,0,0); return res 阅读全文
posted @ 2020-07-08 21:07 Sexyomaru 阅读(173) 评论(0) 推荐(0)
摘要:class Solution { public List<TreeNode> generateTrees(int n) { if(n == 0) return new ArrayList<>(); return build(1,n); } public List<TreeNode> build(in 阅读全文
posted @ 2020-07-08 21:03 Sexyomaru 阅读(103) 评论(0) 推荐(0)
摘要:class Solution { public List<Integer> findSubstring(String s, String[] words) { List<Integer> res = new ArrayList<>(); int n = s.length(), m = words.l 阅读全文
posted @ 2020-07-08 21:01 Sexyomaru 阅读(165) 评论(0) 推荐(0)
摘要:解法一: 动态规划 class Solution { public int longestValidParentheses(String s) { int n = s.length(); if(n < 2) return 0; char[] arr = s.toCharArray(); int[] 阅读全文
posted @ 2020-07-08 20:59 Sexyomaru 阅读(196) 评论(0) 推荐(0)
摘要:class Solution { //倍增思想 public int divide(int dividend, int divisor) { if(dividend == 0) return 0; if(dividend == Integer.MIN_VALUE && divisor == - 1) 阅读全文
posted @ 2020-07-08 20:51 Sexyomaru 阅读(238) 评论(0) 推荐(0)
摘要:class Solution { public ListNode reverseKGroup(ListNode head, int k) { if(head == null || head.next == null || k == 1) return head; ListNode node = he 阅读全文
posted @ 2020-07-08 20:45 Sexyomaru 阅读(166) 评论(0) 推荐(0)
摘要:class Solution { public int maxArea(int[] height) { int n = height.length; int l = 0, r = n - 1; int res = 0; while(l < r) { // 每次移动较低的指针,这样不会丢失最大值 re 阅读全文
posted @ 2020-07-08 20:39 Sexyomaru 阅读(92) 评论(0) 推荐(0)
摘要:class Solution { public boolean isMatch(String s, String p) { int m = s.length(), n = p.length(); char[] arrs = s.toCharArray(); char[] arrp = p.toCha 阅读全文
posted @ 2020-07-08 20:38 Sexyomaru 阅读(342) 评论(0) 推荐(0)
摘要:class Solution { public int myAtoi(String str) { str = str.trim(); int n = str.length(); if(n == 0) return 0; char[] arr = str.toCharArray(); int atoi 阅读全文
posted @ 2020-07-08 20:36 Sexyomaru 阅读(176) 评论(0) 推荐(0)
摘要:本题解法很多,可用动态规划 中心扩展法 这里给出一种最快的方法manacher算法 class Solution { public String longestPalindrome(String s) { char[] arr = manacher(s.toCharArray()); int n = 阅读全文
posted @ 2020-07-08 20:33 Sexyomaru 阅读(109) 评论(0) 推荐(0)
摘要:class Solution { // 本题采用折半删除法 public double findMedianSortedArrays(int[] nums1, int[] nums2) { int k = nums1.length + nums2.length; if(k % 2 == 0) { r 阅读全文
posted @ 2020-07-08 20:25 Sexyomaru 阅读(133) 评论(0) 推荐(0)
摘要:请注意本题有复杂度要求 O(n) 本题有一种很重要的思想,可以将本类统计问题,想象为上下车问题,我们不需要清楚的统计每个时间点有多少个人,只需要统计上车点上了多少人,下车点下了多少人,之后每个点人数可以表示为f(i) = f(i) + f(i-1) class Solution { public i 阅读全文
posted @ 2020-07-08 20:21 Sexyomaru 阅读(123) 评论(0) 推荐(0)
摘要:1、Http和Https的区别 Http协议运行在TCP之上,明文传输,客户端与服务器端都无法验证对方的身份;Https是身披SSL(Secure Socket Layer)外壳的Http,运行于SSL上,SSL运行于TCP之上,是添加了加密和认证机制的HTTP。二者之间存在如下不同: 端口不同:H 阅读全文
posted @ 2020-07-08 17:00 Sexyomaru 阅读(151) 评论(0) 推荐(0)
摘要:方法一: 动态规划 (O(n)) class Solution { public int lengthOfLIS(int[] nums) { int n = nums.length; if(n < 2) return n; int[] dp = new int[n+1]; // dp[i] : 以第 阅读全文
posted @ 2020-07-08 11:50 Sexyomaru 阅读(151) 评论(0) 推荐(0)
摘要:两种复杂度O(n)的方法 方法一:快速选择 class Solution { public int findKthLargest(int[] nums, int k) { int lo = 0, hi = nums.length - 1; // 当前partition 函数的区间边界 int tar 阅读全文
posted @ 2020-07-08 11:44 Sexyomaru 阅读(141) 评论(0) 推荐(0)
摘要:题目描述: 给定一个正数数组arr,arr的累加和代表金条的总长度,arr的每个数代表金条要分成的长度。规定长度为K的金条只需分成两块,费用为K个铜板。返回把金条分出arr中的每个数字的最小代价。 public static int split(int[] arr) { if(arr.length 阅读全文
posted @ 2020-07-07 21:40 Sexyomaru 阅读(371) 评论(0) 推荐(0)
摘要:题目描述 给定两个整数W和K,W代表你拥有的初始资金,K代表你最多可以做K个项目。再给定两个长度为N的正数数组costs[]和profits[],代表一共有N个项目,costs[i]和profits[i]分别表示第i号项目的启动资金与做完后的利润(注意是利润,如果一个项目的启动资金为10,利润为4, 阅读全文
posted @ 2020-07-07 21:17 Sexyomaru 阅读(509) 评论(0) 推荐(0)
摘要:(一)请分别简单说一说进程和线程以及它们的区别。 进程是对运行时程序的封装,是系统进行资源调度和分配的的基本单位,实现了操作系统的并发; 线程是进程的子任务,是CPU调度和分派的基本单位,用于保证程序的 实时性,实现进程内部的并发; 一个程序至少有一个进程,一个进程至少有一个线程,线程依赖于进程而存 阅读全文
posted @ 2020-07-07 17:27 Sexyomaru 阅读(335) 评论(0) 推荐(0)
摘要:class TreeAncestor { int[][] dp; //预处理数组 dp[i][j] 表示i的第2^j个祖先 // 倍增思想 public TreeAncestor(int n, int[] parent) { dp = new int[n][(int)(Math.log(n) / M 阅读全文
posted @ 2020-07-07 16:43 Sexyomaru 阅读(164) 评论(0) 推荐(0)
摘要:方法一:滑动窗口 class Solution { public List<Integer> partitionLabels(String s) { List<Integer> res = new ArrayList<>(); int[] arr = new int[128]; for(char c 阅读全文
posted @ 2020-07-07 16:35 Sexyomaru 阅读(151) 评论(0) 推荐(0)
摘要:不含负数,可能含有相同数值的山峰数组(环形) 山峰A和山峰B能互相看见的条件是: 1、AB是不同的山且相邻 2、AB是不同的山,且A到B的两个方向上至少有一个方向上没有比A高的山峰 分析:本题有两种情况 第一种是山峰都是不同高度的,这样的话本题就可以有O(1)的解,山峰如果是不同高度,那么当山峰数量 阅读全文
posted @ 2020-07-07 15:28 Sexyomaru 阅读(197) 评论(0) 推荐(0)