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


【算法思路】:重新new一个新的数组出来,接受依次接收新的0,1,2.但是由于无返回型的方法,所以一直没有AC。


小总结一下:怎样获取长度?

如果是数组的话就是:数组.length属性;如果是字符串的话就是:字符串.length()方法;如果是集合的话就是:集合.size()方法


public class Solution {
    public void sortColors(int[] nums) {
       
        int len = nums.length;
        //String result="";
        int[] result = new int[len];
        int n = 0;
         
        for(int i = 0;i<len-1;i++)
            if(nums[i]==0){
                result[n++]=nums[i];
                
            }
        
        
        for(int i = 0;i<len-1;i++)
            if(nums[i]==1){
                result[n++]=nums[i];
               
            }
        
        
        for(int i = 0;i<len-1;i++)
            if(nums[i]==2){
                result[n++]=nums[i];
            }
        
        return result;
        
       // result.toCharArray();
    }
}

【AC算法思路】:最直接的方法是,计数排序。先计算0,1,2的个数,然后重新给数组赋值……两次循环,一次计数,一次赋值

public class Solution {
    public void sortColors(int[] nums) {
       
        int len = nums.length;
        int m = 0;//分别来计算0,1,2的个数
        int n = 0;
        int l = 0;
        
        for(int i = 0;i<len;i++){
            
            if(nums[i]==0){
                m++;
                
            }
        
            else if(nums[i]==1){
                n++;
               
            }
            else if(nums[i]==2){
                l++;
            }
        }
        
        for(int i = 0;i<len;i++){
            if(i < m)
                nums[i] = 0;
                
            else if(i >= m && i < m + n)//重m到m+n为1,剩下的为2(前提只有0,1,2三种数)
                nums[i] = 1;
                
            else
                nums[i] = 2;
        }
        
       
    }
}

【AC算法思路】:一次循环实现的算法,设置两个index,left记录第一个1的位置,left左边为0,right记录第一个非2的位置,right右边为2.

由于只扫一遍,左边存放0和1,右边存放2.两边往中间靠。然后使用i从头到尾扫一遍,直到与right相遇。

i遇到0就换到左边去,遇到2就换到右边去,遇到1就跳过。

需要注意的是:由于left记录第一个1的位置,因此A[left]与A[i]交换后,A[left]为0,A[i]为1,因此left++,i++;

而right记录第一个非2的位置,可能为0或1,因此A[right]与A[i]交换后,A[right]为2,A[i]为0或1,i不能前进,要后续判断,rigth--。

由此该数组分为4段:[0,left)-->0; [left,i)-->1; [i,right]-->乱序; (right,n-1]-->2

0  0  0  1  1  1  2  1  0  2  1  2  2  2

           ^         ^             ^

          left         i            right


java代码:

public class Solution {
    public void sortColors(int[] nums) {
        int len = nums.length;
        int left= 0;//left记录第一个1的位置,left左边为0
        int rigth = len-1;//,right记录第一个非2的位置,right右边为2.
        int i = 0;
        while(i <= rigth){
            if(nums[i]==0){
                
                int t;
                t = nums[i];
                nums[i] =nums[left];
                nums[left]= t;
                
              //  swap(nums[i],nums[left]);
                left++;
                i++;
            }
            
            else if(nums[i]==1){
                i++;
            }
            
            else{
                 int t;
                t = nums[i];
                nums[i] =nums[rigth];
                nums[rigth]= t;
                
               // swap(nums[i],nums[rigth]);
                rigth--;
            }
        }
        
    }
   /* public void swap(int a,int b){
        int t;
        t = a;
        a = b;
        b = t;
    }*/
}



webpack性能优化-optimization.splitChunks.chunks中的“all“、“async“和“initial“ 最初,chunks(以及内部导入的模块)是通过内部 webpack 图谱中的父子关系关联的。CommonsChunkPlugin 曾被用来避免他们之间的重复依赖,但是不可能再做进一步的优化。 ——摘自 webpack 从 webpack v4 开始,移除了 CommonsChunkPlugin,取而代之的是 optimization.splitChunks。 而关于 optimization.splitChunks 中的chunks,webpack 官方文档却没有很好的进行举例解释,只说明了 async. 阅读详情

相关推荐

CAN总线BusOff恢复机制详解:快慢恢复策略在车载网络中的实战应用

本文深入解析了CAN总线BusOff恢复机制,重点阐述了快慢恢复策略在车载网络中的工程应用。通过对比NXP S32K与STM32平台的实现代码,并结合CANoe仿真与参数优化方法,为工程师提供了从理论到实战的完整解决方案,旨在提升车载网络的可靠性与功能安全。

7up55的博客 624

【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

COMSOL仿真多模光纤弯曲损耗:从原理到工程实践

光纤弯曲损耗是光通信与光纤传感领域的基础物理现象,其本质是光波导结构变形导致模式耦合与能量泄漏。从波动光学原理出发,当光纤弯曲时,其有效折射率分布发生改变,破坏了全反射条件,使得高阶导模转化为辐射模,从而产生损耗。这一过程对光纤通信系统的传输距离、稳定性以及微型化光纤设备的设计至关重要。利用COMSOL Multiphysics的多物理场仿真能力,特别是其波动光学模块与变换光学方法,可以高效、精确地模拟弯曲半径、折射率剖面等参数对损耗的影响。该方法为光通信设计工程师和科研人员提供了强大的虚拟原型验证工具,能

weixin_31188927的博客 302

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

Cesium创建无人机视锥,同步视锥与鹰眼地图视角,实现视锥键盘控制。解决视锥初始化位置不准确问题等

本文描述了一个基于Cesium的3D视锥体可视化系统开发过程。系统实现了主视图与鹰眼视图的双屏联动,支持通过键盘控制视锥体的空间移动(WASD前后左右,Q/E旋转,C/Z升降),并实时同步更新两个视图。关键技术包括:1)基于主视图Scene创建Camera实现视锥体精确绘制;2)通过PerspectiveFrustum设置视锥参数;3)建立双视图的相机位置及视角同步机制。目前暂未实现视锥体far/near边界在鹰眼视图中的可视化,该系统可应用于无人机视角监控等三维场景交互需求。

coder_bin的博客 1605

AutoCAD Plant 3D自定义零件库实战:手把手教你用Python创建参数化槽式三通

本文详细介绍了如何利用Python脚本在AutoCAD Plant 3D中创建参数化槽式三通组件,从API原理到实战开发,帮助用户构建高效的自定义零件库。通过Python装饰器系统控制零件属性和行为,实现几何建模、参数管理和规格编辑器集成,提升工业管道设计的标准化和效率。

weixin_28267011的博客 351

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

手把手教你用STM32CubeMX和HAL库驱动DW3000:从SPI配置到第一个测距Demo

本文详细介绍了如何使用STM32CubeMX和HAL库驱动DW3000 UWB定位通信模块,从SPI配置到实现厘米级测距Demo的全过程。涵盖硬件连接、SPI通信调试、驱动移植及双向测距(TWR)算法实现,帮助开发者快速掌握UWB高精度定位技术,适用于仓储物流、智能家居等场景。

weixin_27038245的博客 499

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

Lumerical官方案例、FDTD时域有限差分法仿真学习(十四)——超透镜(Metalens)

这个例子的目的是设计一个由圆柱形纳米棒组成的衍射超透镜。 纳米棒的半径和排列经过调整以在超透镜表面上产生所需的相位分布。 该设计通过近场和远场分析在 Ansys FDTD、RCWA(严格耦合波分析)和 OpticStudio 中得到验证。...

weixin_45719141的博客 3万+

【赛题解析&命令脚本配置】2023年全国职业院校技能大赛GZ073网络系统管理赛项赛题第1套模块A:网络构建 & (五)无线网络配置+(六)出口网络配置-命令配置

【赛题解析&命令脚本配置】2023年全国职业院校技能大赛GZ073网络系统管理赛项赛题第1套模块A:网络构建 & (五)无线网络配置+(六)出口网络配置-命令配置

君逍遥o 415

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

onlyou2030的博客 293

跨平台C#上位机开发:.NET MAUI与工业物联网的融合实战

NET MAUI正在重塑工业上位机的开发模式——从“为单一设备定制”转向“全场景覆盖”。其价值不仅在于“代码复用”,更在于打通了工业物联网的“最后一公里”:让边缘设备、云平台、移动终端通过统一的数据模型协同工作。未来,随着.NET MAUI对WebAssembly的支持(计划在.NET 9实现),上位机甚至可直接运行在浏览器中,进一步降低部署门槛。对于工业开发者,掌握跨平台技术意味着能更快响应多样化的现场需求,从“Windows专家”升级为“全栈工业开发者”。

这里只有干货:从 Modbus 到 MES,从 YOLO 到工控机部署;65 篇现场排障案例,每篇都写到改哪一行。 1103

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

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

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

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

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

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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

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

【算法入门】力扣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(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

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 67:Add Binary
下一篇: Leetcode 1:Two Sum
Smith_David
博客等级 码龄11年 1粉丝 4原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值