leetcode 767. Reorganize String(重组字符串)

量子化学仿真软件:VASP_(9).磁性材料计算 在量子化学仿真软件 VASP 中,磁性材料的计算是一个复杂但重要的过程。通过正确设置磁性状态、分析磁性性质、优化计算参数以及研究磁性相变和磁动力学,可以更准确地描述和预测磁性材料的性能。希望本节的内容能够为研究磁性材料的科研人员提供有用的参考和指导。 阅读详情

在这里插入图片描述
重新排列字符串s中的字母,使得任意两个相邻的字母都不相同。

思路:

让相邻字母不同,能想到的办法是先把相同的字母排列,
然后在相同字母的缝隙中插入另一种字母。
比如"aab", 先把"a a"排出来,再在2个a的缝隙中插入b,得到"aba".

那么就需要统计每个字母出现的次数。
为了让能插入字母的缝隙,排列时中间空一位,也就是先把出现最多的字母放在偶数位,
如果不够放就折回来到奇数位,所以字母出现次数不能超过s长度的一半,
不然折回来就是相同字母了。字母的出现次数超过s一半的直接返回“”。

而且一定要先排出现最多的字母(可以试试example1中先排b)。

可以用一个优先队列按字母出现的次数从大到小排列。然后一一取出排列。
也可以只找到出现次数最多的字母,先排列,再直接按顺序排剩下的。

优先队列:

    public String reorganizeString(String s) {
        HashMap<Character,Integer> map = new HashMap<>();
        char[] chs = s.toCharArray();
        //按字母出现的次数倒序排列
        PriorityQueue<Character> pq = new PriorityQueue<>((a,b)->(map.get(b)-map.get(a)));
        char[] res = new char[s.length()];

        for(char c : chs) {
            map.put(c, map.getOrDefault(c, 0)+1);
        }
        pq.addAll(map.keySet());

        //出现频率最多的字母多于字符串长度的一半
        if(map.get(pq.peek()) > (s.length()+1)/2) return "";
        
        int i = 0;

        while(!pq.isEmpty()) {
            char c = pq.poll();
            for(int j = 0; j < map.get(c); j++) {
                if(i >= s.length()) i = 1;  //偶数位放满,开始放奇数位
                res[i] = c;
                i += 2;
            }
        }
        return new String(res);
    }

只先排出现次数最多的,剩下的按顺序排。此方法更快。

    public String reorganizeString(String s) {
        int[] cnt = new int[26];
        char[] chs = s.toCharArray();
        int n = chs.length;
        char[] res = new char[n];

        for(int i = 0; i < n; i++) cnt[chs[i]-'a']++;

        //找到出现次数最多的字母和次数
        int maxCnt = 0;
        int freqCh = 0;
        for(int i = 0; i < 26; i++) {
            if(cnt[i] > maxCnt) {
                maxCnt = cnt[i];
                freqCh = i;
            }
        }

        if(maxCnt > (n+1)/2) return "";
        //先排出现最多的字母,不然可能会出现奇数位和偶数位是同一字母的情况
        int idx = 0;
        while(cnt[freqCh] > 0) {
            res[idx] = (char)(freqCh + 'a');
            idx += 2;
            cnt[freqCh] --;
        }

        for(int i = 0; i < 26; i++) {
            while(cnt[i] > 0) {
                if(idx >= n) idx = 1;
                res[idx] = (char)(i+'a');
                idx += 2;
                cnt[i] --;
            }
        }
        return new String(res);
    }
EPLAN 电气制图(六):结构盒与设备管理器核心概念(基础知识选看) 结构盒是 EPLAN 中用于标识元件安装位置的逻辑容器,非实际设备或黑盒,仅用于原理图中划分功能区域或物理位置(如 “柜内”“柜外”)。标识符设置:在 “显示设备标识符” 中输入位置代号(如 “+ 柜外”),完整格式为 “= 高层代号 + 位置代号”;筛选与定位:通过 “筛选器” 按类别(如电机、接触器)快速查找设备,右键 “转到(图形)” 可高亮定位图纸中的元件;设备保护:重要设备可启用保护(【项目数据】→【设备】→【启用设备保护】),防止误删除(显示橙色圆圈标记)。 阅读详情

相关推荐

ControlCAN.dll开发适配USBCAN_E/2E_U设备

摘要:本文提供了USBCAN_E/2E_U设备的配置指南:1)安装专用驱动;2)获取最新的ControlCAN.dll文件;3)修改VCI调用顺序,新增VCI_SetReference函数设置波特率;4)使用工具计算波特率配置值,并给出了常见波特率对应的代码参考值(如500K对应0x060007)。配置过程强调使用特定工具计算精确的波特率参数,并提供了相关软件下载链接。

weixin_43969033的博客 387

LeetCode之重新排列字符串(五千四百七十二)

目录 题目 解题 方法一、直接法 方法二、转换法 题目 (原题链接:https://leetcode-cn.com/problems/shuffle-string/) 给你一个字符串 s 和一个 长度相同 的整数数组 indices 。 请你重新排列字符串 s ,其中第 i 个字符需要移动到 indices[i] 指示的位置。 返回重新排列后的字符串。 示例 1: 输入:s = "codeleet", indices = [4,5,6,7,0,2,1,3] 输出:"lee..

liuzhen007的专栏 2215

如何修复Windows卡在“正在准备Windows”的问题?这里有详细步骤

如何修复Windows卡在“正在准备Windows”的问题?这里有详细步骤。

wyxtx的博客 5657

算法-字符串重组

1. 字符串重组 给定一个字符串,输出该字符串所有单个字符重新排列的不重复的字符串。 例如: abc 输出:abc,acb,bac,bca,cba,cab 2. 解法 回溯思想,将字符串转化为字符数组,单个字符两两交换位置,完成后回溯到上一层。 public ArrayList<String> Permutation(String str) { ArrayList&lt...

谈谈1974 1703

Unity3D用c#脚本调用前端javascript函数:H5获取unity - webgl 加载并初始化完成的消息

unity3d发布的Webgl 可以作为静态文件加载到H5页面中,并通过javascript 调用unity内置的C#脚本函数,详见Webgl调用unity api。而往往我们需要知道Webgl ready的消息,本文将给出unity 内部调用javascript函数的方案加以解决。

Bravo 2475

解密UFS Explorer:Drobo BeyondRAID数据恢复实战指南

本文详细介绍了使用UFS Explorer Professional Recovery软件中的Drobo BeyondRAID助手进行数据恢复的完整实战流程。当Drobo设备因BeyondRAID配置信息丢失而无法访问时,该工具能安全、只读地扫描硬盘,虚拟重建存储池,帮助用户找回宝贵数据。指南涵盖了从前期准备、硬盘选择、配置扫描到数据安全导出的每一步关键操作与常见问题解决方案。

weixin_29219539的博客 269

字符数组重组-字符串重组

本关任务:编写程序,功能是将源字符串s中所有下标为奇数的字符或ASCII码 为偶数的字符放入新字符(串)数组t中(规定第一个字符放在第0位中)。并输出处理后新的字符串。输入: 一行字符,长度小于20输出: 重组后的字符串。

2301_80109702的博客 795

【R语言-生存分析之观察生存率计算】

全人群肿瘤登记资料常用的统计分析指标包括发病率、死亡率、现患率以及生存率等,其中肿瘤生存率是评估肿瘤治疗效果和肿瘤负担的必要指标,其计算涉及肿瘤患者的发病、 死亡和随访三个方面的资料, 数据整理和计算过程均较为复杂, 如何及时、 准确地计算肿瘤生存率, 并使其可以在不同地区、 不同人群、不同时期间被客观公正地比较和评价,仍然是统计学家们一直在研究的主题。

weixin_46579654的博客 5439

重组字符串

在面试过程中遇到的这个问题。 对于输入:***a*b*cd**e*.... 期望输出:********abcde.... 要求:将字符串中的*全部移动到前半部分,字母移动到后半部分,并保持顺序不变 思路1:定义两个StringBuilder,遍历输入,如果是*存入第一个StringBuilder,否则存入第二个StringBuilder。最后拼接两个字符串。时间复杂度0(N),空间

Renxiao的专栏 1351

YOLOv8【第十一章:视频追踪与流处理篇·第16节】一文搞懂,DeepStream 部署入门:NVIDIA 视频分析 SDK 与 YOLOv8 结合!

🏆 本文收录于 《YOLOv8实战:从入门到深度优化》 专栏。该专栏系统复现并梳理全网各类 YOLOv8 改进与实战案例(当前已覆盖分类 / 检测 / 分割 / 追踪 / 关键点 / OBB 检测等方向),坚持持续更新 + 深度解析,质量分长期稳定在 97 分以上,可视为当前市面上 覆盖较全、更新较快、实战导向极强 的 YOLO 改进系列内容之一。

**My Coding Family** 838

计算机视觉教程1-1:透视空间与透视变换

在生活中,透视现象很常见,其物理本质是光的直线传播。由于人眼与相机在捕获图像时均存在透视现象,因此计算机几何基于透视空间进行研究。本节介绍透视变换原理

FRIGIDWINTER的博客 3089

字符串:重组

    给定一个字符串,要求对其进行重组,使相同的字符不相邻,如果无法满足要求,则返回空串。如字符串“aab”,重组后为“aba”。    分析:先统计每个字符出现的次数,把字符和次数存入HashMap中,对HashMap根据次数进行排序,然后再进行重组:前一半append,后一半间隔插入。(其实也可以不用排序,只需找到出现次数最多的那个字符,先append该字符即可) public Str...

Peanut 大本营 1182

提高RFID检测距离以及检测速度的方法总结

摘要:提高RFID检测距离需优化阅读器功率、标签灵敏度、天线设计及频段选择,同时减少环境干扰;提升检测速度则需改进防碰撞算法、增强并行处理能力、精简数据传输。实际应用需平衡两者关系,如物流仓储采用UHF频段、高增益天线和动态Q算法实现远距离高速读取,ETC系统则通过微波频段和高速调制实现超远距离快速识别。不同场景需针对性优化硬件配置和系统参数。

start_up_go的博客 290

LeetCode: 767 Reorganize String

题目: 767 Reorganize String (https://leetcode.com/problems/reorganize-string/) 每次取当前出现频率最高的两个字母拼接在字符串上。直到PriorityQueue中元素的数量只有一个或者没有。如果只有一个且他还有大于1的频率,就说明这个字符创无法Reorganize。 class Solution { public...

majinliang1234的博客 228

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

[leetcode]Weekly Contest 68 (767. Reorganize String&&769. Max Chunks To Make Sorted&&768. Max Chunks...

[leetcode]Weekly Contest 68 (767. Reorganize String&&769. Max Chunks To Make Sorted&&768. Max Chunks To Make Sorted II) 766. Toeplitz Matrix 第一题不说,贼麻瓜,好久没以比赛的...

swallowblank的博客 126

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

767. Reorganize String**

767. Reorganize String** https://leetcode.com/problems/reorganize-string/ 题目描述 Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not t...

珍妮的选择的博客 390

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Leetcode——767. Reorganize String

题目原址 https://leetcode.com/problems/reorganize-string/description/ 题目描述 Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the ...

想当厨子的程序媛 1409

Leetcode 767. Reorganize String

文章作者:Tyan 博客:noahsnail.com &nbsp;|&nbsp; 优快云 &nbsp;|&nbsp; 简书 1. Description 2. Solution class Solution { public: string reorganizeString(string S) { int length = S.size(); int maximum...

SnailTyan 627

LeetCode 767. Reorganize String Python字符串

Given a stringS, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same. If possible, output any possible result. If not possible, return the empty string. Example 1: Input: S = "aab" Output: "a..

taoqick的专栏 389

【LeetCode】767. Reorganize String 重构字符串(Medium)(JAVA)

【LeetCode】767. Reorganize String 重构字符串(Medium)(JAVA) 题目地址: https://leetcode.com/problems/reorganize-string/ 题目描述: Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same. If possible,

qq_16927853的博客 278

[leetcode] 767. Reorganize String

Description Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same. If possible, output any possible result. If not possible,...

Learning from the mistakes 215

[Leetcode] 767. Reorganize String 解题报告

题目: Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same. If possible, output any possible result.  If not possibl

魔豆(Magicbean)的博客 1047

[LeetCode 767] Reorganize String

Given a stringS, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same. If possible, output any possible result. If not possible, return the...

ExcitedZhang的博客 154

LeetCode | 767. Reorganize String调整字符串使得字符串相同字符不相邻

Givena string S, check if the letters can berearranged so that two characters that are adjacent to each other are not thesame. Ifpossible, output any possible result.  If not possible, return the e

u012737193的博客 2984

[LeetCode] 767. Reorganize String

Given a stringS, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same. If possible, output any possible result. If not possible, retur...

a0712104790的博客 87

LeetCode 767. Reorganize String

Given a stringS, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same. If possible, output any possible result. If not possible, return the...

qq_36056790的博客 164
上一篇: leetcode 542. 01 Matrix(01矩阵)
下一篇: yolov8 No labels found in /path/train.cache解决
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值