【LeetCode OJ 075】Sort Colors

题目链接:https://leetcode.com/problems/sort-colors/

题目: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.

解题思路如下:遍历数组,存储每种颜色的个数,再进行排序,示例代码如下:

/**
 * @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<nums.length;i++)
	        {
	        	a[nums[i]]++;
	        }
	        int x=0;
	        int y=0;
	        int z=0;
	        for(int i=0;i<nums.length;)
	        {
	        	while(x<a[0])
	        	{
	        		x++;
	        		nums[i]=0;
	        		i++;
	        		continue;
	        	}
	        	while(y<a[1])
	        	{
	        		y++;
	        		nums[i]=1;
	        		i++;
	        		continue;
	        	}
	        	while(z<a[2])
	        	{
	        		z++;
	        		nums[i]=2;
	        		i++;
	        		continue;
	        	}
	        }
	 }
}
时间: 2024-11-06 22:46:03

【LeetCode OJ 075】Sort Colors的相关文章

LeetCode OJ平台上Sort Colors题目算法探讨

原题如下,意思就是说无序数组(由0,1,2组成),一遍扫描,把数组整理成若干个0-若干个1-若干个2这样的序列. 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 integ

【LeetCode OJ 016】3Sum Closest

题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exact

【LeetCode OJ 14】Longest Common Prefix

题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest common prefix string amongst an array of strings. 解题思路:寻找字符串数组的最长公共前缀,将数组的第一个元素作为默认公共前缀,依次与后面的元素进行比较,取其公共部分,比较结束后,剩下的就是该字符串数组的最长公共前缀,示例代码如下: public clas

【LeetCode OJ 242】Valid Anagram

题目链接:https://leetcode.com/problems/valid-anagram/ 题目:Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", re

【LeetCode OJ 004】Median of Two Sorted Arrays

题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 解题思路:将两个有序数

【LeetCode OJ 328】Odd Even Linked List

题目链接:https://leetcode.com/problems/odd-even-linked-list/ 题目:Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to d

【LeetCode OJ 225】Implement Stack using Queues

题目链接:https://leetcode.com/problems/implement-stack-using-queues/ 题目:Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empt

【LeetCode OJ 101】Symmetric Tree

题目链接:https://leetcode.com/problems/symmetric-tree/ 题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \

【LeetCode OJ 34】Search for a Range

题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not foun