虽然这种练习是确保在编程竞赛中提高性能的唯一方法,但掌握一些技巧可以确保优势和快速调试。 1) 在不使用%运算符的情况下检查数字是偶数还是奇数: 虽然这个技巧并不比使用%运算符好多少,但有时是有效的(对于大数字)。
使用和操作员:
System.out.println((a & 1) == 0 ? "EVEN" : "ODD" );
例子:
num = 5 Binary: "101 & 1" will be 001, so false num = 4 Binary: "100 & 1" will be 000, so true.
2) 2的快速乘法或除法
乘以2意味着将所有位向左移位,除以2意味着向右移位。
例子: 2(二进制10):向左移动4(二进制100)和向右移动1(二进制1)
n = n << 1; // Multiply n with 2n = n >> 1; // Divide n by 2
3) 使用异或交换2个数字:
这种方法速度快,不需要使用第三个变量。
// A quick way to swap a and b a ^= b; b ^= a; a ^= b;
4) 更快的I/O:
参考 在这里 用于java中的快速I/O
5) 对于字符串操作:
使用 字符串缓冲区 用于字符串操作,如 一串 在java中是不变的。参考 在这里 .
6) 计算最高有效位:
要计算任何数字的最高有效位,可以直接使用日志来计算它。
Suppose the number is N then Let double K = Math.log10(N);now K = K - Math.floor(K);int X = (int)Math.pow(10, K);X will be the most significant digit.
7) 直接计算位数:
为了计算数字中的位数,我们可以高效地使用log,而不是循环:
No. of digits in N = Math.floor(Math.log10(N)) + 1;
8) 内置GCD方法:
Java在中内置了GCD方法 大整数类 .它返回一个大整数,其值是abs(this)和abs(val)的最大公约数。如果this==0&&val==0,则返回0。
语法:
public BigInteger gcd(BigInteger val)Parameters :val - value with which the GCD is to be computed.Returns :GCD(abs(this), abs(val))
以下是GCD的实施情况:
JAVA
// Java program to demonstrate how // to use gcd method of BigInteger class import java.math.BigInteger; class Test { public static int gcd( int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd( long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } // Driver method public static void main(String[] args) { System.out.println(gcd( 3 , 5 )); System.out.println(gcd(10000000000L, 600000000L)); } } |
1200000000
9) 检查质数:
Java在中内置了isProbablePrime()方法 大整数类 .如果这个大整数可能是素数(有一定的把握),则返回true;如果它肯定是复合的,则返回false。
BigInteger.valueOf(1235).isProbablePrime(1)
10) 知道一个数字是否是2的幂的有效技巧 :
通常的除法复杂度是O(logN),但可以用O(v)来解决,其中v是二进制形式的数字的位数。
JAVA
/* Method to check if x is power of 2*/ static boolean isPowerOfTwo ( int x) { /* First x in the below expression is for the case when x is 0 */ return x!= 0 && ((x&(x- 1 )) == 0 ); } |
11) 排序算法:
对于基元,使用数组。sort()使用 双支点快速排序 算法。
12) 搜索算法:
- 数组。二进制搜索()( 第一组 | SET2 )用于对排序数组应用二进制搜索。
- 收藏。二进制搜索() 用于基于比较器对集合应用二进制搜索。
13) 复制算法:
- 数组。copyOf()和copyOfRange() 复制指定的数组。
- 收藏。复印件() 复制指定的集合。
14) 旋转和频率
我们可以使用 收藏。轮换 将集合或数组旋转指定距离。你也可以使用 收藏。频率() 方法获取集合或数组中指定元素的频率。
15) 大多数数据结构已经在 集合框架 .
16) 使用 包装类 用于获取数字的基数转换的函数 有时需要对数字进行基数转换。为此,可以使用包装器类。
JAVA
// Java program to demonstrate use of wrapper // classes for radix conversion class Test { // Driver method public static void main(String[] args) { int a = 525 ; long b = 12456545645L; String binaryA = Integer.toString(a, 2 ); System.out.println( "Binary representation" + " of A : " + binaryA); String binaryB = Long.toString(b, 2 ); System.out.println( "Binary representation" + " of B : " + binaryB); String octalA = Integer.toString(a, 8 ); System.out.println( "Octal representation" + " of A : " + octalA); String octalB = Long.toString(b, 8 ); System.out.println( "Octal representation" + " of B : " + octalB); } } |
Binary representation of A : 1000001101Binary representation of B : 1011100110011101111100110101101101Octal representation of A : 1015Octal representation of B : 134635746555
17) NullPointerException(为什么?) 参考 在这里 和 在这里 为了避免它。
本文由 高拉夫·米格拉尼 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。