JAVA
文章平均质量分 60
Black_Cater
这个作者很懒,什么都没留下…
展开
-
Fenwick Tree学习 灵魂画手 307. Range Sum Query - Mutable 线段树
这个人说的非常非常非常清晰,强烈推荐https://www.youtube.com/watch?v=uSFzHCZ4E-8对数组进行切分, 根据二进制排出父子节点。class BIT { int[] aux; public BIT(int[] nums) { aux = new int[nums.length + 1]; System.arraycopy(nums, 0, aux, 1, nums.length); for (int i原创 2021-03-30 21:35:12 · 156 阅读 · 0 评论 -
蚂蚁金服多线程面试题:三个线程ABC轮流打印1-100
public class TestThreadABC { public static void main(String[] args) { Print action = new Print(); new A(action, "A--->").start(); new B(action, "B--->").start(); new C(action, "C--->").start(); }}class原创 2021-02-26 10:29:29 · 905 阅读 · 1 评论 -
322. Coin Change 非常经典的动态规划问题
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, retu.原创 2021-02-07 11:55:26 · 261 阅读 · 0 评论 -
LEETCODE 979 Distribute Coins in Binary Tree
**Algs :This is a kind of Tree problem, so obviously, we can use a dfs to solve;for this problem, we want to fill each node with one coin, so we can ask left child if you have any coins left, please give them to me, then doing the same thing to the righ原创 2021-01-17 12:25:23 · 135 阅读 · 0 评论 -
42. Trapping Rain Water
给定一组数组,分别代表不同长度的柱子,求最大装水容量;https://leetcode.com/problems/trapping-rain-water/Example 1:Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Explanation: Theabove elevation map (black section) is represented by array[0,1,0,2,1,0,1,3,2,1,2,1]. In this c原创 2020-12-20 22:49:25 · 85 阅读 · 0 评论 -
LeetCode.46 & 47 全排列
PermutataionAlgs:基础回溯算法,列出每种可能性, 剪枝直接在每次递归最后撤销选择即可。public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res = new ArrayList<>(); List<Integer> list = new ArrayList<>(); .原创 2020-12-07 23:50:22 · 106 阅读 · 0 评论 -
LeetCode 31. Next Permutation
题目: 给定一个数组, 找出其permutation中下一个大的permutation;解释: 按照从小到大的顺序,如果数组是12432, 那么需要返回13224, 即下一个比他大的数组。题解:1.从右往左遍历数组,一旦发现有降序相邻数组,即nums[i] > nums[i-1], 那么变化一定是在nums[i-1],使从i-1向后的数组成为原数组i-1向后数组的下一个最大permutation;2. 确定改变位置之后,需要找到下一个最大的值与i-1位置的值交换,这里注意,是要找到比nums[原创 2020-12-07 13:42:30 · 153 阅读 · 0 评论 -
LeetCode 287. Find the Duplicate Number
Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.There is only one duplicate number in nums, return this duplicate number.Follow-ups:How can we prove that at least one duplicate number must ex原创 2020-12-04 13:58:11 · 145 阅读 · 0 评论 -
LeetCode3. Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without repeating characters.Example 1:Input: s = “abcabcbb” Output: 3 Explanation: The answer is “abc”, withthe length of 3. Example 2:Input: s = “bbbbb” Output: 1 Explanation: The answer is原创 2020-12-03 11:26:33 · 85 阅读 · 0 评论 -
Algorithms Part II Maxflow 笔记
MinCut初始化是一个edge-weighted digraph, 有一个源点是s, 和目标点t;st-cut(cut)/ 割:将digraph的点分为两个不相连的集合,s在一个集合A,t在一个集合B;Capacity 从集合A到B的所有edge的容量总和;mincut : 拥有最小capacity的割问题;MaxFlowflow : 流,1. 小于等于该边容量;2. 输入流和输出流必须相等(除了s和t,在有向图中,假设s只有输出,t只有输入);##最大流问题就是找到输入目标终点的最大流原创 2020-11-30 11:28:47 · 219 阅读 · 0 评论 -
300. Longest Increasing Subsequence
Given an integer array nums, return the length of the longest strictlyincreasing subsequence.A subsequence is a sequence that can be derived from an array bydeleting some or no elements without changing the order of theremaining elements. For example,.原创 2020-11-19 11:26:30 · 109 阅读 · 0 评论 -
Direct Graph
Topological Sort重置不含有循环的图, 使所有连接都指向上面。算法:后序遍历graph, 并插入到stack; stack将其方向调换。// postOrder : operate when the pointer leaves the node// a diagraph may have topological order if there is no directed cycle!!!class TopologicalSort { boolean[] marked; Sta原创 2020-11-05 11:00:47 · 302 阅读 · 0 评论 -
Princeton Algorithms. QuickSort
Algorithm打乱数组;随机挑选一个值(一般取第一个),使得它的左侧全部小于他,右侧全部大于他;(用两个指针,一个遍历找到小于它的,一个遍历找到大于它的, 如果发现未知反了,那么交换两个指针当前位置的值)将左右部分递归,得出有序数组Implementation:4. 找到索引位置private int partition(Comparable[] a, int lo, int hi) { int i = lo; int j = hi; while (true) { // 找到需原创 2020-10-15 12:28:11 · 127 阅读 · 0 评论 -
Princeton Algorithm ElementarySort
1. Selection SortAlgorithm:一个指针遍历数组,另一个指针遍历后面位置的值并找到最小值,与当前指针位置的值进行交换。时间复杂度O(N^2).public void selectionSort(int[] a) { int size = a.length; for (int i = 0; i < size; i++) { // 选择当前遍历到的点作为最小值 int min = i; // 然后遍历后面的数,找到最小值,并且替换到当前位置 for (int原创 2020-10-14 10:59:37 · 160 阅读 · 0 评论 -
Leetcode.993 Cousins in BST
In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.Two nodes of a binary tree are cousins if they have the same depth, but have different parents.We are given the root of a binary tree with unique values, an原创 2020-10-05 12:18:23 · 114 阅读 · 0 评论 -
Leetcode 78 子集 & 90 子集II & leetcode 46全排列 回溯算法总结
78.Given a set of distinct integers, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.Example:Input: nums = [1,2,3]Output:[[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[]]暴力解求子集,先加一个空集,.原创 2020-09-24 20:04:03 · 143 阅读 · 0 评论 -
LeetCode 694不同岛屿数量
Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.Count the number of distinct islands. A.原创 2020-09-17 13:56:39 · 611 阅读 · 0 评论 -
LeetCode 216组合总和III
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。说明:所有数字都是正整数。解集不能包含重复的组合。示例 1:输入: k = 3, n = 7输出: [[1,2,4]]示例 2:输入: k = 3, n = 9输出: [[1,2,6], [1,3,5], [2,3,4]]来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/combination-sum-iii著作权归领原创 2020-09-11 16:49:12 · 143 阅读 · 0 评论 -
LeetCode 257 二叉树的所有路径
给定一个二叉树,返回所有从根节点到叶子节点的路径。说明: 叶子节点是指没有子节点的节点。示例:输入:1/ 2 35输出: [“1->2->5”, “1->3”]解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->31.DFS(递归)class Solution{ public List<String> binaryTreePaths(TreeNode root) { List<String> r原创 2020-09-04 10:48:54 · 97 阅读 · 0 评论 -
LeetCode 200岛屿数量 (UnionFind)
跟完CS61B的UnionFind不久,所以看到这题第一反应是并查集解。给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。岛屿总是被水包围,并且每座岛屿只能由水平方向或竖直方向上相邻的陆地连接形成。此外,你可以假设该网格的四条边均被水包围。示例 1:输入: [ [‘1’,‘1’,‘1’,‘1’,‘0’], [‘1’,‘1’,‘0’,‘1’,‘0’],[‘1’,‘1’,‘0’,‘0’,‘0’], [‘0’,‘0’,‘0’,‘0’,‘0’] ]输出: 1原创 2020-08-23 21:12:20 · 129 阅读 · 0 评论 -
CS61B Tries 听课笔记
**1.Basic Trie Impletation**Public class TrieSet{ private static final int R =128; //ASCII private NOde root; public class DataIndexedCharMap<V> { private V[] items; public DataIndexedCharMap(int R) { items = (V[]) new Object原创 2020-08-17 16:36:38 · 247 阅读 · 0 评论 -
cs 61b Heaps and PQs
The priority Queue: Allowing tracking and removal of the smallest item in a priority queue.找到每天最大的几条信息,下面是直接的方法public List<String> unharmoniousTexts(Sniffer sniffer, int M) { ArrayList<String>allMessages = new ArrayList<String>();原创 2020-08-12 10:38:56 · 201 阅读 · 0 评论 -
leetcode 9.回文(easy)
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。示例 1:输入: 121输出: true示例 2:输入: -121输出: false解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。示例 3:输入: 10输出: false解释: 从右向左读, 为 01 。因此它不是一个回文数。1.暴力解,转化为StringBuilder直接翻转class Solution { public boolean原创 2020-08-04 16:12:47 · 95 阅读 · 0 评论 -
CS61B discussion3 1.2 翻转列表 同leetcode206
反转一个单链表。示例:输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL进阶:你可以迭代或递归地反转链表。你能否用两种方法解决这道题?来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/reverse-linked-list著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。1.首先用迭代;class Solution {原创 2020-07-16 12:49:21 · 191 阅读 · 0 评论 -
CS 61B week 2 extraIntListPractice
/** Returns an IntList identical to L, but with * each element incremented by x. L is not allowed * to change. */ public static IntList incrList(IntList L, int x) { if (L == null ){ return null;// }else {原创 2020-07-12 16:53:21 · 644 阅读 · 0 评论 -
初学者CS61B week 2笔记
IntList————public class IntLIst(){ public int first;// 声明变量,类型是int public IntList rest;//类型是intlist public IntList(int f, IntList r){ First=f; rest=r; //constructor,初始化变量 } }实现size和iterativesize方法public int原创 2020-07-12 15:45:16 · 501 阅读 · 3 评论 -
CS106A boolean
/* bollean 只返回true&flase*/public boolean IsEven(int n) { if (n & 2 == 0) { return ture; } else{ return false;` }}//或者可以简化写成//public boolean xxx(int balabla){ return balabla + 6 //if condition// //条件达原创 2020-07-12 22:16:28 · 165 阅读 · 0 评论