#463 Sort Integers
来源:互联网 发布:三工序雕刻机怎么编程 编辑:程序博客网 时间:2024/06/16 17:54
题目描述:
Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort or any O(n2) algorithm.
Example
题目思路:Given [3, 2, 1, 4, 5]
, return [1, 2, 3, 4, 5]
.
O(n^2)代价的sort方法有很多,随便想了一种:对于每一个A[i],从j = i + 1...开始寻找比它更小的数,如果找到了就swap i and j,这样保证i永远是i...A.size() - 1中最小的数。
Mycode(AC = 50ms):
class Solution {public: /** * @param A an integer array * @return void */ void sortIntegers(vector<int>& A) { // Write your code here for (int i = 0; i < A.size(); i++) { for (int j = i + 1; j < A.size(); j++) { if (A[i] > A[j]) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } } } }};
0 0
- #463 Sort Integers
- #464 Sort Integers II
- LintCode-463.Sort Integers
- [LintCode]Sort Integers(C++)
- [LintCode] Sort Integers 整数排序
- "Sum of Two Integers" and "Sort Colors"
- Sort distinct integers in the range [0..N-1]
- Sort an Array Mixed With Integers and Strings - Ruby
- 463.Sort Integers-整数排序(入门题)
- 464.Sort Integers II-整数排序II(容易题)
- 464.Sort Integers II-整数排序 II(容易题)
- Given an array of integers, sort the array according to frequency of elements
- wiggle sort Given a list of integers, sort them so the output is s1 <= s2 >=s3 <=s4 ... sN.
- sort
- Sort
- sort
- Sort
- sort
- 线程-实例变量
- [又值奥运季] 2016年里约奥运会--8月18日赛事
- Spring MVC的拦截器
- linux listen 内核实现
- 各种字符编码的知识-ASCII,Unicode,UTF-8,UTF-16
- #463 Sort Integers
- jquery中 html() text() val() innerText总结
- Cocos2dx Shader学习
- springMVC拦截器
- Rtsp发送ANNOUNCE到Darwin时,RTSP/1.0 401 Unauthorized
- 原型模式
- ntpd时间同步 安装与配置
- java质数判断/质数因子/所有质数(素数)*
- LInux系统入门学习了解