GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — SolvedAlgorithm/Sorting Algorithm/75.md at main · EsperErWang/SolvedAlgorithm

Expand file tree

/

75.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
32 lines (28 loc) · 879 Bytes
/

75.md

Copy path

File metadata and controls

32 lines (28 loc) · 879 Bytes
Raw
Copy raw file
Download raw file
Edit and raw actions
public void sortColors(int[] nums) {
    // The left side of red are all red color,
    // The left side of white are red and white color.
    int red = 0;
    int white = 0;

    for(int i = 0; i < nums.length; i++) {
        if (nums[i] == 0) {
            swap(nums, red, i);
            red++;
            white++;

            // for the situation that we have put the first white element to the i idx,
            // so we need to put it back to white area(to be the last white element).
            if (nums[i] == 1) {
                swap(nums, white - 1, i);
            }
        }else if(nums[i] == 1) {
            swap(nums, white, i);
            white++;
        }   
    }        
}

private void swap(int[] nums, int left, int right) {
    int cur = nums[left];
    nums[left] = nums[right];
    nums[right] = cur;
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.