重新排列红,蓝和绿球阵列 [英] Reorder Array of Red, Blue and Green balls

查看:132
本文介绍了重新排列红,蓝和绿球阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一次采访中,面试官问我 - 一些大小的数组包含红,蓝和绿球都混在一起随意。 像RGBBBRRGGG,其中RGB是红,绿和蓝

In one of my interviews the interviewer asked me - An array of some size contains Red, Blue and Green balls all mixed up randomly. like RGBBBRRGGG, where RGB is for Red, Green and Blue.

什么是结了喜欢 - RRRRGGGGBBBB数组最优化的方式 即所有的R的,所有的G的和所有B的在一起。

What is the most optimal way to end up with an array like- RRRRGGGGBBBB i.e. all R's, all G's and all B's together.

我建议将所有红色,蓝色,绿色他们的ASCII值,然后运行最高效的排序算法就可以了。 但他并不IM pressed。任何其他更有效的解决这个问题?以最低的空间和时间复杂度?

I proposed converting all Red, Blue, Green to their ASCII values and then running the most efficient sorting algorithm on it. But he wasn't impressed . Any other more efficient solution to this problem ? with lowest space and time complexity ?

推荐答案

只需经过阵列和计数研究的发生, B 分别。然后,输出的字符串。线性时间。

Simply go through the array and count the occurrence of R, G and B respectively. Then, output the string. Linear time.

这篇关于重新排列红,蓝和绿球阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆