当前位置:网站首页>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;
}
}
边栏推荐
- 【Day_10 0428】密码强度等级
- QT_Event class
- 主流小程序框架性能分析
- How can become a good architect necessary skills: painting for all the people praise the system architecture diagram?What is the secret?Quick to open this article and have a look!.
- QT_QThread线程
- GTK修改pixmap像素,提取pixmap像素RGB值
- DBPack SQL Tracing 功能及数据加密功能详解
- SQL的substring_index()用法——MySQL字符串截取
- 中信证券是国内十大券商吗?怎么开户安全?
- 关系运算符和if,else语句
猜你喜欢
随机推荐
星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
golang json returns null
面经汇总-社招-6年
golang json 返回空值
bat 批示处理详解-2
力扣每日一题-第45天-697. 数组的度
opencv real-time face detection
极化微波成像概述3
SQL函数 TO_CHAR(一)
开发工具:第五章:使用idea生成实体类
关于MySql中explain结果filtered的理解
EpiSci | Deep Reinforcement Learning for SoCs: Myth and Reality
【无标题】setInterval和setTimeout详解
实现mnist手写数字识别
后台管理系统的权限思路
程序员架构修炼之道:如何设计“易理解”的系统架构?
sql添加索引
When custom annotations implement log printing, specific fields are blocked from printing
【TDP加码福利】COS用户实践征文月,等你来投稿!!!
史上最全的Redis基础+进阶项目实战总结笔记







![[供应链·案例篇]石油和天然气行业的数字化转型用例](/img/44/9ef9f86f8afb85f49aac1cce55723d.jpg)
