ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. Note: You are not suppose to use the library's sort function for this problem. Example: ``` Input: [2,0,2,1,1,0] Output: [0,0,1,1,2,2] ``` ``` var sortColors = function(nums) { let i = -1, j = -1, k = -1; for (let num of nums) { switch(num) { case 0: nums[++k] = 2; nums[++j] = 1; nums[++i] = 0; break; case 1: nums[++k] = 2; nums[++j] = 1; break; case 2: nums[++k] = 2; break; } } }; ```