一、Java实现单链表反转操作
public class Node<V> {
V value;
Node next;
}
public Node reverse(Node node){
if(node==null || node.next==null){
return node;
}
Node pre=null;
Node curr=node;
Node next=null;
while(curr!=null){
next=curr.next;
curr.next=pre;
pre=curr;
cur=next;
}
return pre;
}
二、单例模式(既是线程安全的,又支持延迟加载)
public class Singleton{
private Singleton(){}
/**
3 * 类级的内部类,也就是静态的成员式内部类,该内部类的实例与外部类的实例
4 * 没有绑定关系,而且只有被调用到才会装载,从而实现了延迟加载
5 */
private static class SingletonHolder{
/**
8 * 静态初始化器,由JVM来保证线程安全
9 */
private static class SingletonHolder {
private static final Singleton singleton = new Singleton();
}
public static Singleton getInstance(){
return SingletonHolder.instance;
}
}
三、给定一个排序数组和一个目标值,找出数组中所有的两个数的和等于目标数
public void getSumNum(int[] nums,int targert){
Arrays.sort(nums);
int start =0; int end=nums.length-1;
int sum= 0;
while(start<=end){
sum= nums[0]+nums[end];
if(sum==targrt){
System.out.print(start,end);
}else if(sum<target){
start++;
}else{
end--;
}
}
}
四、Java实现二分查找算法
public int binarySearch(int[] nums,int target){
if(nums==null){ return -1}
Arrays.sort(nums)
int low=0; int high=nums.length-1;
int middle=0
if(nums[low]>target || nums[end]<high || low<high){ return -1}
while(low<high){
middle=(low+high)/2
if(num[middle]>target){
high=middle-1;
}else if(num[middle]<target){
low=middle+1;
}else{
return middle;
}
}
}
}
五、Java实现裴波那切数组
//裴波那契数列的定义:他的第一项和第二项均为1,以后各项都为前两项的和。如:
1,1,2,3,5,8,13,21,34,55,89,144,233,~~~~
public static int printNum(int k){
if(k==1||k==2){
return 1;
}else{
int [] a = new int[k+1];
a[0]=1;
a[1]=1;
for(int i=2;i<=k;i++){
a[i]=a[i-1]+a[i-2];
}
return a[k-1];
}
}
六、java 两个有序数组合并成一个有序数组算法(归并算法思想)
int indexM=0; int indexN=0;
int[] arrayM=new int[]{1,4,6,7,8};
int[] arrayN=new int[]{2,3,5,9,11};
int k=0;
int[] res=new int[arrayM.length+arrayN,length];
while(indexM<arrayM,length&&indexN<indexN.length){
if(arrayM[indexM]<arrayN[indexN]){
res[k]=arrayM[indexM];
indexM++;
}else{
res[k]=arrayN[indexN];
indexN++;
}
k++;
}
if(indexM!=arrayM.length){
for(int i=indexM;i<arrayM.length;i++){
res[k]=arrayM[i]
k++;
}
}else{
// 此时 a =indexM,并a++,将M数组中剩余值添加到res中;
for(int a = indexN ;a<arrayN.length;a++){
res[k]=arrayN[a];
k++;
}
}
for(int a =0;a<k;a++){ System.out.println(res[a]); }
七、Java实现二叉树的遍历(非递归方式实现)博客地址:https://blog.csdn.net/wang454592297/article/details/79472938
// 用非递归的方法进行先序遍历
public void qinaxuFeiDigui(TreeNode treeNode) {
Stack<TreeNode> stack = new Stack<TreeNode>();
while (treeNode != null || !stack.isEmpty()) {
while (treeNode != null) {
qianxuNumList.add(treeNode.val);
stack.push(treeNode);
treeNode = treeNode.left;
}
if(!stack.isEmpty()){
treeNode = stack.pop();
treeNode = treeNode.right;
}
}
}
八、Java多线程交替打印A和B
class PrintA{
boolean nowA=true;
syncronized void printA(){
while(!nowA){
try{
this.wait();
}catch(Exeception e){
}
nowA=false;
system.out.println("A")
}
}
synchronized void printB(){
while(nowA){
try {
this.wait();
} catch (Exception e) {
}
}
nowA=true;
System.out.println(Thread.currentThread()+" "+"B");
this.notify();
}
public class Test {
public static void main(String[] args){
Print print=new Print();
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<10;i++){
print.printA();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<10;i++){
print.printB();
}
}
}).start();;
}
九、Java实现生产者和消费者模式
public class Storage {
// 仓库容量
private final int MAX_SIZE = 10;
// 仓库存储的载体
private LinkedList<Object> list = new LinkedList<>();
public void produce() {
synchronized (list) {
while (list.size() + 1 > MAX_SIZE) {
System.out.println("【生产者" + Thread.currentThread().getName()
+ "】仓库已满");
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.add(new Object());
System.out.println("【生产者" + Thread.currentThread().getName()
+ "】生产一个产品,现库存" + list.size());
list.notifyAll();
}
}
public void consume() {
synchronized (list) {
while (list.size() == 0) {
System.out.println("【消费者" + Thread.currentThread().getName()
+ "】仓库为空");
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.remove();
System.out.println("【消费者" + Thread.currentThread().getName()
+ "】消费一个产品,现库存" + list.size());
list.notifyAll();
}
}
}
}
public class Producer implements Runnable{
private Storage storage;
public Producer(){}
public Producer(Storage storage , String name){
this.storage = storage;
}
@Override
public void run(){
while(true){
try{
Thread.sleep(1000);
storage.produce();
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
public class Consumer implements Runnable{
private Storage storage;
public Consumer(){}
public Consumer(Storage storage , String name){
this.storage = storage;
}
@Override
public void run(){
while(true){
try{
Thread.sleep(3000);
storage.consume();
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
Storage storage = new Storage();
Thread p1 = new Thread(new Producer(storage));
Thread p2 = new Thread(new Producer(storage));
Thread p3 = new Thread(new Producer(storage));
Thread c1 = new Thread(new Consumer(storage));
Thread c2 = new Thread(new Consumer(storage));
Thread c3 = new Thread(new Consumer(storage));
p1.start();
p2.start();
p3.start();
c1.start();
c2.start();
c3.start();
}
}