NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
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; } } }; ```