📜  用于竞争性编程的Java技巧(适用于Java 8)

📅  最后修改于: 2021-06-26 13:50:06             🧑  作者: Mango

尽管这种做法是确保提高编程比赛的性能的唯一方法,但是掌握一些技巧可以确保较高的优势和快速的调试。
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 2
n = n >> 1;   // Divide n by 2

3)使用XOR交换2个数字:

此方法快速,不需要使用3rd变量。

// A quick way to swap a and b
  a ^= b;
  b ^= a;
  a ^= b; 

4)更快的I / O:

请参阅此处以获取Java的快速I / O

5)对于字符串操作:

使用StringBuffer进行字符串操作,因为Java中的String是不可变的。请参考这里。

6)计算最高有效位数:

要计算任何数字的最高有效位,可以直接使用log进行计算。

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在BigInteger类中内置了GCD方法。它返回一个BigInteger,其值是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));
    }
}


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);   
}


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);
    }
}


输出
1
200000000

9)检查素数:

Java在BigInteger类中具有内置的isProbablePrime()方法。如果此BigInteger可能是质数(可以肯定),则返回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)排序算法:

  1. Arrays.sort()用于对数组的元素进行排序。
  2. Collections.sort()用于对集合的元素进行排序。

对于基元,Arrays.sort()使用双重数据透视快速排序算法。

12)搜索算法:

  1. Arrays.binarySearch()(SET 1 | SET2)用于对排序后的数组应用二进制搜索。
  2. Collections.binarySearch()用于对基于比较器的集合进行二进制搜索。

13)复制算法:

  1. Arrays.copyOf()和copyOfRange()复制指定的数组。
  2. Collections.copy()复制指定的集合。

14)旋转和频率

我们可以使用Collections.rotate()将集合或数组旋转指定的距离。您还可以使用Collections.frequency()方法获取集合或数组中指定元素的频率。

15)大多数数据结构已经在Collections Framework中实现

例如Stack,LinkedList,HashSet,HashMaps,Heaps等。

16)使用Wrapper类函数获取数字的基数转换有时您需要数字的基数转换。为此,您可以使用包装器类。

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 : 1000001101
Binary representation of B : 1011100110011101111100110101101101
Octal representation of A : 1015
Octal representation of B : 134635746555

17)NullPointerException(为什么?)请在此处和此处避免。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。