博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Sort Colors
阅读量:4150 次
发布时间:2019-05-25

本文共 930 字,大约阅读时间需要 3 分钟。

class Solution {//reverse travel the array //endOfZero + endOfOne//classified discuss for current number{0, 1, 2}public:	void sortColors(int A[], int n) {		// Start typing your C/C++ solution below		// DO NOT write int main() function		int e0, e1;		e0 = e1 = n-1;		for (int cur = n-1; cur >= 0; --cur)		{			if(A[cur] == 1)				swap(A[e0--], A[cur]);			else if (A[cur] == 2)			{				swap(A[e0], A[cur]);				swap(A[e0], A[e1]);				e0--;				e1--;			}		}	}};

second time

class Solution {public:    void sortColors(int A[], int n) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector
cntSort(3, 0); for(int i = 0; i < n; ++i) cntSort[A[i]]++; int k = 0; for(int i = 0; i < 3; ++i) { int curCnt = cntSort[i]; for(int j = 0; j < curCnt; ++j) { A[k++] = i; } } }};

转载地址:http://aqxti.baihongyu.com/

你可能感兴趣的文章
设计模式学习笔记-单例模式
查看>>
SpringMVC中的组件及各个组件的作用
查看>>
BeanFactory 和 ApplicationContext的区别
查看>>
浅析Spring框架设计
查看>>
ThreadLocal与synchronized的区别
查看>>
常用设计模式的应用场景
查看>>
AbstractWizardFormController
查看>>
Hexo+Github: 博客网站搭建完全教程(看这篇就够了)
查看>>
博客搭建——代码开源
查看>>
PicGo+GitHub:你的最佳免费图床选择!
查看>>
Python GUI之tkinter窗口视窗教程大集合(看这篇就够了)
查看>>
Markdown Emoji表情语法速查表
查看>>
vim的复制粘贴小结
查看>>
Doxygen + Graphviz windows下安装配置(图解)
查看>>
win8 PL2303驱动的问题
查看>>
vim中寄存器使用和vim标记
查看>>
原码、反码和补码
查看>>
STM32中断(转载)
查看>>
STM32 flash操作
查看>>
gedit assertion `lang != NULL' failed
查看>>