算法刷题常用操作

输入输出

Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
    String str = scanner.nextLine();
    System.out.println(str);
}

数据结构

        Stack stack = new Stack();
        stack.push(1);
        stack.push(2);
        System.out.println(stack.empty());
        System.out.println(stack.size());
        //peek查看栈顶元素,不删除
        //pop弹出栈顶元素
        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.size());
    }

普通队列

Queue queue = new LinkedList();
queue.offer(1);//入队列
queue.offer(2);//入队列
queue.offer(3);//入队列
System.out.println(queue.isEmpty());
System.out.println(queue.peek());//获取队头元素,但是不出队列
System.out.println(queue.poll());//出队列
System.out.println(queue.poll());//出队列
System.out.println(queue.poll());//出队列

双端队列

Deque deque = new LinkedList<>();
deque.isEmpty();//判断空
deque.peekFirst();//访问队头数据
deque.peekLast();//访问队尾数据
deque.addFirst(2);
deque.addLast(1);//在队尾插入数据
deque.removeLast();//移除队尾数据
deque.removeFirst();//移除队头数据

数组和List转换

//创建list
        List<Integer> lists = Arrays.asList(1, 2, 3, 4);

        //数组转list
        int[] num = {1, 2, 3, 4};
        //int转list
        List<Integer> collect = Arrays.stream(num).boxed().collect(Collectors.toList());
        //list排序
        Collections.sort(collect);
        System.out.println("collect排序后:" + collect);//collect排序后:[1, 2, 3, 4]
        Collections.reverse(collect);
        System.out.println("collect反转后:" + collect);//collect反转后:[4, 3, 2, 1]

        //字符串数组转list
        String[] strArr = new String[] {"1", "2", "3", "4"};
        List<String> strings = Arrays.asList(strArr);

        System.out.println(strings.subList(1, 2));//[ )   -->2

        //list转数组
        List<Integer> list2 = new ArrayList<>();
        list2.add(1);
        list2.add(2);
        list2.add(3);

        //注意 list.toArray只能转object
        Object[] objects = list2.toArray();
        System.out.println(Arrays.toString(objects));//[1, 2, 3]
        //转为对应类型的arr
        Integer[] intArr = new Integer[list2.size()];
        Integer[] integers = list2.toArray(intArr);
        System.out.println(Arrays.toString(integers));//[1, 2, 3]

        //list转基础类型
        List<String> list4 = Arrays.asList("1", "3", "5");
        int[] ints = list4.stream().mapToInt(Integer::new).toArray();

        //数组排序
        int[] num3 = {1, 2, 4, 1, 5, 6, 23};
        Arrays.sort(num3);
        System.out.println(Arrays.toString(num3));//[1, 1, 2, 4, 5, 6, 23]

字符串

//String遍历
        String str = "abcdefg-hijk";
        for (int i = 0, len = str.length(); i < len; i++) {
            char charAt = str.charAt(i);
            System.out.print(charAt);//abcdefg-hijk
        }

        System.out.println("============");

        char[] chars = str.toCharArray();
        System.out.println(Arrays.toString(chars));//[a, b, c, d, e, f, g, -, h, i, j, k]

        //截取
        String str2 = "0123456";
        System.out.println(str2.substring(5));//[ ]56
        System.out.println(str2.substring(5, 6));//[  )5

        //拼接,作为一个新str,所以下面不包含
        System.out.println(str2.concat("a"));//0123456a
        //包含
        System.out.println(str2.contains("a"));//false
        String str3 = str2.concat("a");
        System.out.println(str3.contains("a"));//true

        //比较,字典序,从0个开始,一个一个比较差值,返回第一个不同的差值
        System.out.println("abc".compareTo("aab"));

        //  正则,符合a-z的字母
        String str4 = "abc";
        System.out.println(str4.matches("[a-z]*"));
        //反转
        StringBuilder builder = new StringBuilder();
        StringBuilder append = builder.append(str4);
        System.out.println(append.reverse());//cba

进制转换

 //16进制数字
        String str="5B";
        //转10进制,第二个参数是输入参数是多少进制的
        Integer integer10 = Integer.valueOf(str, 16);
        System.out.println("10进制="+integer10);//10进制=91
        //通过10进制再转其他进制
        //2进制
        System.out.println("2进制="+Integer.toBinaryString(integer10));//2进制=1011011
        //8进制
        System.out.println("8进制="+Integer.toOctalString(integer10));//8进制=133
        //16进制
        System.out.println("16进制="+Integer.toHexString(integer10));//16进制=5b

Math数学计算

    float numf=1.35F;
        double numd=3.14;//默认D

        //向上取整
        System.out.println("向上取整="+(int)Math.ceil(numf));//向上取整=2
        //向下取整
        System.out.println("向下取整="+(int)Math.floor(numf));//向下取整=1
        //四舍五入
        System.out.println("四舍五入="+(int)Math.round(numf));//四舍五入=1
        //次方
        System.out.println("次方="+(int)Math.pow(2,3));//次方=8

//        7 abs()
//        返回参数的绝对值。
//        8	ceil()
//        返回大于等于( >= )给定参数的的最小整数,类型为双精度浮点型。
//        9	floor()
//        返回小于等于(<=)给定参数的最大整数 。
//        10	rint()
//        返回与参数最接近的整数。返回类型为double。
//        11	round()
//        它表示四舍五入,算法为 Math.floor(x+0.5),即将原来的数字加上 0.5 后再向下取整,所以,Math.round(11.5) 的结果为12,Math.round(-11.5) 的结果为-11。
//        12	min()
//        返回两个参数中的最小值。
//        13	max()
//        返回两个参数中的最大值。
//        14	exp()
//        返回自然数底数e的参数次方。
//        15	log()
//        返回参数的自然数底数的对数值。
//        16	pow()
//        返回第一个参数的第二个参数次方。
//        17	sqrt()
//        求参数的算术平方根。

character

char charStr = 'a';
        System.out.println("是字母:" + Character.isLetter(charStr));
        System.out.println("是数字:" + Character.isDigit(charStr));
        System.out.println("是空格:" + Character.isWhitespace(charStr));
        System.out.println("是大写:" + Character.isUpperCase(charStr));
        System.out.println("是小写:" + Character.isLowerCase(charStr));
        System.out.println("转大写:" + Character.toUpperCase(charStr));
        System.out.println("转string:" + Character.toString(charStr));
        //        是字母:true
        //        是数字:false
        //        是空格:false
        //        是大写:false
        //        是小写:true
        //        转大写:A
        //        转string:a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值