【LeetCode】C# 75、Sort Colors
来源:互联网 发布:vc mfc编程登录界面 编辑:程序博客网 时间:2024/06/06 06:33
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.
把相同颜色(数字)整理到一起。
思路,利用交换保证数组中各元素个数不变更。然后遍历数组,把指针遇到的0,与前面代表0边界的指针交换。
public class Solution { public void SortColors(int[] nums) { int p1 = 0, p2 = nums.Length - 1, index = 0; while (index <= p2) { if (nums[index] == 0) { nums[index] = nums[p1]; nums[p1] = 0; p1++; } if (nums[index] == 2) { nums[index] = nums[p2]; nums[p2] = 2; p2--; continue; } index++; } }}
阅读全文
0 0
- 【LeetCode】C# 75、Sort Colors
- Leetcode 75 Sort Colors
- LeetCode 75 Sort Colors
- [leetcode 75] Sort Colors
- LeetCode 75 Sort Colors
- leetcode-75 Sort Colors
- [leetCode 75] Sort Colors
- LeetCode 75 Sort Colors
- leetcode || 75、Sort Colors
- Leetcode 75Sort Colors
- Sort Colors - LeetCode 75
- leetcode 75:Sort Colors
- LeetCode(75) Sort Colors
- Leetcode # 75 Sort Colors
- leetcode 75: Sort Colors
- Leetcode#75||Sort Colors
- Leetcode 75:Sort Colors
- 【leetcode】【75】Sort Colors
- 马云总结创业三要素:乐观、自查、坚持
- 修改postgresql的自动提交
- html中转义字符的含义 > &It; 等
- 花了280亿美元,收购163家公司,谷歌硬件为何还是失败
- java文件传输基础:transient
- 【LeetCode】C# 75、Sort Colors
- [QT]QMessageBox 的四种用法
- Qt-获取主机网络信息
- 完全平方数 BZOJ
- JDK 8新特性LongAdder和AtomicLong的性能测试对比
- java核心技术卷 之泛型程序设计
- 从零一起学react(6)---props属性验证v16.0.0
- PostgreSQL在非默认的路径下创建表空间及数据库后,删除数据库目录后,还需要删除pg_tblspc下的链接文件,否则尝试启动其他实例时会报错
- iOS面试题