国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode 75.Sort Colors

2019-11-08 18:36:24
字體:
來源:轉載
供稿:網友

description: Given an array with n objects colored red, white or blue, sort them 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.

可以使用雙指針的算法來進行計算,但是這用因為程序就只有3個變量,因此可以使用特定的判斷來進行運算。

public class Solution { public void sortColors(int[] nums) { if (nums == null || nums.length < 1) { return; } int left = 0; int right = nums.length - 1; int i = 0; while (i <= right) { if (nums[i] == 0) { swap(nums, left, i); left++; i++; } else if (nums[i] == 1) { i++; } else { swap(nums, i, right); right--; } } } private void swap(int[] nums, int left, int right) { int temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白山市| 九龙县| 昭通市| 扶余县| 彰化县| 布拖县| 莫力| 新安县| 额济纳旗| 原阳县| 庄浪县| 武山县| 花垣县| 梁平县| 新余市| 乌拉特前旗| 丹阳市| 陈巴尔虎旗| 辉南县| 山阳县| 镇原县| 江津市| 棋牌| 磴口县| 商南县| 迁安市| 禄丰县| 嫩江县| 越西县| 阿拉尔市| 调兵山市| 五河县| 昌平区| 蒲城县| 内江市| 文登市| 宜州市| 太谷县| 兴隆县| 康平县| 沁源县|