算法

1.计算单一字符字串个数

count=n*(n+1)/2

注:n为字符串的长度

比如"aaa"  n=3 count =6   a 3个  aa 2个  aaa 1个  3+2+1=6

2.

闰年是公历中的名词。闰年分为普通闰年和世纪闰年,闰年的定义:

普通闰年:公历年份是 4 的倍数,且不是 100 的倍数的,为闰年(如 2004 年就是闰年)。

世纪闰年:公历年份是整百数的,必须是 400 的倍数才是世纪闰年(如 1900 年不是世纪闰年,2000 年是世纪闰年)。

闰年是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。补上时间差的年份为闰年。闰年共有 366 天( 1-12 月分别为 31 天,29 天,31 天,30 天,31 天,30 天,31 天,31 天,30 天,31 天,30 天,31 天)

闰年的判断方法:四年一闰,百年不闰,四百年再闰,即:

(Y % 100 != 0 && Y % 4 == 0) || Y % 400 == 0

(Y % 100 != 0 and Y % 4 == 0) or (Y % 400 == 0)

3.

int[][] items

// 整理 items

        // 对于 id 从小到大排序:Arrays.sort(items)

        // 对于分数,从大到小排序

        Arrays.sort(items, (a, b) -> (a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]));

4.list:

从大到小排序:Collections.sort(list, Collections.reverseOrder());

从小到大排序:Collections.sort(list);

5.t

int[] t

从小到大排序: Arrays.sort(t);

从大到小排序:

Arrays.sort(t, new Demo2());

class Demo2 implements Comparator<Integer> {
    /**
     * 从写compare方法,默认从小到大排序,更改后从大到小排序
     *
     * @param o1
     * @param o2
     * @return
     */
    @Override
    public int compare(Integer o1, Integer o2) {
        // 默认是o1 < o2时返回-1, 一下同理
        if (o1 > o2) {
            return -1;
        }
        if (o1 < o2) {
            return 1;
        }
        return 0;
    }
6.获取list的最小值

Collections.min(list)

获取一定list中最贴近指定值target的最小值

 Collections.min(list,new Comparator<Integer>(){

            @Override

            public int compare(Integer o1,Integer o2){

                return Math.abs(o1-target)<Math.abs(o2-target)?-1:1;

            }

        });

 

7.new StringBuffer(str).reverse() 

用来对字符串反转,如StringBuffer x=new StringBuffer('helloworld'), 使用Reverse方法后获得新字符串'dlrowolleh'

8.Character.isDigit判断字符是否为数字

9.创建一个队列,但是是排序的

int[][] intervals

PriorityQueue<Integer> allocator=new PriorityQueue<Integer>(intervals.length,new Comparator<Integer>(){

            public int compare(Integer a,Integer b){

                return a-b;

            }

        });

10.给二维数组按0下标排序:

int[][] intervals

Arrays.sort(intervals,new Comparator<int[]>(){

            public int compare(final int[] a, final int[] b) {

            return a[0] - b[0];

            }

        });

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值