Java算法小技巧

字符串逆序

private static String reverse(String str) {
    if (str == null) {
        return null;
    }
    return new StringBuffer(str).reverse().toString();
}

整数逆序排序

Arrays.sort(v,1,k+1, Comparator.reverseOrder());

大根堆

Queue<Integer> queue = newPriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> queue = newPriorityQueue<>((o1, o2)->o2.compareTo(o1));
PriorityQueue<Integer> queue = newPriorityQueue<>(newComparator<Integer>() { 
    @Overridepublicintcompare(Integer o1, Integer o2) { 
        return o2.compareTo(o1); 
    } 
})

大整数

//大整数赋初值
BigInteger x_1=BigInteger.ONE;
BigInteger x_2=BigInteger.TEN;
BigInteger x_3=BigInteger.ZERO;
//compareTo数字比较
BigInteger num1 = new BigInteger("123456789012345678901234567890");
BigInteger num2 = new BigInteger("987654321098765432109876543210");

// 使用 compareTo 方法比较两个大整数的大小
int result = num1.compareTo(num2);

if (result < 0) {
    System.out.println("num1 小于 num2");
} else if (result > 0) {
    System.out.println("num1 大于 num2");
} else {
    System.out.println("num1 等于 num2");
}

进制转换

public class Main {
	public static void main(String[] args) {
		int n = 18;
		Integer.toHexString(n);
		System.out.println(n + "的二进制是:" + Integer.toBinaryString(n));
		System.out.println(n + "的八进制是:" + Integer.toOctalString(n));
		System.out.println(n + "的十六进制是:" + Integer.toHexString(n));
		System.out.println(n + "的三进制是:" + Integer.toString(n, 3));
	}
}

字符串

字符串匹配:regionMatches,不如哈希
public boolean regionMatches(int toffset,
                             String other,
                             int ooffset,
                             int len)

或
public boolean regionMatches(boolean ignoreCase,
                             int toffset,
                             String other,
                             int ooffset,
                             int len)

参数

  • ignoreCase -- 如果为 true,则比较字符时忽略大小写。
  • toffset -- 此字符串中子区域的起始偏移量。
  • other -- 字符串参数。
  • ooffset -- 字符串参数中子区域的起始偏移量。
  • len -- 要比较的字符数。

返回值

如果字符串的指定子区域匹配字符串参数的指定子区域,则返回 true;否则返回 false。是否完全匹配或考虑大小写取决于 ignoreCase 参数。

一般情况下,够用了

public class Test {
        public static void main(String args[]) {
            String Str1 = new String("www.runoob.com");
            String Str2 = new String("runoob");
            String Str3 = new String("RUNOOB");
    
            System.out.print("返回值 :" );
            System.out.println(Str1.regionMatches(4, Str2, 0, 5));
    
            System.out.print("返回值 :" );
            System.out.println(Str1.regionMatches(4, Str3, 0, 5));
    
            System.out.print("返回值 :" );
            System.out.println(Str1.regionMatches(true, 4, Str3, 0, 5));
        }
    }
}
处理循环读入判空问题
while (true) {
    String line = br.readLine();
    if (line == null || line.isEmpty()) break; // 如果读取到的行为null或者为空,结束循环

    String[] st = line.split(" ");
    int x = Integer.parseInt(st[0]);
    list.add(f[x]);
}

数组遍历

在数组的遍历中,一定要考虑对数组边界的判断,如下,如果不加判断,结果无法输出,会报错

for(int i=1;i<=n;i++){
    int max=-1;
    int min=0x3f3f3f3f;
    for(int j=i;j>=0;j--){
        max=Math.max(max,a[j]);
        min=Math.min(min,a[j]);
        if(max-min==i-j) {
            if(j-1>=0)
                f[i] = (f[i] + f[j - 1]) % ANS;
        }
    }
}

区间连续判断

一段区间内的最大值减去最小值等于区间长度,则说明区间连续

求对数

先使用换底公式换一下底就行了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值