2026/5/18 6:16:31
网站建设
项目流程
网页架构,苏州seo网站系统,安卓软件开发用什么软件,wordpress 谷歌搜索题目链接#xff1a;3606. 优惠券校验器#xff08;简单#xff09; 算法原理#xff1a; 解法#xff1a;模拟 击败47.54% 时间复杂度O(Nlogn) 这题的思路非常简单#xff0c;但是实现起来比较麻烦#xff0c;感觉应该算个中等题#xff0c;主要就是考察排序 记忆3606. 优惠券校验器简单算法原理解法模拟击败47.54%时间复杂度O(Nlogn)这题的思路非常简单但是实现起来比较麻烦感觉应该算个中等题主要就是考察排序记忆1. 字符串比compareTo ✅什么词序、字典序、名称排序都用它2. 数字比用包装类的 compare ✅Integer.compare(a, b)、Double.compare(a, b)3. 其他基本类型直接相减 ✅char1 - char2、int1 - int2小数值可用Java代码class Solution { public ListString validateCoupons(String[] c, String[] b, boolean[] isActive) { int nc.length; ListString retnew ArrayList(); String[][] tmpnew String[n][2]; for(int i0;in;i){ if(isc(c[i])isb(b[i])isActive[i]){ tmp[i][0]c[i]; tmp[i][1]b[i]; }else{ //标记为无效记录防止空指针报错 tmp[i][0]; tmp[i][1]; } } Arrays.sort(tmp,(x,y)-{ //处理空记录空记录排在最后 if(x[0].equals()y[0].equals()) return 0; if(x[0].equals()) return 1;//x为空x在后面 if(y[0].equals()) return -1;//y为空x在前面 //先比较品牌首字符 int tmpretCharacter.compare(x[1].charAt(0),y[1].charAt(0)); if(tmpret!0) return tmpret; //品牌首字母相同再比较代码 return x[0].compareTo(y[0]); }); for(String[] t:tmp){ if(t[0].equals()) continue; ret.add(t[0]); } return ret; } private boolean isc(String s){ for(char c:s.toCharArray()){ if(!(cacz)!(cAcZ) !(c0c9)!(c_)) return false; } return true; } private boolean isb(String s){ return (s.equals(electronics)||s.equals(grocery)||s.equals(pharmacy)||s.equals(restaurant)); } }