GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — 颜色分类-75 · Issue #28 · sl1673495/leetcode-javascript Open Labels Description给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。 此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。 注意: 示例: 进阶: 一个直观的解决方案是使用计数排序的两趟扫描算法。 来源:力扣(LeetCode) 思路计数排序最简单的思路就是遍历一遍整个数组,统计出其中各个颜色的数量,最后把这个数组重新填充即可。 let sortColors = function(nums) { let colors = [0, 0, 0] for (let i = 0; i < nums.length; i++) { colors[nums[i]] ++ } nums.length = 0 for (let i = 0; i < colors.length; i++) { for(let j = 0; j < colors[i]; j++) { nums.push(i) } } }; ActivityMetadataMetadataAssigneesLabelsProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |