刷题笔记

1370. Increasing Decreasing String

题目(这是一道虚假的easy题)

Given a string s. You should re-order the string using the following algorithm:

Pick the smallest character from s and append it to the result.
Pick the smallest character from s which is greater than the last appended character to the result and append it.
Repeat step 2 until you cannot pick more characters.
Pick the largest character from s and append it to the result.
Pick the largest character from s which is smaller than the last appended character to the result and append it.
Repeat step 5 until you cannot pick more characters.
Repeat the steps from 1 to 6 until you pick all characters from s.
In each step, If the smallest or the largest character appears more than once you can choose any occurrence and append it to the result.

Return the result string after sorting s with this algorithm.

思路

说在前面,这道题对我来说完全不easy,我最开始并没能写出来,并且看大神写的答案都看了一会才看明白。

  1. 首先要用到String的toCharArray()方法,将string转化为一个字符数组。
  2. 然后对字符数组中的字符进行计数,计数方法是在一个26位的数组中在相应位置++。
  3. 创建一个StringBuilder,对StringBuilder进行顺序和逆序交替append,并用while循环进行约束,约束条件为StringBuilder与s的长度。
  4. 顺序逆序交替append实现的方法,是通过一个三目运算符,来分别实现顺序和逆序判断每个字母位置上有没有剩下的数,如果有,则在StringBuilder后面append相应的字符;如果没有,就继续找下一个位置。

代码实现

class Solution {public String sortString(String s) {StringBuilder ans = new StringBuilder();int[] count = new int[26];int n = s.length();for (char x : s.toCharArray()) {count[x - 'a']++;}while (ans.length() < n) {addToAns(count, ans, true);addToAns(count, ans, false);}return ans.toString();}public static void addToAns (int[] count, StringBuilder ans, boolean b) {for (int i = 0; i < 26; i++) {int index = b ? i : 25 - i;if (count[index]-- > 0) {ans.append((char)(index + 'a'));}}}
}

这种解法的速度太顶了。

其他解法:

 public String sortString(String s) {StringBuilder ans = new StringBuilder();TreeMap<Character, Integer> tm = new TreeMap<>();for (char c : s.toCharArray()) {tm.put(c, 1 + tm.getOrDefault(c, 0));}boolean asc = true;while (!tm.isEmpty()) {for (char c : asc ? new TreeSet<>(tm.keySet()) : new TreeSet(tm.descendingKeySet())) {ans.append(c);tm.put(c, tm.get(c) - 1);tm.remove(c, 0);}asc = !asc; // same as asc ^= true;}return ans.toString();}

解法思路

  1. Use TreeMap to count each char in s;
  2. Append to StringBuilder the keys in TreeMap in sorted order, decrease the count of each key, and remove the entry if reaching 0;
  3. Do the similar operation in step 2, but in descending order of the keys;
  4. Repeat 2 and 3 till the TreeMap is empty.

1370. Increasing Decreasing String相关推荐

  1. Leetcode 1370. Increasing Decreasing String

    Leetcode 1370. Increasing Decreasing String 题目链接: Increasing Decreasing String 难度:easy 题目大意: 按照题目要求对 ...

  2. 深入了解SpringCloud Hystrix

    雪崩效应即在多个服务节点当中,如果有一个服务不可用而这个不可用的服务导致整个应用资源都耗在这里,进而影响整个系统的崩溃.在分布式环境中,不可避免地会出现雪崩效应.Hystrix是一个netflix实现 ...

  3. 我和乘子交替方向法admm_找到最大和交替子序列

    我和乘子交替方向法admm Problem statement: 问题陈述: Given a sequence of numbers, you have to find the maximum sum ...

  4. Synopsys Sentaurus TCAD系列教程之-Tcl《2》

    Tool command language(Tcl) 2.基础 介绍对使用TCAD sentaurus工具最有用的基本Tcl命令 (本章节中用到的所有示例,请参考上一部门<1>的内容) 2 ...

  5. PBAS 背景建模源码浅析

    pbas.h #include <iostream> #include <cv.h> #include <highgui.h>#pragma once class ...

  6. 含最新数据! 使用Python检测新冠肺炎疫情拐点

    注:本文案例仅供技术学习,不代表研究性观点. 本文对应代码.数据及文献资料已上传至Github仓库https://github.com/CNFeffery/DataScienceStudyNotes ...

  7. 南加州大学机器视觉实验室_机器学习带动南加州爱迪生的变革

    南加州大学机器视觉实验室 By Tom Davenport* 汤姆·达文波特* Analytics are typically viewed as an exercise in data, softw ...

  8. python operator __gt___Python operator.gt方法代码示例

    本文整理汇总了Python中operator.gt方法的典型用法代码示例.如果您正苦于以下问题:Python operator.gt方法的具体用法?Python operator.gt怎么用?Pyth ...

  9. 在Azure Data Studio中探索SandDance可视化扩展

    Azure Data Studio is an open-source, cross-platform and lightweight data management tool. We can use ...

  10. 前景检测算法(九)--PBAS算法

     Pixel-Based Adaptive Segmenter(PBAS)检测算法,是基于像素的无参数模型,该算法结合了SACON和VIBE两个算法的优势,并在这两个算法的基础上改进而来,SACO ...

最新文章

  1. linux安装mysql5.6.26_linux mysql-5.6.26 安装
  2. 目标跟踪数据集OTB、VOT下载
  3. python关键字匹配_python通过BF算法实现关键词匹配的方法
  4. 华为鸿蒙系统首发设备,华为鸿蒙系统首发设备曝光!不是手机
  5. 蓝牙:为啥叫“蓝”牙,不叫“白”牙?
  6. Effective C++学习第二天
  7. 特征选择的基本方法概述
  8. 【在大学的快乐生活】锐捷校园网无感认证通过路由器mac地址克隆实现一账号多终端
  9. red5搭建流媒体直播系统
  10. VSS无法访问 (0x80072EFD) 转载
  11. 重启该计算机 选择操作系统,电脑为什么会自动重启 电脑经常自动重启修复方法...
  12. 关于“质量”概念的理解
  13. map返回另一个对象
  14. 在服务器上如何打开aspx文件,aspx是什么文件_aspx用什么软件打开
  15. 服务器怎么买,腾讯云服务器购买三种流程介绍
  16. logcat查询日志
  17. AppScan--图解web扫描工具IBM Security AppScan Standard
  18. 按键精灵--多点找形状介绍
  19. python 简单的Http服务器
  20. numpy.random.randint()函数生成随机坐标点

热门文章

  1. 自律给你自由——Android设计布局的新姿势
  2. mysql插入路径_Conventional-pathinsert(传统路径插入)
  3. Opencv中的颜色检测
  4. 帮助台湾?匿名者组织攻击联合国和世界卫生组织网站
  5. 揭开 Java 注解的神秘面纱
  6. 币种对应的转换因子(处理日元台币…
  7. Yigo平台环境配置——详细步骤
  8. android app怎么给界面加背景图,想做一个Android app,但是背景不能铺满整个页面,不知道大家是用什么样的图片?...
  9. Vmware安装CensOS6.4
  10. 计算机考研复试-计算机组成原理