Sort Integers II
分析
Quick Sort
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public class Solution { /** * @param A an integer array * @return void */ public void sortIntegers2(int[] A) { // Write your code here quickSortHelper(A, 0, A.length - 1); } private void quickSortHelper(int[] A, int left, int right) { if(left >= right) return; int i = left, j = right; int key = A[left + (right - left) / 2]; while(i <= j){ while(i <= j && A[i] < key) ++i; while(i <= j && A[j] > key) --j; if(i <= j){ int tmp = A[i]; A[i++] = A[j]; A[j--] = tmp; } } quickSortHelper(A, left, j); quickSortHelper(A, i, right); }} |
Merge Sort
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public class Solution { /** * @param A an integer array * @return void */ public void sortIntegers2(int[] A) { // Write your code here int[] tmp = new int[A.length]; mergeSortHelper(A, tmp, 0, A.length - 1); } private void mergeSortHelper(int[] A, int[] tmp, int left, int right) { if(left >= right) return; int mid = left + (right - left) >> 1; mergeSortHelper(A, tmp, left, mid); mergeSortHelper(A, tmp, mid + 1, right); merge(A, tmp, left, mid, right); } private void merge(int[] A, int[] tmp, int left, int mid, int right){ int i = left, j = mid + 1, index = left; while(i <= mid || j <= right){ int a, b, min; a = (i <= mid) ?A[i] : Integer.MAX_VALUE; b = (j <= right) ? A[j] : Integer.MAX_VALUE; min = (a <= b) ?A[i++] : A[j++]; tmp[index++] = min; } for(int k = left; k <= right; k++){ A[k] = tmp[k]; } }} |
Sort Integers II的更多相关文章
- [LintCode] Sort Integers II 整数排序之二
Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- [LintCode] Sort Integers 整数排序
Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
- Wiggle Sort I & II
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- Sort Integers
) algorithm. 分析 bubble sort 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Solution ...
- 143. Sort Colors II
最后更新 一刷 class Solution { public void sortColors2(int[] colors, int k) { // write your code here if ( ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
随机推荐
- DB知识点记录
DB知识点记录 分页 SqlServer:ROW_NUMBER () over (ORDER BY ID) AS RN, MySql:limit Oracle:ROWNUM AS RN 数据表的基本结 ...
- php使用mysql之sql注入(功)
sql注入就是用户通过构造sql语句,完成sql一系列操作 准备素材如下: 这是test.html <!DOCTYPE html> <html> <meta charse ...
- NO.05--谈一谈Angular 和 Vue.js 的对比。
几天的vue之后,给需要的盆友们带来一篇对比,也算是我近期之内业余时间的大工程,现在开始: Vue.js 是开源的 JavaScript 框架,能够帮助开发者构建出美观的 Web 界面.当和其它网络工 ...
- 基于marathon-lb的服务自发现与负载均衡
参考文档: Marathon-lb介绍:https://docs.mesosphere.com/1.9/networking/marathon-lb/ 参考:http://www.cnblogs.co ...
- 微信小程序---scroll-view在苹果手机上触底或触顶页面闪动问题
在项目开发中遇到一个关于scroll-view的的问题,具体如下: 项目要求是横向滚动,由于直接在scroll-view组件设置display:flex不生效,因此考虑直接在scroll-view下增 ...
- 暑假App
简介 实现了一个计时器APP,程序界面简洁,只有一个时间显示区域和两个图片按钮,一个按钮是开始/暂停,另一个按钮是停止. 功能介绍 一个显示界面,当最小计时单位为0.1秒时,显示为:分钟:秒:0.1秒 ...
- html、JSP运行原理
HTML运行原理 1.本地运行 所谓本地运行就是直接用 浏览器打开 2.远程访问的原理示意图: 何为协议?计算机互相通信(网络)的规则.常见的协议有 http .smtp. ftp.pop等 ...
- tensorboard入门
Tensorboard tensorboard用以图形化展示我们的代码结构和图形化训练误差等,辅助优化程序 tensorboard实际上是tensorflow机器学习框架下的一个工具,需要先安装ten ...
- Web站点性能-微观手段
文章:网站性能优化 百度百科:高性能Web站点 文章:构建高性能WEB站点之 吞吐率.吞吐量.TPS.性能测试
- lintcode-422-最后一个单词的长度
422-最后一个单词的长度 给定一个字符串, 包含大小写字母.空格' ',请返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 注意事项 一个单词的界定是,由字母组成,但不包含任何的空 ...