当前位置:网站首页>Leetcode75. Color Classification
Leetcode75. Color Classification
2022-08-01 17:58:00 【Java Full Stack R&D Alliance】
题目传送地址:https://leetcode.cn/problems/sort-colors/
运行效率
代码如下:
class Solution {
public static void sortColors(int[] nums) {
//Find the first one from left to right2, At the same time, look for the first ratio from right to left2小的数,然后交换位置
int i = 0;
int j = nums.length - 1;
while (i < j) {
while (i < nums.length) {
if (nums[i] == 2) {
break;
}
i++;
}
while (j >= 0) {
if (nums[j] < 2) {
break;
}
j--;
}
if (i < j) {
swap(i, j, nums);
}
}
//经过上面的代码,现在所有的2have been placed at the end of the array
//Next, we will0和1进行位置调整
i = 0;
j = nums.length - 1;
while (i < j) {
while (i < nums.length) {
if (nums[i] == 1) {
break;
}
i++;
}
while (j >= 0) {
if (nums[j] == 0) {
break;
}
j--;
}
if (i < j) {
swap(i, j, nums);
}
}
}
public static void swap(int i, int j, int[] nums) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
边栏推荐
猜你喜欢
随机推荐
创造建材数字转型新视界,中建材如何多边赋能集团业务快速发展
B002 - 基于嵌入式的老人定位追踪监测仪
想做期货,农产品期货怎么炒?波动大么
存储日报-数据湖架构权威指南(使用 Iceberg 和 MinIO)
B011 - 51-based multifunctional fingerprint smart lock
ROS2系列知识(7):用rqt_console查看日志logs
ROS2支持技术:DDS简述
指针和解引用
食品安全 | 新鲜食品vs速食食品,哪一种是你的菜?
以消费场景为驱动的CMDB要怎么建?
typora操作手册
极化微波成像概述3
关于单应性矩阵的若干思考
【Day_12 0507】二进制插入
Leetcode74. 搜索二维矩阵
M1芯片电脑安装cerebro
OpenCV安装、QT、VS配置项目设置
matlab 基于奇偶校验的LSB隐藏水印 三种改进
Leetcode73. 矩阵置零
一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近