leetcode 75: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.

Note:
You are not suppose to use the library's sort function for this problem.

click to show follow up.

Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.

Could you come up with an one-pass algorithm using only constant space?

思路:

从左往右扫描,遇到0放左边,遇到2放右边

时间复杂度:O(n)

实现如下:

class Solution {
public:
	void sortColors(vector<int>& nums) {
		int size = nums.size();
		if (size < 2) return;
		int left=0, right=size-1,index=0;
		while (index <= right)
		{
			if (nums[left] == 0)
			{
				++left;
				++index;
				continue;
			}
			if (nums[right] == 2)
			{
				--right;
				continue;
			}
			if (nums[left] == 2 && nums[right] == 0)
			{
				swap(nums[left++], nums[right--]);
				if(index<left) ++index;
				continue;
			}
			else if (nums[left] == 2)
			{
				swap(nums[left], nums[right--]);
				continue;
			}
			else if (nums[right] == 0)
			{
				swap(nums[left++], nums[right]);
				if(index<left) ++index;
				continue;
			}
			if (nums[index] == 0) swap(nums[index++], nums[left++]);
			else if (nums[index] == 2) swap(nums[index++], nums[right--]);
			else ++index;
		}
	}
};

唉,这段代码写的真水。

下面来看看别人家赏心悦目的代码:

 class Solution {
    public:
        void sortColors(vector<int> &A) {
            int n=A.size();
            int second=n-1, zero=0;
            for (int i=0; i<=second; i++) {
                while (A[i]==2 && i<second) swap(A[i], A[second--]);
                while (A[i]==0 && i>zero) swap(A[i], A[zero++]);
            }
        }
    };

不得不说这段代码写的精妙,不过时间可能比上面的解法长点。因为右边的0也是先换到中间,在换到左边,同样右边的2也可能先换到中间,再换到右边。

从零到一:手把手教你用C#开发AGV调度系统(附避坑指南) 本文详细介绍了如何使用C#开发高效的AGV调度系统,涵盖从环境搭建到核心算法实现的完整流程。重点解析了动态路径规划、多机协同等关键技术,并提供了实用的避坑指南和性能优化建议,帮助开发者快速构建工业级车辆控制调度系统。 阅读详情

相关推荐

大厂案例:快手AI人才管理系统的架构设计与效果

凌晨两点刷简历的HR、说不清“团队离职率高”原因的业务leader、不知道“该学什么”的员工——这些人力资源场景中的“痛点”,曾是快手高速增长期的真实写照。当员工规模从几千扩张到几万,传统HR系统的“经验驱动”模式彻底失效。2021年,快手启动AI人才管理系统建设,用“数据+算法”重构招聘、培养、绩效、留存全流程。本文将拆解这套系统的架构设计逻辑核心技术实现,以及真实业务效果AI如何让HR从“事务执行者”变成“战略伙伴”?

大模型应用工坊 1722

【LeetCode】解题75:Sort Colors

LeetCode解题 75:Sort Colors Problem 75: Sort Colors [Medium]解题思路Solution (Java)修改过程 Problem 75: Sort Colors [Medium] Given an array with n objects colored red, white or blue, sort them in-place so that ...

Fayedily的博客 270

AIDL HAL服务从零到一:构建现代Android硬件抽象层

本文详细介绍了从零开始构建现代Android AIDL HAL服务的完整流程。通过定义接口、实现服务逻辑、配置构建与VINTF清单,并最终集成到系统镜像,开发者可以掌握利用AIDL替代传统HIDL来开发硬件抽象层的核心方法。文章重点解析了AIDL HAL的创建流程,并对比了其与HIDL在语法、工具链和集成方式上的优势,帮助开发者高效构建稳定的硬件接口。

weixin_29174141的博客 153

LeetCode:75. Sort Colors

LeetCode:75. Sort Colors Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. ...

Wisimer 291

opencv读取海康威视摄像头相关配置

1、开发环境 海康威视的网络摄像头使用: 开发工具:vs2015 C++ 海康SDK 2、相关操作 下载海康威视SDK:网址:http://www.hikvision.com/cn/download_61.html • 1:对照自己的开发平台可以下载设备网络SDK_Win32设备网络SDK_Win64,解压到本地文件下: • 2:VS配置 、Opencv配置。参考址 http://blog.csdn.net/poem_qianmo/article/details/19809337 (建议自己创

HMJ115的博客 996

红外与可见光融合避坑指南:LLVIP数据集实测6大算法效果对比

本文基于专为弱光视觉设计的LLVIP数据集,对GTF、DenseFuse、FusionGAN、IFCNN等6种主流红外与可见光图像融合算法进行了深度实测与对比。通过客观指标与可视化分析,揭示了算法在极端弱光下易出现特征丢失的共性问题,并探讨了融合图像对下游目标检测任务的实际增益,为工程选型与算法调优提供了实用指南。

weixin_29092031的博客 446

LeetCode75:Sort Colors

Given an array withnobjects colored red, white or blue, sort themin-placeso that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the in...

卷卷萌的博客 213

【C/C++ 性能优化】提高C++程序的缓存命中率以优化性能

在探讨计算机性能优化的旅程中,我们常常被引导去关注算法的复杂度、代码的优化,或是更高效的数据结构。然而,有一个经常被忽视的角色在幕后默默地影响着程序的运行效率——那就是缓存(Cache)。正如哲学家亨利·戴维·梭罗在《瓦尔登湖》中所说:“细节是造成美丽的源泉”,在软件开发的世界里,对缓存的理解和优化,就是那些决定程序性能优雅与否的细节。

探索C++编程的奥秘,分享深入的技术见解和实践,旨在激发读者创造力与解决问题的思维。 3749

Leetcode 75:Sort Colors

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

Smith_David的博客 457

Python实战:打造学生信息管理系统

通过本教程,我们构建了一个简单的学生信息管理系统,实现了基本的添加、删除、修改和查询功能。当然,这个系统还有很多可以改进和扩展的地方,比如添加用户界面、增加数据持久化功能、支持批量操作等。简介:本文将介绍如何使用Python编写一个功能齐全的学生信息管理系统,包括添加、删除、修改和查询学生信息的基本操作。学生信息管理系统是学校日常管理中不可或缺的一部分,它能够帮助管理人员快速、准确地管理学生信息,提高工作效率。为了实现上述功能,我们可以设计一个包含学生信息的字典,并使用函数来封装各个操作。

铭渊老黄 1497

YOLOV12融合MSDA多尺度空洞注意力:突破尺度限制的科研实践指南

MSDA通过多尺度空洞注意力的设计,为YOLOV12突破尺度检测瓶颈提供了有效方案。从原理理解到代码实现,再到集成调优,每一步都是科研中“解决实际问题”的具体实践。如果你在实验中发现某类尺度目标的检测仍有不足,可以尝试结合数据增强(如随机缩放、多尺度训练)来强化模型对尺度变化的鲁棒性。科研的价值在于不断解决真实难题,希望这份指南能真正帮你在目标检测的尺度感知领域找到新的突破点,祝你在科研探索中持续收获新的见解与成果。

FJN110的博客 256

LeetCode 75 : Sort Colors

问题描述 Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use th...

zero 155

广东省道路工程检测练习试题(单选题)真题

1.一般项目的合格点率应达到( ),且不合格点的最大偏差值不得大于规定允许偏差值的1.5倍。A.不低于75%B.不低于80%C.不低于85%D.不低于90%答案:B2.进行土路基平整度检测时,当路宽小于9m,检测点数应为( )A.1B.2C.3D.4答案:A3.根据《城镇道路工程施工与质量验收规范》,在分部工程中,根据工种、材料、施工工艺等划分的是( )A.单位工程B.分项工程C.分部工程D.工序工程答案:B4.根据《城镇道路工程施工与质量验收规范》,土方路基压实度的检测频率为每1000m每压实层抽检( )

ZShy001238的博客 693

leetcode75: 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

sinat_35425429的博客 320

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Leetcode: 75. Sort Colors

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...

anrui0096的博客 137

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

LeetCode 75: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

henryzhihua 976

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

leetcode 75: Sort Colors

class Solution { public: void sortColors(vector& nums) { int i=-1,j=-1,k=-1; for(int m=0;m<nums.size();m++) { if(nums[m]==0) { nums[

mikefan1991的博客 282

leetcode 75:Sort Colors

用红蓝指针往中间夹的时候注意 i++ ,和i的上界 // 用 红蓝两个指针往中间夹- class Solution { public: void sortColors(vector& nums) { int red=0; int blue=nums.size()-1; for(int i=0;i<blue+1;){ // [0,

le119126的专栏 425

leetCode75: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

Kingfar Ou 417

leetcode:(75) Sort Colors(java)

题目:       Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we wil...

Sunshine_liang1的博客 322

【算法入门】力扣75. 颜色分类(Sort Colors)题解:Java与JavaScript双指针实现LeetCode 75:颜色分类 - 三路快排与双指针法详解(Java & JavaScript

本文介绍了LeetCode 75题"颜色分类"的两种高效解法:三路快排和双指针法。题目要求将包含0(红)、1(白)、2(蓝)的数组原地排序为红、白、蓝的顺序。 核心解法均基于荷兰国旗问题: 三路快排法:使用三个指针(left,curr,right)将数组分为0、1、2三部分,时间复杂度O(n),空间复杂度O(1) 双指针法:维护0和2的边界指针,交换元素到正确位置 提供了Java和JavaScript的实现代码,两种方法都能在一次扫描内完成排序,适合处理类似分类问题。掌握这些指针技巧对解

南北极之间 820

LeetCode 75. Sort Colors (颜色分类):三路快排

Given an array withnobjects colored red, white or blue, sort themin-placeso that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the in...

weixin_30650859的博客 256

[leetcode:python]75.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

cyy2learn的博客 1002

leetcode:75. 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,

jxfang的专栏 388

leetcode: 75. Sort Colors

QGiven 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

JNing 574

leetcode解题报告:75. 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 intege

wengyunpeng的博客 313
上一篇: leetcode 74:Search a 2D Matrix
下一篇: leetcode 76:Minimum Window Substring
onlyou2030
博客等级 码龄15年 13粉丝 215原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值