您的位置:首页 > 职场人生

面试:数组:重叠区间个数

2016-04-08 00:07 323 查看
array.sort排序

java中Arrays.sort使用了两种排序方法,快速排序和优化的合并排序。

快速排序主要是对哪些基本类型数据(int,short,long等)排序, 而合并排序用于对对象类型进行排序。

使用不同类型的排序算法主要是由于快速排序是不稳定的,而合并排序是稳定的。这里的稳定是指比较相等的数据在排序之后仍然按照排序之前的前后顺序排列。对于基本数据类型,稳定性没有意义,而对于对象类型,稳定性是比较重要的,因为对象相等的判断可能只是判断关键属性,最好保持相等对象的非关键属性的顺序与排序前一直;另外一个原因是由于合并排序相对而言比较次数比快速排序少,移动(对象引用的移动)次数比快速排序多,而对于对象来说,比较一般比移动耗时。

补充一点合并排序的时间复杂度是n*logn, 快速排序的平均时间复杂度也是n*logn,但是合并排序的需要额外的n个引用的空间

java数据比较

实现java.lang.Comparable的接口;实现compareTo 方法

接受Object为参数

sort排序

对任意的基本类型数组排

list排序

同数组排序

//HelloDate.java
/*
* [输入]
* 重叠区间:[1,5],[10,15],[5,10],[20,30]
*
* [输出]
* 3
*
* 【算法】
* 属于区间,根据大小排序,当起点遇到终点,重叠个数加1
*/
import java.util.*;

class Interval{
int start; //起点
int end; //终点
Interval(int a, int b){
start=a;
end=b;
}
}

class Point implements Comparable<Point>{
int value; //数值
int type; //点的类型,0为起点,1为终点
Point(int v,int t){
value=v;
type=t;
}
//实现compareTo函数
public int compareTo(Point p){
if(this.value==p.value){
return 0;
}else if(this.value >p.value){
return 1;
}else {
return -1;
}
}

//区间转换

}

public class MyDemo{

public int getOverlappingCount(Interval[] A){

int max = 0,count = 1;

if(A==null || A.length==0) return max;

Point[] points = new Point[A.length*2];

for(int i = 0;i < A.length;i++){

points[2*i] = new Point(A[i].start, 0);

points[2*i+1] = new Point(A[i].end, 1);

}

//Collection.sort(points);

Arrays.sort(points);

for (int i = 0; i < points.length; i++) {

if (points[i].type==0) {

count++;

max = Math.max(max, count);

}else{

count--;

}

}

return max;

}
public static void main(String[] args){

Interval[] testInterval=new Interval[4];
testInterval[0]=new Interval(1,5);
testInterval[1]=new Interval(10,15);
testInterval[2]=new Interval(5,10);
testInterval[3]=new Interval(20,30);

MyDemo demo=new MyDemo();
int max = demo.getOverlappingCount(testInterval);

System.out.println(max);

}
}


c++

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <Windows.h>
using namespace std;

//区间定义
class Interval
{
public:
Interval( int iStart, int iEnd)
:m_iStart( iStart), m_iEnd(iEnd){}
int m_iStart;
int m_iEnd;
};

typedef vector<Interval> IntervalVec;

//区间拆分的点定义
class PointComparable
{
public:
PointComparable( int iVal, int iType )
:m_iVal( iVal ), m_iType( iType ){}

//重载小于操作符,排序使用
bool operator < ( const PointComparable& pcPoint )
{
if ( this->m_iVal == pcPoint.m_iVal )
{
return this->m_iType < pcPoint.m_iType;
}
return this->m_iVal < pcPoint.m_iVal;
}

int m_iVal;
int m_iType;//点类型,0为起点,1为终点
};

int GetOverlappedIntervalMaxCount( const IntervalVec& intvVec )
{
vector<PointComparable> pcVec;
for ( IntervalVec::const_iterator it = intvVec.begin();
it != intvVec.end(); ++it )
{
pcVec.push_back( PointComparable( it->m_iStart, 0 ) );
pcVec.push_back( PointComparable( it->m_iEnd, 1 ) );
}

sort( pcVec.begin(), pcVec.end() );

int iMaxCount = 0;
int iCurCount = 0;
for ( vector<PointComparable>::iterator itTemp = pcVec.begin();
itTemp != pcVec.end(); ++itTemp )
{
cout << itTemp->m_iVal << " " << itTemp->m_iType << endl;
if ( itTemp->m_iType == 0 )
{
iCurCount++;
iMaxCount = __max( iCurCount, iMaxCount );
}
else
{
iCurCount--;
}
}

return iMaxCount;
}

int main()
{
IntervalVec intvVec;
//  intvVec.push_back( Interval(1,5) );
//  intvVec.push_back( Interval(5,10) );

//  intvVec.push_back( Interval(1,7) );
//  intvVec.push_back( Interval(2,5) );
//  intvVec.push_back( Interval(3,6) );
//  intvVec.push_back( Interval(8,15) );
//  intvVec.push_back( Interval(9,17) );
//  intvVec.push_back( Interval(20,25) );

intvVec.push_back( Interval(1,2) );
intvVec.push_back( Interval(2,3) );
intvVec.push_back( Interval(3,4) );
intvVec.push_back( Interval(4,5) );

cout << "最大重叠区间个数:" << GetOverlappedIntervalMaxCount( intvVec )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: