- 博客(206)
- 收藏
- 关注
原创 【无标题】
使用Function<?super T,?extends V>可以灵活地传入多种Function,增强了泛型方法的通用性和适应性。在这个例子中,我们成功地用处理了Dog和Cat实例,实现了灵活的类型匹配。
2024-11-07 20:59:24
221
2
原创 多线程反转数组
1. 比如25个数5个线程,则线程 1 依次反转 0 <-> 24 (n-index-1), 5 <-> 19(n-index-1) , 10 <-> 14, 15 <-> 9 , 20 <-> 4。线程3: 2 <-> 22 . . . . .,线程5: 4 <-> 21 . . . . .线程2:1 <-> 23, . . . . ,线程4:3 <-> 22, . . . . ,2. 对于剩余线程和 1 同理。简单快速的数组反转。
2023-05-26 10:38:44
163
原创 字节面试题,数组A中给定可以使用的1~9的数,返回由A数组中的元素组成的小于n的最大数。例如A={1, 2, 4, 9},x=2533,返回2499
字节面试题,数组A中给定可以使用的1~9的数,返回由A数组中的元素组成的小于n的最大数。例如A={1, 2, 4, 9},x=2533,返回2499,暴力递归和双指针方法,正确定经过了验证
2023-03-27 12:35:49
596
原创 关于java中的并集,交集和差集
1. 对于List和set的规则几乎是一样的,甚至list和Set之间可以相互做集合运算现有的规则如下:以下数据均是在A={1, 2, 3} , B={2 ,3, 4}的情况下生成1.1 并集A.addAll(B) 结果:{1, 2, 3, 2, 3, 4}1.2 交集A.retainAll(B) 结果 {2, 3}1.3 差集A.removeAll(B) 结果:{1}...
2021-05-19 14:57:36
207
原创 线程中的join()和 wait()以及notify()和notifyAll()的调用
1.join()https://www.baeldung.com/java-thread-join2. wait(),notify(),notifyAll()2.1 官方的解释https://www.baeldung.com/java-wait-notify#3-waitlong-timeout-int-nanos2.2 一些疑问?为什么wait()需要while 循环https://blog.youkuaiyun.com/csp_6666/article/details/1035470482.
2021-05-16 23:34:32
161
原创 关于扑克牌把最上面的牌移到最下面,扔出下面一张牌是A,接着把现在最上面的牌移到最下面扔出一张牌是2以此类推扔出所有牌顺序是A,2,3,4....K
代码编写如下,采用了模拟的方法package Demo1;import java.util.*;class Node{ int val; int rank; public Node(int rank,int val) { this.rank=rank; this.val=val; }}public class Pukepai { public static void main(String[] arg.
2020-12-04 15:27:50
601
原创 按照层次遍历建立一个二叉树
package com.atguigu.springboot.bean.binary;import sun.reflect.generics.tree.Tree;import java.util.*;class TreeNode{ TreeNode left; TreeNode right; int val; TreeNode(int val) { this.val=val; }}public class CreateTwo.
2020-09-17 11:26:27
762
原创 Comparable和CompareTo还有Comparator
这几个概念老是记不住,每次使用都要百度真是难受特将作用记下来。1、Comparable 是一个接口,Arrays类中的 sort 方法承诺可以对对象数组进行排序,但要求满 足下列前提:对象所属的类必须实现了 Comparable 接口,下面是 Comparable 接口的代码:public interface Comparable { int compareTo(Object other);} 在调用 X.c0mpareT0(y) 的时候,这个 compareTo方法必须确实比较两个对象
2020-09-13 21:22:48
503
原创 rand5() 转换为 rand7()
package bishi;import java.util.Random;public class Rand5toRand7 { public static void main(String[] args) { for(int i=0;i<100;i++) { System.out.println(rand7()); } } static Random rand=new Random();.
2020-09-11 11:18:44
172
原创 java 层次遍历二叉树,并层次访问
package com.atguigu.springboot.bean.binary;import sun.reflect.generics.tree.Tree;import java.util.*;class TreeNode{ TreeNode left; TreeNode right; int val; TreeNode(int val) { this.val=val; }}public class CreateTwo.
2020-09-11 09:35:19
186
原创 未排序数组连续子数组的和为给定值的最大长度
前缀和就可以解决问题了。import java.util.HashMap;import java.util.Map;public class _01_Array_maxLength { public int maxLength(int[] arr, int k) { if (arr == null || arr.length == 0) { return 0; } Map<Integer, .
2020-08-30 14:37:36
159
原创 牛顿法和二分法开平方以及保存格式处理
package test;import java.util.Scanner;public class sqrt { public static void main(String[] args) { Scanner in = new Scanner(System.in); double m = in.nextDouble(); System.out.println(dichotomy(m)); System.out.prin.
2020-08-20 15:39:26
136
原创 leetcode 191竞赛
5425. 切割后面积最大的蛋糕矩形蛋糕的高度为 h 且宽度为 w,给你两个整数数组 horizontalCuts 和 verticalCuts,其中 horizontalCuts[i] 是从矩形蛋糕顶部到第i 个水平切口的距离,类似地, verticalCuts[j] 是从矩形蛋糕的左侧到第 j 个竖直切口的距离。请你按数组 horizontalCuts 和 verticalCuts 中提供的水平和竖直位置切割后,请你找出 面积最大 的那份蛋糕,并返回其 面积 。由于答案可能是一个很大的...
2020-06-01 07:30:43
366
转载 最长回文子串
https://www.jianshu.com/p/a7741619dd58 public String longestPalindrome(String s) { if (s.isEmpty()) { return s; } int n = s.length(); boolean[][] dp ...
2020-04-26 12:14:12
149
原创 56.合并区间
package ag;import java.util.Arrays;public class MergeD { public static void main(String[] args) { int [] a={9,10,1,4,3,6,8,12}; int m=a.length/2; int [][] matrix=new ...
2020-04-24 12:16:14
151
转载 236. 二叉树的最近公共祖先
递归:非递归:class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { // Stack for tree traversal Deque<TreeNode> stack = new ArrayD...
2020-04-23 14:04:09
178
原创 美团2020,4.16笔试第三题
作者:左程云链接:https://www.nowcoder.com/discuss/410116?type=0&order=0&pos=21&page=1来源:牛客网import java.util.Arrays;import java.util.Comparator;public class Code01_KthMinPair { public sta...
2020-04-21 00:20:08
1049
原创 240. 搜索二维矩阵 II
暴力搜索:class Solution {public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int m = matrix.size(); if (m == 0) return false; int n = matri...
2020-02-11 18:34:07
123
原创 238. Product of Array Except Self
class Solution { public int[] productExceptSelf(int[] nums) { int n=nums.length; int [] fwd=new int[n]; int [] bwd=new int[n]; int [] res=new int...
2020-01-08 17:38:43
99
原创 235. Lowest Common Ancestor of a Binary Search Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pub...
2020-01-08 14:47:51
95
转载 233. Number of Digit One
class Solution { public int countDigitOne(int n) { //remain 当前位的数字,coef=10^(n-1) int count=0,coef=1,previous=0; while(n>0) { int remain=n...
2020-01-05 19:47:17
118
原创 230. Kth Smallest Element in a BST
递归:class Solution{ int count=0; int res=0; public int kthSmallest(TreeNode root, int k) { if(root.left!=null) kthSmallest(root.left,k); count++; if(count==k)...
2019-12-30 22:04:36
79
原创 leetcode 229摩尔选举法
class Solution { public List<Integer> majorityElement(int[] nums) { int cnt1=0.cnt2=0,a=0,b=0; List<Integer> res=new ArrayList<>(); int n=nums.length; ...
2019-12-30 20:18:25
141
原创 227. Basic Calculator II
class Solution{ public int calculate(String s) { Stack<Integer> stack=new Stack<>(); int res=0; int num=0; int i=0; char op='+'; while(i<s.len...
2019-12-25 21:27:48
72
原创 leetcode226
递归算法:class Solution { ArrayDeque<TreeNode> q=new ArrayDeque<>(); public TreeNode invertTree(TreeNode root) { if(root!=null) { TreeNode leftNode=root.left;...
2019-12-23 21:47:52
81
原创 225. Implement Stack using Queues
class MyStack { /** Initialize your data structure here. */ private ArrayDeque<Integer> q1=new ArrayDeque<>(); private ArrayDeque<Integer> q2=new ArrayDeque<>();...
2019-12-22 21:30:59
60
转载 threejs Object的点击(鼠标)事件(获取点击事件的object)
objects=[];raycaster = new THREE.Raycaster();mouse = new THREE.Vector2();//监听全局点击事件,通过ray检测选中哪一个objectdocument.addEventListener("mousedown", (event) => { event.preventDefault(); this.mouse...
2019-12-16 23:01:05
2286
1
原创 216. Combination Sum III
class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> res=new ArrayList<>(); List<Integer> tmp=new A...
2019-12-05 10:16:33
68
转载 [LeetCode] 212. Word Search II
class Solution { class TrieNode{ TrieNode [] children=new TrieNode[26]; String word; } public TrieNode buildTree(String [] words) { TrieNode root=new...
2019-11-30 16:23:50
84
原创 211. Add and Search Word - Data structure design
class WordDictionary { /** Initialize your data structure here. */ TrieNode root; public WordDictionary() { root = new TrieNode(); } /** Adds a word into the data ...
2019-11-24 20:35:18
80
原创 210. Course Schedule II
class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { ArrayList<ArrayList<Integer>> graph=new ArrayList<>(); int [] in=new int[...
2019-11-24 17:49:28
78
原创 209. Minimum Size Subarray Sum
笨方法:class Solution { public int minSubArrayLen(int s, int[] nums) { if(nums.length==0) return 0; int l=0,r=1,minLength=10000000,currlength; if...
2019-11-24 15:34:14
117
原创 207. Course Schedule
public class Solution { public boolean canFinish(int numCourses, int[][] prerequisites) { if (numCourses < 0 || prerequisites == null) return false; if (prerequisites.length ==...
2019-11-03 16:51:24
79
原创 IDEA编写scala使用maven打包,出现的一些坑,编写一个wordcount程序
被这个死问题整整折磨了两个小时,首先我们来看一下如何建立一个使用maven的scala工程(虽然个人觉得sbt更好用)新建项目后点击右侧scala选项,选择左侧面板的IDea建立好项目输入项目名称,建立好项目之后可以右键项目名称(此时选择Project模式 ) 会出来一个点击Add Framework support... ,选择maven之后,新建scala文件夹,右键scala文件夹选择M...
2019-11-03 10:24:01
502
原创 204. Count Primes
public class Solution { public int countPrimes(int n) { boolean[] notPrime = new boolean[n]; int count = 0; for (int i = 2; i < n; i++) { if (notPrime[i] ==...
2019-10-17 10:53:09
79
原创 202. Happy Number
class Solution { public boolean isHappy(int n) { HashSet<Integer> set=new HashSet<>(); while(n!=1) { int sum=0; while...
2019-10-16 22:13:39
69
原创 200. Number of Islands
class Solution { public int numIslands(char[][] grid) { int m= grid.length; if(m==0) return 0; int n= grid[0].length; int count=0; for(int i=0;i&l...
2019-10-16 21:33:52
64
原创 189. Rotate Array
第一种:class Solution { public void rotate(int[] nums, int k) { int [] afternum=new int[nums.length]; int len=nums.length; if(k>=nums.length)...
2019-10-16 16:18:15
78
原创 187. Repeated DNA Sequences
class Solution { public List<String> findRepeatedDnaSequences(String s) { HashSet<String> res=new HashSet<>(); HashSet<String> tempset=new ...
2019-10-15 11:34:24
85
原创 173. Binary Search Tree Iterator
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class BSTIterator { ...
2019-10-13 17:16:31
71
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人