leetcode-1534. 统计好三元组
leetcode-1534. 统计好三元组
原始思路
1234567891011121314151617181920212223242526class Solution { List<Integer> list = new ArrayList<>(); List<List<Integer>> result = new ArrayList<>(); public int countGoodTriplets(int[] arr, int a, int b, int c) { dfs(0, arr, a, b, c); return result.size(); } public void dfs(int cur, int[] arr, int a, int b, int c){ if (list.size() == 3){ int i = list.get(0); ...
leetcode-1544. 整理字符串
leetcode-1544. 整理字符串
1234567891011121314151617181920212223242526272829303132333435363738394041class Solution { public String makeGood(String s) { int n = s.length(); ArrayList<Character> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(s.charAt(i)); } while (judge(list)) { for (int i = 1; i < list.size(); i++) { Character pre = list.get(i - 1); ...
acwing-853. 有边数限制的最短路
acwing-853. 有边数限制的最短路
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import java.util.*;class Main{ static int N = 10010; //存放图数据 static Node[] node = new Node[N]; //定义距离 static int[] dist = new int[N]; static int inf = 1000000000; public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nex ...
leetcode-501. 二叉搜索树中的众数
leetcode-501. 二叉搜索树中的众数
原始思路
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647class Solution { Map<Integer, Integer> map = new HashMap<>(); public int[] findMode(TreeNode root) { if (root == null) { return new int[]{}; } dfs(root); //记录最大值 int max = Integer.MIN_VALUE; for(Map.Entry<Integer, Integer> entry: map.entrySet()){ max = Math.max( ...
acwing-850. Dijkstra求最短路 II
acwing-850. Dijkstra求最短路 II
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import java.util.*;class Main{ // 定义最大数用于初始化数组 static int N = 1000010; // 定义邻结表 h存储边头结点(即一个边节点的index),e存储边结点,w存储边的权值,ne存储下一个边结点, static int[] h = new int[N]; static int[] e = new int[N]; static int[] w = new int[N]; static int[] ne = new int[N]; // 定义距离 static int[] dist = new int[N]; // 定义标记已经访问过的数组 ...
leetcode-1042. 不邻接植花
leetcode-1042. 不邻接植花
原始思路
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960class Solution { public int[] gardenNoAdj(int N, int[][] paths) { int[] result = new int[N]; if (paths.length == 0) { Arrays.fill(result, 1); return result; } Map<Integer, Set<Integer>> map = new HashMap<>(); for(int[] path: paths){ // 正向 ...
leetcode-997. 找到小镇的法官
leetcode-997. 找到小镇的法官
原始思路
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748class Solution { public int findJudge(int N, int[][] trust) { // 首先找出谁都不信任的人 int n1 = trust.length; Map<Integer, List<Integer>> map = new HashMap<>(); for (int i = 0; i < n1; i++) { int[] arr = trust[i]; if (map.containsKey(arr[0])){ List<Integer> curList = map.get(arr[ ...
leetcode-904. 水果成篮
leetc0de-904. 水果成篮
原始思路:使用滑动窗口
123456789101112131415161718192021222324252627class Solution { public int totalFruit(int[] tree) { int n = tree.length; if (n == 0) { return 0; } // k记录种类 List<Integer> list = new ArrayList<>(); HashSet<Integer> set = new HashSet<>(); int i = 0; int j = 0; int result = 0; while (i < n && j < n) { list.add(tree ...
leetcode-617. 合并二叉树
leetcode-617. 合并二叉树
原始思路
12345678910111213141516class Solution { public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { if (t1 != null && t2 != null) { TreeNode node = new TreeNode(); node.val = t1.val + t2.val; node.right = mergeTrees(t1.right, t2.right); node.left = mergeTrees(t1.left, t2.left); return node; } else if (t1 != null) { return t1; } else if (t2 != null) ...
acwing-849. Dijkstra求最短路 I
acwing-849. Dijkstra求最短路 I
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import java.util.*;class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[][] g = new int[501][501]; int[] dist = new int[501]; boolean[] st = new boolean[501]; //初始化g for(int i = 1; i < g.length; i++){ ...