Java 笔试复盘 (2021-03-25)

这篇博客探讨了如何在Java中判断一个数组是否包含另一个数组作为连续子序列,以及如何实现两个数字字符串的十进制加法。首先,通过将数组转换为字符串并使用`lastIndexOf`方法来检查子数组的存在性。其次,通过遍历字符串并处理进位来实现字符串数字的加法,确保在没有额外库支持的情况下正确执行运算。
摘要由CSDN通过智能技术生成
  • 编写一个函数,对传入的两个数组,判断第二个数组是否为第一个数组的子数组(要连续比对)。如果不是,返回 -1; 如果是,则返回在第一个数组中最大的下标。

    public static int sub(int[]A, int[]B){
    	// add your code here
    }
    
    Example:
    System.out.println(sub(new int[] {4,5,6,7,5,6,8}, new int[] {5,6}));	// 4
    System.out.println(sub(new int[] {4,5,7,5,8}, new int[] {5,6}));	// -1
    System.out.println(sub(new int[] {4,5,6,7,5,6,8}, new int[] {6}));	// 5
    System.out.println(sub(new int[] {4,5,6,7,5,6,8}, new int[] {4,5,6}));	// 0
    

    解题:

    public class Solution {
        public static void main(String[] args) {
            System.out.println(sub(new int[]{4,5,6,7,5,6,8}, new int[]{5,6}));// 4
            System.out.println(sub(new int[]{4,5,7,5,8}, new int[]{5,6}));// -1
            System.out.println(sub(new int[]{4,5,6,7,5,6,8}, new int[]{6}));// 5
            System.out.println(sub(new int[]{4,5,6,7,5,6,8}, new int[]{4,5,6}));// 0
        }
        public static int sub(int[]A, int[] B){
            String s1 = arr2String(A);
            String s2 = arr2String(B);
            return s1.lastIndexOf(s2);
        }
        private static String arr2String(int[]A){
            StringBuilder sb = new StringBuilder();
            for (int i: A  ) {
                sb.append(i);
            }
            return sb.toString();
        }
    }
    
  • 编写一个函数实现对两个数字字符串进行十进制的加法操作,并返回求和结果,如果出错清返回“ERROR”。不能使用 BigInteger 等 java 库中的工具类。

public static String add(String numberA, String numberB){
	// add your code here
}
Example:
System.out.println(add("123456789123456789123456789", "123456789123456789123456789"));	246913578246913578246913578
System.out.println(add("1", "
2"));	// 3
System.out.println(add("A", "123456789123456789123456789")); 	// ERROR

解题:

public class Solution {
    public static void main(String[] args) {
        System.out.println(add("123456789123456789123456789", "123456789123456789123456789"));//246913578246913578246913578
        System.out.println(add("1", "2"));//3
       System.out.println(add("A", "1"));// ERROR
    }

    public static String add(String numberA, String numberB){
        int index = 0;
        int other = 0; // 进位
        StringBuilder sb = new StringBuilder();

        // 从个位开始相加
        while(index < Math.max(numberA.length(), numberB.length() )) {
            // 计算 A B 字符串的倒数位置
            int a = numberA.length() - 1 - index;
            int b = numberB.length() - 1 - index;

            // 计算 A B 字符串倒数位置上的值,如果位置为 -1,则指默认补 0, 如果转换异常,则返回 Error
            int numA = 0;

       int numB = 0;
            try {
                numA = a >= 0 ? Integer.valueOf( String.valueOf(numberA.charAt(a))) : 0; // numberA 个位
                numB = b >= 0 ? Integer.valueOf( String.valueOf(numberB.charAt(b))) : 0; // numberB 个位
            }catch (NumberFormatException e){
                return "ERROR";
            }
            int sum =  numA+ numB + other;

            other = sum / 10;
            int num = sum % 10;

            sb.append(num);

            index ++;
        }
        return sb.reverse().toString();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HolaSecurity

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值