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 — LeetCode/Two_Pointers/075.Sort-Colors at master · wisdompeak/LeetCode

Expand file tree

/

075.Sort-Colors

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

075.Sort-Colors

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
75. Sort Colors.cpp
75. Sort Colors.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

075.Sort-Colors

难点在于如何in-palce实现。基本思想是设置三个指针:

int left=0;
int mid=0;
int right=nums.size()-1;

遍历mid指针,当遇到2号球时,便和right指针进行交换;遇到1号球时,继续前进;与到0号球时,便和left指针进行交换。 理解的难点在于:

if (nums[mid]==0)
{
  swap(nums[left],nums[mid]);
  left++;
  mid++;
}

因为left和mid初始位置相同,他们之间出现异步只是因为1号球的出现。所以这两个指针之间间隔的永远只会是1号球,所以当left和mid交换时,left指针传给mid的一定会是1号球而不是0号球。

Leetcode Link

You are visitor 01402792 — 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.