当前位置:网站首页>2022.8.8-----leetcode.761
2022.8.8-----leetcode.761
2022-08-10 10:04:00 【Road Lu727】
public String makeLargestSpecial(String s) {//If 1 is (, 0 is), then s can be regarded as a legal bracket sequence with only one pair of brackets in the outermost layer#//Remove the outermost layer, it can be divided into several #, they belong to the same layer//One operation is to sort the # of the same layer to get the maximum resultif (s.length() == 0) return s;List list = new ArrayList<>();char[] cs = s.toCharArray();//Remove the outermost layer of the # of the previous layer, and divide it to get this layer#for (int i = 0, j = 0, k = 0; i < cs.length; i++) {k += cs[i] == '1' ? 1 : -1;//When k=0, get a complete#if (k == 0) {String next=makeLargestSpecial(s.substring(j + 1, i));//Remove the outermost layer and enter the next layer of sortinglist.add("1" + next + "0");//After sorting the next time, add the outermost layerj = i + 1;}}Collections.sort(list, (a, b)->(b + a).compareTo(a + b));//Sort this layerStringBuilder sb = new StringBuilder();for (String str : list) sb.append(str);//Splicing returnreturn sb.toString();}
边栏推荐
猜你喜欢
随机推荐
dedecms supports one-click upload of Word content
04 【计算属性 侦听器】
13 【script setup 总结】
The web project accesses static resources inside the reference jar
傅立叶级数与傅里叶变换
Matlab符号函数的生成及计算其函数值
效率开发目录
ESP8266-Arduino编程实例-MQ-8氢气传感器驱动
【物联网架构】什么是物联网平台?
细说Redis监控和告警
Fourier series and Fourier transform
JS高级 之 使用 Iterator - Generator
09 【Attributes继承 provide与inject】
CentOS和Ubantu的Mysql主从配置
数据库事务
交换 生成树 知识总结
keepalived:主备配置
解决问题目录
ESP8266-Arduino编程实例-MQ-7一氧化碳传感器驱动
武功修炼:内功