[LeetCode-interview algorithm classic-Java implementation] [075-Sort Colors (Color Sorting)], leetcode -- java

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [075-Sort Colors (Color Sorting)], leetcode -- java
[075-Sort Colors (Color Sorting )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question

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.

Theme

Given an array of objects, the objects are red, white, and blue. Sort the colors, red, white, and blue.
Use 0, 1, and 2 to represent red, white, and blue respectively.
  Note:The database function cannot be used for sorting.

Solutions

Scan the array and count the number of records 1 and the sum of the entire array. After scanning, we can get the number t of 1, the number of 2 (sum-t)/2, and the number of 0, 2 and then set the value of the array.

Code Implementation

Algorithm Implementation class

Public class Solution {public void sortColors (int [] A) {if (A = null) {return;} int count = 0; // count the number of 1 int sum = 0; // count the sum of the array and for (int I: A) {if (I = 1) {count ++ ;} sum + = I;} sum = (sum-count)/2; // calculate the number of 2 count =. length-count-sum; // the position where 1 begins to appear. sum =. length-sum; // the position where 2 starts to appear for (int I = 0; I <count; I ++) {A [I] = 0 ;} for (int I = count; I <sum; I ++) {A [I] = 1 ;}for (int I = sum; I <. length; I ++) {A [I] = 2 ;}}}
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/47142945]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.