Skip to content

Instantly share code, notes, and snippets.

View longluo's full-sized avatar
💭
I may be slow to respond.

Frank Luo longluo

💭
I may be slow to respond.
View GitHub Profile
@longluo
longluo / prime.java
Last active June 14, 2022 06:11
Prime Number
/**
* 判断数字是否是一个质数
*
* check is a prime number
*
*/
boolean isPrime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
@longluo
longluo / Fibonacci.java
Created June 14, 2022 03:13
Fibonacci Algorithm
‎‎​
@longluo
longluo / BinarySearch.java
Last active June 14, 2022 06:11
BinarySearch 二分查找
/**
* Binary Search
* 二分查找
*
* Returns the index of the specified target in the specified array.
*
* @param arr the array of integers, must be sorted in ascending order
* @param target the search target
* @return index of key in array {@code arr} if present; {@code -1} otherwise
*/
@longluo
longluo / Sorting.java
Last active June 14, 2022 06:10
Sorting 排序
‎‎​
@longluo
longluo / FFT.cpp
Last active June 17, 2022 07:04
Fast Fourier Transform FFT 快速傅里叶变换
‎‎​
@longluo
longluo / Regex 常用正则表达式.md
Last active June 15, 2022 03:59
Regex 常用正则表达式

常用正则表达式

一、校验数字的表达式

  1. 数字:^[0-9]*$

  2. n位的数字:^\d{n}$

  3. 至少n位的数字:^\d{n,}$

@longluo
longluo / KMP.java
Last active June 15, 2022 03:32
KMP 字符串匹配算法
// KMP time: O(n+m) space: O(m)
public int KMP(String haystack, String needle) {
if (needle == null || needle.length() == 0) {
return 0;
}
int sLen = haystack.length();
int pLen = needle.length();
int[] next = new int[pLen];
@longluo
longluo / BitManipulation.java
Last active June 14, 2022 06:08
Bit Manipulation 位运算
/**
* The Lower 1 of the bits.
* @param num
* @return
*/
public static int lowBit(int num) {
return num & (-num);
}
@longluo
longluo / Newton.java
Last active June 14, 2022 06:11
Newton 牛顿迭代法求根
// 牛顿迭代法求某个数平方根
// Newton
public static int mySqrt_newton(int x) {
if (x == 0) {
return 0;
}
double C = x;
double x0 = x;
while (true) {
@longluo
longluo / gcd.java
Last active June 14, 2022 03:26
GCD 最大公约数(greatest common divisor)
// GCD Recursion
public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
// GCD Iteration
public int gcd_iter(int a, int b) {
int remain = 1;
while (remain != 0) {
remain = a % b;