1370. Increasing Decreasing String

"本文解析了题目"IncreasingDecreasingString"的解题思路,介绍了两种不同的解决方法:一是使用StringBuilder和计数数组,二是利用TreeMap按顺序和逆序交替插入字符。两种方法展示了如何巧妙地利用数据结构进行字符串排序。"
摘要由CSDN通过智能技术生成

刷题笔记

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值