柳州做网站哪家好,网站建设服务费应该做到什么科目,创造软件的软件下载,自己创业做网站昨天做了美团的笔试#xff0c;现在复盘一下。
1、将数组按照绝对值大小排序 有道算法题解决思路需要将数组按照绝对值大小进行排序#xff0c;我使用的是sort方法Comparator比较器实现的#xff0c;这里记录一下#xff1a;
public static void main(String[] args) {In… 昨天做了美团的笔试现在复盘一下。
1、将数组按照绝对值大小排序 有道算法题解决思路需要将数组按照绝对值大小进行排序我使用的是sort方法Comparator比较器实现的这里记录一下
public static void main(String[] args) {Integer nums[] new Integer[10];nums[0] -3;nums[1] 5;nums[2] 4;nums[3] 1;nums[4] -2;nums[5] -5;nums[6] -4;nums[7] -1;nums[8] 3;nums[9] 2;System.out.println(Arrays.toString(nums));//排序前Arrays.sort(nums,new ComparatorInteger(){Overridepublic int compare(Integer o1, Integer o2) {return Math.abs(o1)-Math.abs(o2);}});System.out.println(Arrays.toString(nums));//排序后}
运行结果 2、String的matches()方法 有一道算法题需要判断字符串里出现的字符需要用到matches()方法这里补一下用法。
public static void main(String[] args) {//匹配字符String regex a....s; //若不限制a和s之间字符的数量可以写为String regex a.*s;System.out.println(abbbbs.matches(regex)); // trueSystem.out.println(alias.matches(regex)); // falseSystem.out.println(as.matches(regex)); // false//匹配数字String regex1 [24680]; //表示匹配一个或多个数字字符而[24680]表示只匹配一个数字字符。System.out.println(abc.matches(regex1)); // falseSystem.out.println(846.matches(regex1)); // trueSystem.out.println(98 4.matches(regex1)); // false,不允许空字符}