首页 > 其他分享 >【哈希表】LeetCode 767. 重构字符串

【哈希表】LeetCode 767. 重构字符串

时间:2023-04-26 16:57:57浏览次数:38  
标签:index temp int list 767 getValue 偶数 哈希 LeetCode

题目链接

767. 重构字符串

思路

先用哈希表统计出出现次数最多的字符,如果这个次数大于一半,说明这个字符总会挨在一起,直接返回 ""

如果不超过一半,则先把字符填在偶数位置(先填出现次数最多的字符),偶数位置填满了再填奇数位置。

代码

class Solution {
    public String reorganizeString(String s) {
        char[] chars = s.toCharArray();
        int n = chars.length;
        int threshold = (n + 1) / 2;
        HashMap<Character, Integer> hashMap = new HashMap<>();

        for(char c : chars){
            hashMap.put(c, hashMap.getOrDefault(c, 0) + 1);
        }
        ArrayList<Map.Entry<Character, Integer>> list = new ArrayList<>(hashMap.entrySet());
        Collections.sort(list, (a, b) -> (b.getValue() - a.getValue()));

        if(list.get(0).getValue() > threshold){
            return "";
        }

        char[] result = new char[n];

        Map.Entry<Character, Integer> temp = list.get(0);
        int index = 0;
        for(; index < n && temp.getValue() > 0; index += 2){
            result[index] = temp.getKey();
            temp.setValue(temp.getValue() - 1);
        }

        // 这里不重置 index,因为偶数位可能还没有被填完,所以需要接着填偶数位
        for(int j = 1; j < list.size(); j++){
            temp = list.get(j);
            while(temp.getValue() > 0){
                temp.setValue(temp.getValue() - 1);
                // 偶数位填完了,开始填奇数位
                if(index >= n){
                    index = 1;
                }
                result[index] = temp.getKey();
                index += 2;
            }
        }

        return new String(result);
    }
}

标签:index,temp,int,list,767,getValue,偶数,哈希,LeetCode
From: https://www.cnblogs.com/shixuanliu/p/17356532.html

相关文章

  • 【DP】LeetCode 740. 删除并获得点数
    题目链接740.删除并获得点数思路分析动态规划题目的时候只需要考虑最后一个阶段,因为所有的阶段转化都是相同的,考虑最后一个阶段容易发现规律在数组的动态规划问题中,一般dp[i]都是表示以nums以前i个元素组成(即nums[i-1])的状态;dp[i][j]分别表示以nums1前i个元素(......
  • LeetCode 152. 乘积最大子数组
    20230426顺利通过原题解题目约束题解classSolution{public:intmaxProduct(vector<int>&nums){intmaxF=nums[0],minF=nums[0],ans=nums[0];for(inti=1;i<nums.size();++i){intmx=maxF,mn=minF;......
  • 哈希表
    哈希表1.哈希表基本介绍散列表(HashTable,也叫哈希表),是根据关键码值(KeyValue)而直接进行访问的数据结构。也即是说,它通过把关键码值映射到表中的一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。哈希表的内存布局(数组+链表)2.......
  • [LeetCode] 2418. Sort the People
    Youaregivenanarrayofstrings names,andanarray heights thatconsistsof distinct positiveintegers.Botharraysareoflength n.Foreachindex i, names[i] and heights[i] denotethenameandheightofthe ith person.Return names sorted......
  • 【LeetCode动态规划#13】买卖股票含冷冻期(状态众多,比较繁琐)、含手续费
    最佳买卖股票时机含冷冻期力扣题目链接(opensnewwindow)给定一个整数数组,其中第i个元素代表了第i天的股票价格。设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):你不能同时参与多笔交易(你必须在再次购买前出售掉之前......
  • LeetCode 1643. 第 K 条最小指令
    康托展开一开始无脑枚举全排列,果断超时,还是得看看如果降低计算量。题目destination=[2,3],相当于2个V,3个H,输出全排列去重后的对应位置字典序列内容。忽略去重则问题为全排列,所有可能为:\[(\sumdestination)!=(2+3)!=5!\]k恰好为康托展开结果+1,直接逆向......
  • 算法学习day07哈希表part02-454、383、15、18
    packageLeetCode.hashpart02;importjava.util.HashMap;importjava.util.Map;/***454.四数相加II*给你四个整数数组nums1、nums2、nums3和nums4,数组长度都是n,请你计算有多少个元组(i,j,k,l)能满足:*0<=i,j,k,l<n*nums1[i]+nums2[j]+nums......
  • leetcode调研version0
     这是我第一次发博客,所以许多功能还不太会使用。前几次的随笔既当作记录,也当作自己的练习。 最近想要刷leetcode,纠结用哪种语言(我自己学过c/c++,python,fortran,Java),所以前期做了一些调研,在此记录一下。 c语言: 网址:https://github.com/begeekmyfriend/leetcode ......
  • leetcode 607 銷售員
    銷售員 selects.`name`fromsalespersonsleftjoinordersoons.sales_id=o.sales_idleftjoincompanycono.com_id=c.com_idandc.name='RED'groupbys.sales_idhavingcount(c.`name`)=0 ===selects.`name`fromsalespersonswheres.sa......
  • 【DP】LeetCode 213. 打家劫舍 II
    题目链接213.打家劫舍II思路分析动态规划题目的时候只需要考虑最后一个阶段,因为所有的阶段转化都是相同的,考虑最后一个阶段容易发现规律在数组的动态规划问题中,一般dp[i]都是表示以nums以前i个元素组成(即nums[i-1])的状态;dp[i][j]分别表示以nums1前i个元素(即n......