• 【面试题01】找出数组中的最长前缀


    题目1:如图,finally中的输出语句会执行吗?(另外自己去考虑虚拟机退出、catch中抛异常、try中抛异常、守护线程等相关问题)

    题目2:Byte+"hello"报错吗?Byte+7报错吗?
    不会报错. 除了null以外,字符串与任何值相加都是将别的值转为字符串,然后相加, null没有toString函数,因此不可以!
    在这里插入图片描述

    题目3:找出数组中的最长前缀
    输入String[] arr,[“flow”, “flower”, “flag”]
    输出"fl"

    算法代码如下:

    public static String getMaxLengthPrefix(String[] arr) {
            // 空则返回空. 应对arr = []或者arr = null情况
            if (arr == null || arr.length == 0) {
                return "";
            }
    
            // 空字符串数组则返回"", 应对最短字符串为空情况. arr = ["flow", "", "floor"], 这种也会返回""
            for (String s : arr) {
                if (s == null || s.length() == 0) {
                    return "";
                }
            }
    
            // 其他正常情报. 即arr中最短字符串一定包含字符. arr = ["a", "aid", "architecture"]
            int index = 0;
            boolean flag = true;
            while (flag) {
    
                for (String s : arr) {
                    // 判断index位置上是否索引越界. 以及判断字符是否相等
                    // 1. 索引越界即退出, 并记录位置. 2. 任何数组中的字符串的第index位不与第一位相等即退出, 并记录位置.
                    if (s.length() <= index || arr[0].charAt(index) != s.charAt(index)) {
                        flag = false;
                        index--;
                        break;
                    }
                }
    
                index++;
            }
    
            return arr[0].substring(0, index);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述
    全部代码如下:

    public class TestDemo01 {
        public static void main(String[] args) {
            // 测试finally是否最终会执行
            System.out.println(testReturn());
    
            // 测试最长前缀是否寻找OK
            System.out.println(getMaxLengthPrefix(new String[]{"flow", "flight", "floor"}));
            System.out.println(getMaxLengthPrefix(new String[]{"", "", ""}));
            System.out.println(getMaxLengthPrefix(new String[]{"a", "aid", "architecture"}));
            System.out.println(getMaxLengthPrefix(new String[]{"architecture", "aid", "a"}));
    
            // 测试Byte分别与字符串和数字加是否会报错
            testByte();
    
            // 可以转换成功, 因为除了null以外,任何值+字符串都是字符串,任意值会调用toString方法.
            System.out.println("aaa"+new TestDemo01());
        }
    
        public static String testReturn () {
            try {
                System.out.println("我丢");
                return "halou";
            } catch (Exception e) {
                System.out.println("异常了");
            } finally {
                System.out.println("finally是否会执行");
            }
    
            return "我搞";
        }
    
        public static String getMaxLengthPrefix(String[] arr) {
            // 空则返回空. 应对arr = []或者arr = null情况
            if (arr == null || arr.length == 0) {
                return "";
            }
    
            // 空字符串数组则返回"", 应对最短字符串为空情况. arr = ["flow", "", "floor"], 这种也会返回""
            for (String s : arr) {
                if (s == null || s.length() == 0) {
                    return "";
                }
            }
    
            // 其他正常情报. 即arr中最短字符串一定包含字符. arr = ["a", "aid", "architecture"]
            int index = 0;
            boolean flag = true;
            while (flag) {
    
                for (String s : arr) {
                    // 判断index位置上是否索引越界. 以及判断字符是否相等
                    // 1. 索引越界即退出, 并记录位置. 2. 任何数组中的字符串的第index位不与第一位相等即退出, 并记录位置.
                    if (s.length() <= index || arr[0].charAt(index) != s.charAt(index)) {
                        flag = false;
                        index--;
                        break;
                    }
                }
    
                index++;
            }
    
            return arr[0].substring(0, index);
        }
    
        public static void testByte() {
            Byte b1 = 7;
            System.out.println(b1 + 14);
    
            Byte b2 = 7;
            System.out.println(b2+"我丢~");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
  • 相关阅读:
    iOS App Store上传项目报错 缺少隐私政策网址(URL)解决方法
    uImage的制作过程详解
    linux系统-----------搭建LNMP 架构
    Java从入门到精通-数组(二)
    Spring Boot 实现参数校验基本使用
    Oracle 数据库相关操作记录
    解析java中的debug模式之异常断点调试
    基于一种新颖强化学习 (RL) 的电机控制与电力驱动控制研究(Python代码实现)
    2022就业季|Spring认证教你,如何使用 Spring 构建 REST 服务(三)
    MySQL之窗口函数
  • 原文地址:https://blog.csdn.net/qq_34291570/article/details/134257086