程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 【LeetCode OJ 075】Sort Colors

【LeetCode OJ 075】Sort Colors

編輯:C++入門知識

【LeetCode OJ 075】Sort Colors


題目:Given an array withnobjects 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.

解題思路如下:遍歷數組,存儲每種顏色的個數,再進行排序,示例代碼如下:

 

/**
 * @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.
 * @author 徐劍   
 * @date 2016年3月27日 下午12:32:14 
 * @version V1.0
 */
public class Solution
{	
	 public static void sortColors(int[] nums)
	 {
	        if(nums==null||nums.length<=0)
	        	return;
	        int a[]=new int[3];
	        for(int i=0;i

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved