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 ...
随机推荐
- C# TTS-文本转语音
System.Speech 命名空间包含支持语音识别的类型,你可以从Visual Studio很方便的添加相关组件的引用. System.Speech相关介绍:https://msdn.microso ...
- C#Framework4.0支持异步async/await语法
由于用户使用的是XP系统,但是程序里异步都是通过async/await代码来实现的,然而async/await需要Framework4.5版本才可以,而XP系统最高只能支持到Framework4.0, ...
- 译图智讯VIN码识别助力汽配商转型升级
汽配猫是上海佳驰经合能源科技有限公司自主开发的汽车配件B2B网上商城及服务平台,该平台依托互联网云技术.利用创新的商业模式及互联网思维,整合汽配产业链优秀资源,为汽车维修保养企业等产业链各方面提供汽配 ...
- JUC——线程同步锁(锁处理机制简介)
锁处理机制简介 juc的开发框架解决的核心问题是并发访问和数据安全操作问题,当进行并发访问的时候如果对于锁的控制不当,就会造成死锁这样的阻塞问题. 为了解决这样的缺陷,juc里面重新针对于锁的概念进行 ...
- shell 判断日期间隔及润年
#!/bin/bash test.sh until echo "----------------------------------" echo "请输入您的选择:&qu ...
- 《C++设计新思维》勘误,附C++14新解法
勘误: 原书(中文版)3.13节,65-69页中GenScatterHierarchy以及FieldHelper均存在问题,当TypeList中类型有重复时,无法通过编译(原因在于“二义性基类”). ...
- 2017年4月8日Office 365 使用CSV文件导入邮件组
国内版 第一步: Import-Module msonline Connect-MsolService 输入用户名密码 第二步: Get-MsolUser" 第三步: Set-Executi ...
- 华为笔试——C++消重输出
题目:消重输出 题目介绍: 输入一个正整数,给出消除重复数字以后最大的整数,注意需要考虑长整数. 例: 输入:988274320 输出:9874320 题目分析:这个结果的实现需要两个步骤:消重和排序 ...
- ES6的新特性(11)——Class 的继承
Class 的继承 简介 Class 可以通过extends关键字实现继承,这比 ES5 的通过修改原型链实现继承,要清晰和方便很多. class Point { } class ColorPoint ...
- PSP Daily——团队项目Alpha发布
视频展示:优酷视频链接.文案如下 PSP Daily软件NABCD分析: 1) N (Need 需求) PSP Daily 解决了用户(软件工程课上学生)记录例行报告.写每周PSP表格和统计的需求.潜 ...