-
数字:
^[0-9]*$ -
n位的数字:
^\d{n}$ -
至少n位的数字:
^\d{n,}$
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 判断数字是否是一个质数 | |
| * | |
| * check is a prime number | |
| * | |
| */ | |
| boolean isPrime(int n) { | |
| for (int i = 2; i * i <= n; i++) { | |
| if (n % i == 0) { | |
| return false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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 | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * The Lower 1 of the bits. | |
| * @param num | |
| * @return | |
| */ | |
| public static int lowBit(int num) { | |
| return num & (-num); | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 牛顿迭代法求某个数平方根 | |
| // Newton | |
| public static int mySqrt_newton(int x) { | |
| if (x == 0) { | |
| return 0; | |
| } | |
| double C = x; | |
| double x0 = x; | |
| while (true) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; |
OlderNewer