一个面试题:取出字符串中的整数放到数组中并排序,查找一个值

本文介绍了一个Java程序,该程序能从给定的字符串中提取所有整数,并使用插入排序算法进行排序。此外,还实现了二分查找算法来搜索特定的整数值。通过这个例子,读者可以了解到如何在Java中处理字符串、整数数组以及实现基本的排序和查找算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class Test1 {
        private static final Logger log = LoggerFactory.getLogger(com.xpfirst.hdrRouter.Test.Test1.class);

        public static void main(String[] args){

            String str = "sl3234ls12123679804jge";
            Integer czInt = 6;
            getIntAndSort(str,czInt);
        }
        //取出字符串中的整数放到数组中并排序,查找一个值
        public static void getIntAndSort(String str,Integer czInt) {
            // 取出整数
            List<Integer> list = new ArrayList<>();
            for(int i = 0; i < str.length(); i++) {
                String s = str.substring(i,i+1);
                try{
                    Integer tmpint = Integer.valueOf(s);
                    list.add(tmpint);
                }
                catch(Exception ex){}
            }
            // 插入排序
            int len = list.size();
            for(int i = 1; i < len; i++){
                for( int j = i; j>0 && list.get(j-1) > list.get(j); j--){
                    Integer tmpInt = list.get(j-1);
                    list.set(j-1,list.get(j));
                    list.set(j,tmpInt);
                }
            }
            // 二分查找

            System.out.print(list.toString());
            System.out.print(erfen(list,czInt));
        }
        /**
         * @description 二分查找
         * @author 郜金丹
         * @date 2019/1/16 13:42
         * @return Exception
        **/
        public static int erfen(List<Integer> list,Integer searchInt) {
            if (list.size() == 0){
                return -1;
            }
            int mod = list.size()/2;
            Integer tmpInt = list.get(mod);
            if (searchInt.equals(tmpInt)){
                return mod;
            }
            else if (searchInt > tmpInt){
                List<Integer> tmpList = list.subList(mod+1,list.size());
                return mod +1+ erfen(tmpList,searchInt);
            }
            else {
                List<Integer> tmpList = list.subList(0,mod);
                return erfen(tmpList,searchInt);
            }

        }

        /**
         * @description 插入排序
         * @date 2019/1/16 11:13
         * @return Exception
        **/
        public static void charu(int[] ints) {
            int len = ints.length;
            for(int i = 1; i < len; i++) {
                for (int j = i; j > 0 && ints[j-1] > ints[j]; j--){
                    int int1 = ints[j-1];
                    int int2 = ints[j];

                    ints[j] = int1;
                    ints[j-1] = int2;

                }
            }
        }

        /**
         * @description maopao
         * @date 2019/1/16 10:44
         * @param
         * @return Exception
        **/
        public static void maopao(int[] ints) {
            int len = ints.length;
            for(int i = 0; i < len; i++) {
                for (int j = 1; j < len - i; j++){
                    int int1 = ints[j-1];
                    int int2 = ints[j];
                    if (int1 > int2){
                        ints[j] = int1;
                        ints[j-1] = int2;
                    }
                }
            }
        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值