leetcode-783. 二叉搜索树节点最小距离
783. 二叉搜索树节点最小距离
123456789101112131415161718192021222324252627class Solution { int ans = Integer.MAX_VALUE; List<Integer> list = new ArrayList<>(); public int minDiffInBST(TreeNode root) { dfs(root); for(int i = 1; i < list.size(); i++){ ans = Math.min(ans, list.get(i) - list.get(i-1)); } return ans == Integer.MAX_VALUE ? 0 : ans; } public void dfs(TreeNode root){ if(root == null){ ...
acwing-95. 费解的开关
acwing-95. 费解的开关
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import java.util.*;public class Main{ static int[][] arr = new int[5][5]; static int[][] backup = new int[5][5]; public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i = 0; i < n; i++){ for(int k = 0; k < 5; k++) ...
leetcode-179. 最大数
179. 最大数
123456789101112131415161718192021222324class Solution { public String largestNumber(int[] nums) { Queue<Integer> queue = new PriorityQueue<>((o1,o2)->{ String num1 = "" + o1 + o2; String num2 = "" + o2 + o1; return num2.compareTo(num1); }); for(int i = 0; i < nums.length; i++){ queue.offer(nums[i]); } StringBuffer buffer = new StringBuffe ...
leetcode-面试题 17.21. 直方图的水量
面试题 17.21. 直方图的水量
12345678910111213141516171819202122232425262728class Solution { public int trap(int[] height) { int ans = 0; int n = height.length; if(n == 0){ return ans; } // 初始化左边最大 int[] leftMax = new int[n]; leftMax[0] = height[0]; for(int i = 1; i < n; i++){ leftMax[i] = Math.max(leftMax[i-1], height[i]); } // 初始化右边最大 int[] rightMax = new int[n]; ...
leetcode-154. 寻找旋转排序数组中的最小值 II
154. 寻找旋转排序数组中的最小值 II
12345678910111213141516171819class Solution { public int findMin(int[] nums) { int i = 0; int j = nums.length - 1; while(i < j){ int mid = (i+j)/2; if(nums[mid]>nums[j]){ i = mid + 1; }else if(nums[mid] == nums[j] && nums[i] >= nums[j]){ i = i + 1; }else if(nums[mid] == nums[j] && nums[i] <nums[j]){ ...
leetcode-153. 寻找旋转排序数组中的最小值
153. 寻找旋转排序数组中的最小值
123456789101112131415161718class Solution { public int findMin(int[] nums) { int i = 0; int j = nums.length - 1; while(i <= j){ if (i == j) { break; } int mid = (i+j)/2; if(nums[mid] > nums[j]){ i = mid + 1; }else{ j = mid; } } return nums[i]; }}
leetcode-81. 搜索旋转排序数组 II
81. 搜索旋转排序数组 II
1234567891011121314151617181920212223242526272829303132333435363738class Solution { public boolean search(int[] nums, int target) { if(nums.length == 0){ return false; } // 比较前后两个数 int frist = nums[0]; int last = nums[nums.length-1]; if(target == frist || target == last){ return true; } if(target > frist){ // 从左到右查找 for(int i = 1; i < nu ...
leetcode-781. 森林中的兔子
781. 森林中的兔子
1234567891011121314151617181920212223242526272829class Solution { public int numRabbits(int[] answers) { int ans = 0; if(answers.length == 0){ return 0; } Arrays.sort(answers); int k = 0; for(int i = 0; i < answers.length; i++){ if(k == 0){ ans += answers[i] + 1; k = answers[i]; continue; } if(answers[i] == answer ...
leetcode-80. 删除有序数组中的重复项 II
80. 删除有序数组中的重复项 II
1234567891011121314151617181920212223242526272829303132class Solution { public int removeDuplicates(int[] nums) { int n = nums.length; if(n < 3){ return n; } int last = nums[nums.length - 1]; int i = 0; for(i = 2; i < n; i++){ if(nums[i] < nums[i-1]){ break; } if(nums[i-1] == nums[i] && nums[i] == nums[i- 2]){ ...
acwing-94. 递归实现排列型枚举
acwing-94. 递归实现排列型枚举
12345678910111213141516171819202122232425262728293031323334353637383940414243import java.util.*;import java.io.*;public class Main{ static List<List<Integer>> res = new ArrayList<>(); static Set<Integer> set = new HashSet<>(); static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); int n = ...