Skip to content

Instantly share code, notes, and snippets.

View alonWang's full-sized avatar
😁

alonwang alonWang

😁
View GitHub Profile
@alonWang
alonWang / easyLexer.go
Last active May 20, 2022 20:06
easy lexer
package main
/*
简单的词法分析器演示
number: [0-9]+
identifier: [a-zA-Z_]
*/
/*
0 start
1 identifer
@alonWang
alonWang / Integer.java
Created January 5, 2021 09:24
数字的二进制标识中,左侧有多少个0. JDK实现
public static int numberOfLeadingZeros(int i) {
// HD, Count leading 0's
if (i <= 0)
return i == 0 ? 32 : 0;
int n = 31;
if (i >= 1 << 16) { n -= 16; i >>>= 16; }
if (i >= 1 << 8) { n -= 8; i >>>= 8; }
if (i >= 1 << 4) { n -= 4; i >>>= 4; }
if (i >= 1 << 2) { n -= 2; i >>>= 2; }
return n - (i >>> 1);
@alonWang
alonWang / TimeUtil.java
Created January 5, 2021 08:30
判断传入时间与今天是否是同一天的高效实现
package com.github.alonwang.util;
import com.google.common.collect.Range;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
/**
@alonWang
alonWang / SpuriousWakeUp.java
Created July 20, 2019 05:45
Java Thread Spurious WakeUp Demo
/**
* @description: 参考了 https://www.jianshu.com/p/da312eee4ac4
* @author: alonwang
* @create: 2019-07-19 15:54
**/
public class SpuriousWakeUp {
private final Object lock = new Object();
private int product = 0;
//如果没有产品,在lock对象上等待唤醒,如果有产品,消费.
private Runnable consumer = () -> {
/**
* 将一维数组看做一颗二叉树 父节点和子节点的关系为 Nk~=N2k+1,N2k+2
* 1. 构建堆
* 2. 调整堆
*/
public class HeapSort {
public static void main(String[] args) {
int[] arr = new int[]{1, 5, 4, 65, 3};
heapSort(arr);
for (int n : arr) {
@alonWang
alonWang / QuickSort.java
Created May 6, 2019 01:46
quick sort实现
package com.company;
import java.util.Arrays;
public class QuickSort {
public static void sort(Integer[] arrays, int low, int high) {
if (high <= low) {
return;
}
int parationIdx = paration(arrays, low, high);
@alonWang
alonWang / gist:17c6f23d51622c9e4ef330b298f465bc
Created March 28, 2019 03:09
Spring中获取targetClass
AopUtils.getTargetClass
@alonWang
alonWang / MapUtil.java
Created January 16, 2019 08:00
map的lambda封装尝试
public class MapUtil {
/**
* 如果map中存在key,将其转化后进行处理
*
* @param map
* @param key
* @param mapper
* @param consumer
* @param <K>
* @param <V>
@alonWang
alonWang / binaryTree.go
Last active December 4, 2018 07:26
binary tree implement
package main
import "fmt"
type BinaryTree struct {
head *node
}
func NewBinaryTree(value int) BinaryTree {
return BinaryTree{&node{val: value}}
@alonWang
alonWang / gist:92b338ed2dc0e6e4117d4298bd1216c2
Created September 4, 2018 02:44
利用sed和find修改当前目录下所有txt文件
sed -i "s/old/new/" `find . -name '*.txt'`