有一批数据 
[1,300,20,21,22,24,25,50,51,53,23,20001]
需要返回  
[1]
[300]
[20,25]
[50,53]
就是连续的合并只要首尾, 不连续的返回单个数字, 数据不定.  

解决方案 »

  1.   

    Resolved, thank you!
    I will give the points to somebody who can give the reasonable idea.
      

  2.   


    public static void main(String[] args) throws Exception 

    int a[] ={1,300,20,21,22,24,25,50,51,53,23,24};//数组
    for(int i=0;i<a.length;i++)
    {
    boolean flag = true;//判断之加顺序的第一个数的标志位
    while(i<a.length)
    {
    if(i<a.length-1&&a[i]+1==a[i+1])//如果连续,同时没到最后一个数
    {
    if(flag)
    {
    System.out.print(a[i]);//则把第一个顺序的数加进去
    flag = false;//状态位改为false以后的数就进不来了
    }
    i++;//继续往后数
    }
    else
    {
    System.out.println(a[i]);//当不符合顺序的时候,把当前的数加近来,此时他是顺序的最后一位
    break;
    }
    }
    }
      

  3.   

    i=0;j=i+1; 
    while(j<a.length){
       while(a[i]<=a[j]){
          j++;
       }
       if(i==j){
         //只有一个数据.
       }else{
         //a[i]是头,a[j]是尾,中间是有序的.
       }
       j=i;  //继续找下一组.
       j++;
    }
      

  4.   

    非常粗略的写了一个,我在上班,没办法就精,结果出来了:import java.util.ArrayList;
    import java.util.List;public class TestMain { 
    public static void main(String[] args) {
    int[] testData = new int[]{1,300,20,21,22,24,25,50,51,53,23,20001};
    List totalData = new ArrayList();//放整理后的数据
    List tempList = null;//中间变量,临时存放整理好一组的数据
    Integer startData = null;//中间变量,临时存放整理好一组数据中的起始数字
    Integer endData = null;//中间变量,临时存放整理好一组数据中的结束数字
    for(int i = 0; i < testData.length; i++) {
    if(startData == null) {//如果起始数为空
    startData = new Integer(testData[i]);//以当前循环数为起始数
    }
    if(i < testData.length - 1) {//如果本数字不是数组中最后一个
    if(endData == null) {//如果本组还没有结束数
    if(testData[i]+1 == testData[i+1]) {//那看是不是连续的
    endData = new Integer(testData[i+1]);//如果是连续的,将本数字的下一个做为结束数
    } else {//如果不是连续的,开始存到totalData中,并将所有临时变量设null
    if(tempList == null){
    tempList = new ArrayList();
    }
    tempList.add(startData);
    if(endData != null) {
    tempList.add(endData);
    }
    totalData.add(tempList);
    tempList = null;
    startData = null;
    endData = null;
    }
    } else {//如果本组数已经有结束数了,那接着往下判断,看后面还没有连续数了,如果有,替换上次设的结束数
    if(endData.intValue() + 1 == testData[i+1]) {
    endData = new Integer(testData[i+1]);
    } else {
    if(tempList == null){
    tempList = new ArrayList();
    }
    tempList.add(startData);
    if(endData != null) {
    tempList.add(endData);
    }
    totalData.add(tempList);
    tempList = null;
    startData = null;
    endData = null;
    }
    }
    } else {//如果已经到最后一个数了,把上一组数没做完的事做完
    if(tempList == null) {
    tempList = new ArrayList();
    }
    if(startData == null) {
    startData = new Integer(testData[i]);
    }
    tempList.add(startData);
    if(endData != null) {
    tempList.add(endData);
    }
    totalData.add(tempList);
    }
    }
    for(int i = 0; i < totalData.size(); i++) {
    List tempData = (List)totalData.get(i);
    for(int j = 0; j < tempData.size(); j++) {
    System.out.print(tempData.get(j) + ",");
    }
    System.out.println();
    }
    }
    }
      

  5.   

    改正一下:
    i=0;j=i+1; 
    while(i<a.length){
       while(a[i]<=a[j]&&j<a.length){
          j++;
       }
       if(i==j){
         //只有一个数据.做相应的处理
       }else{
         //a[i]是头,a[j]是尾,中间是有序的.做相应的处理
       }
       j=i;  //继续找下一组.
       j++;
    }
      

  6.   

    4楼的其实并不符合lz的要求!
    lz其实还是没有表达清楚!
    我的理解是:在一组数字中当出现有连续数字的时候就将这段连续数字的第一个数和最后一个数输出!否则直接输出!
    不知我的理解对否!……lz?
      

  7.   

    再改一下:i=0;j=i+1; 
    while(i<a.length){
       while(j+1<a.length&&a[j]<=a[j+1]){
          j++;
       }
       if(i==j){
         //只有一个数据.做相应的处理
       }else{
         //a[i]是头,a[j]是尾,中间是有序的.做相应的处理
       }
       i=j+1;  //继续找下一组.
       j=i+1;
    }
      

  8.   


    Sorry, you are right, the input data should be [1,300,20,21,22,24,25,50,51,52,53,23,20001],
     and the out put date:
    [1] 
    [300] 
    [20,25] 
    [50,53] 
    [20001]Thank you lwb314, but you result is
    1
    300
    2022
    2425
    5051
    53
    2324
     I need combine the 20,21,22,23,24,25 as (20,25).For some reason, I can not input Chinese character, hope you can understand.
      

  9.   

    如果按照你的要求那么我写对了楼主,注意我的数组,中间是少个23的,断开了,
    如果按照你说的I need combine the 20,21,22,23,24,25 as (20,25). 
    中间有23的话结果是(20,25)
      

  10.   

    int a[] ={1,300,20,21,22,23,24,25,50,51,52,53,46,20001};
    如果数组是这个,结果就出来了,除非你说的连续不是相差1的意思,我是按相差1做的算法
      

  11.   

    LZ的意思是即使后面出现了23这个数,虽然没有紧跟着22后出来,但确实是在这组数中的,还是算的,还是要算到20-25中,LZ,是这个意思吗?
      

  12.   

    那就这样:
    i=0;j=i; //把i,和j看成两个指针,i指向连续数字的第一个,j指向最后那个.
    while(i<a.length){
       while(j+1<a.length&&a[j]+1=a[j+1]){
          j++;
       }
       if(i==j){
         //只有一个数据.做相应的处理,比如打印出来,System.out.println(a[i]);
       }else{
         //a[i]是头,a[j]是尾,中间的数是连续.做相应的处理 比如打印出来,System.out.println("["+a[i]+"],["+
    a[j])
       }
       i=j+1;  //继续找下一组.
       j=i;
    }
      

  13.   

    public class TestSort {
    static int[] a={1,300,20,21,22,24,25,50,51,53,23,20001};
    static List list = getList(a);
    static List getList(int[] a){
    List<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < a.length; i++) {
    list.add(a[i]);
    }
    Collections.sort(list);
    return list;
    }
    public static void getInt(){
    for (int i = list.size()-1; i >0; i--) {
    if((Integer)list.get(i)==(Integer)list.get(i-1)+1){
    //System.out.println((Integer)list.get(i));
    //如果相邻的两个差值为1保留前一个
    list.remove(i);
    }
    }
    }
    public static void main(String[] args) {
    getInt();
    for (int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i).toString());
    }
    }
    }输出:
    1
    20
    50
    53
    300
      

  14.   

    感觉20楼越改越像我的,但不知道为什么,看看他的我又有了个新的想法
    去掉最外边的循环,只用一个循环,不知道是否可以稍微提要点效率,下边是我改进的代码 public static void main(String[] args) throws Exception 

    int a[] ={1,300,20,21,22,23,24,25,50,51,52,53,46,20001};
     boolean flag = true;//判断只加,顺序的第一个数,的标志位
     int i=0;
             while(i<a.length)
             {
                 if(i<a.length-1&&a[i]+1==a[i+1])//如果连续,同时没到最后一个数
                 {
                     if(flag)
                     {
                         System.out.print(a[i]);//则把第一个顺序的数加进去
                            flag = false;//状态位改为false以后的数就进不来了
                       }                    
                     i++;//继续往后数                    
                 }
                 else
                 {
                     System.out.println(a[i]);//当不符合顺序的时候,把当前的数加近来,此时他是顺序的最后一位
                     i++;     
                     flag = true;//加完顺序的最后一个数标志位再改为true
                 }
             }
    }
      

  15.   


    import java.util.Arrays;
    public class Test { /**
     * @param args
     */
    public static void main(String[] args) {
    boolean b=true;
    boolean c=false;
    int []a=new int[]{1,300,20,21,22,24,25,50,51,53,23,20001};
    Arrays.sort(a);
    for(int i=0;i<a.length-1;i++){
    if(b){
    System.out.print("[");
    System.out.print(a[i]);}
    if(a[i+1]==a[i]+1){
    b=false;
    c=true;
    continue;

    }
    else{


    }
    if(c){
    System.out.print(",");
    System.out.print(a[i]);

    }
    System.out.print("]");
    System.out.println();
    b=true;
    c=false;
    } }}
      

  16.   

    shi de!Yes,that's what I am trying to say.
      

  17.   


    That's what I did, Thank you!
    Any better suggestion?
      

  18.   

    这就有点难度了,我快下班了,时间不一定够,思路是还是用List来存,最好再建个类,放首尾两个数,List中再放这个类的对象,后面遍历的过程中方便再次拿出来做比较,因为这个List中的对象值在后面的遍历中还有可能被替换掉,1,300,20,21,22,24,25,50,51,53,23,20001这样一组数最好也存放在List中,这样每存一个可以移除一个(从性能来考虑的,因为按照楼主的要求,这时每次都要从这个List中从头到尾的遍历,除非已经加入到另一个List中),这时还有另一个问题需要考虑:如果20出现在后面,这样从前面遍历,会出现21打头,25结尾,这时再出现20,还要考虑把21替换掉。我先说这么多,等我到家再写程序。
      

  19.   

    楼主给我的回复有语法错误,,代码写的有点繁琐,,,,技术问题,,哈哈
    That's what I did, Thank you! 
      

  20.   

    楼主我最后一个没有算,,,i am sorry
      

  21.   

      测试通过,大家太不厚道了,给个system.out.println的lz怎么用呢?
    我来给个返回List<int[]>的

    输入int[] a = {1,300,20,0,21,22,24,50,51,53,23,20001,20002};
    输出[1][300][20][0][21, 22][24][50, 51][53][23][20001, 20002] public static List<int[]> getResult(int a[]) {
    List<int[]> result = new ArrayList<int[]>();
    boolean ok=false;//是否输出
    boolean flag=false;//是否连续
    int left = 0;
    int right = 0;
    for (int i = 0; i < a.length; i++) {
    if(flag){//连续,
    right=a[i];
    if(i!=a.length-1&&right+1!=a[i+1]){
    ok=true;
    }
    }else{//非连续
    left=a[i];
    if(i!=a.length-1&&left+1==a[i+1]){
    flag=true;
    }else{
    ok=true;
    }
    }

    if(ok||i==a.length-1){//输出
    if(flag){
    int[] temp = new int[2];
    temp[0] = left;
    temp[1] = right;
    result.add(temp);
    right=0;
    }else{
    int[] temp = new int[1];
    temp[0]=left;
    result.add(temp);
    }
    left=0;
    ok=false;
    flag=false;
    }
    }
    return result;
    }
      

  22.   

    引用 19 楼 ltandfyy 的回复: 
    LZ的意思是即使后面出现了23这个数,虽然没有紧跟着22后出来,但确实是在这组数中的,还是算的,还是要算到20-25中,LZ,是这个意思吗?
    不好意思,没看清楚。
      

  23.   

    写了一个 public static void main(String[] args) { 
            int a[] ={1,300,20,21,22,24,25,50,51,53,23,24};
            
            int last = a[0]; // 记录上一个元素
            int count = 1; // 记录连续数字的个数
            System.out.print("[" + a[0]);
            for(int i=1;i<a.length;i++) {
             if(a[i] == last + 1) {
             count ++;
             } else {
             if(count > 1)
             System.out.print(", " + last);
             System.out.print("]\n[" + a[i]);
             count = 1;
             }
             last = a[i];
            }
            System.out.println("]");
        }
    输出
    [1]
    [300]
    [20, 22]
    [24, 25]
    [50, 51]
    [53]
    [23]
      

  24.   

    package com.ricky.www;/**
     * 
     * 有一批数据 [1,300,20,21,22,24,25,50,51,53,23,20001] 需要返回 [1] [300] [20,25] [50,53]
     * 就是连续的合并只要首尾, 不连续的返回单个数字, 数据不定.
     * 
     */
    public class Test {
    public static void main(String[] args) {
    int[] array = { 1, 300, 20, 21, 22, 24, 25, 50, 51, 53, 23, 20001 };
    sort(array);
    String result = coordinate(array);
    System.out.println(result);
    } public static void sort(int[] array) {
    int length = array.length; for (int i = 0; i < length; i++) {
    int temp = array[i];
    for (int j = i; j < length; j++) {
    if (temp > array[j]) {
    temp = array[j];
    array[j] = array[i];
    array[i] = temp;
    }
    }
    }
    } public static String coordinate(int[] array) {
    int length = array.length;
    int start = 0;
    int end = 0;
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < length; i++) {
    start = array[i];
    end = array[i];
    int j = 1;
    int k = 0;
    for (; j < length - i - 1; j++) {
    if (array[i] + j == array[i + j]) {
    end = array[i + j];
    k ++;
    }
    }

    i = i + k;
    if (start == end) {
    buffer.append("[" + start + "]\n");
    } else {
    buffer.append("[" + start + "," + end + "]\n");
    }
    }
    return buffer.toString();
    }
    }
    [1]
    [20,25]
    [50,51]
    [53]
    [300]
    [20001]
      

  25.   

    简单写了一个算法,代码还可以优化一些。运行结果应该没错。package houlei.test;/**
     * 有一批数据 [1,300,20,21,22,24,25,50,51,53,23,20001] 
     * 需要返回 
     * [1] 
     * [300] 
     * [20,25] 
     * [50,53] 
     * 就是连续的合并只要首尾,
     * 不连续的返回单个数字, 数据不定. 
     * 
     * 该类创建于 2009-6-11 23:21:21
     * 
     * @version 1.0.0
     * @author 侯磊
     */
    public class FindNumber { private static enum State{Single,First,Last};//用于标明匹配状态
    private final static int Default=0;//设置一个特殊的默认值,该值不参与运算。

    public static void find(int array []){
    //为算法简便,构建一个数组。
    int temp [] = new int[array.length+1];
    System.arraycopy(array, 0, temp, 0, array.length);
    temp[array.length]=Default;
    array=temp;
    //初始化默认值
    State state=State.Single;
    int first=Default;
    //循环遍历数组并输出结果
    for(int i=1;i<array.length;i++){
    if(array[i-1]==array[i]-1){
    switch(state){
    case Single :
    first=array[i-1];
    state=State.First;
    continue;
    case First:
    continue;
    case Last:
    continue;
    }
    }else{
    switch(state){
    case Single :
    print(array[i-1]);
    continue;
    case First:
    state=State.Last;
    continue;
    case Last:
    state=State.Single;
    print(first,array[i-1]);
    continue;
    }
    }
    }
    }

    public static void main(String[] args) {
    int array [] = new int[]{1,300,20,21,22,24,25,50,51,53,23,20001};
    find(array);
      }

    public static void print(int i){
    System.out.println("["+i+"]");
    }
    public static void print(int i,int j){
    System.out.println("["+i+","+j+"]");
    }
    }
      

  26.   

    不会好算法,只能先排序再求输出了 public static void main(String[] args) throws Exception 

    int a[] ={1,50,300,21,25,22,23,24,20,51,52,53,46,20001};
    /*
     * 排顺
     */
    int k=0;
    while(k<a.length)
    {
    if(k<a.length-1&&a[k]>a[k+1])
    {
    a[k] = a[k]^a[k+1];
    a[k+1] = a[k]^a[k+1];
    a[k] = a[k]^a[k+1];
    k=0;
    }
    k++;
    }
    /*
     * 排顺后输出结果
     */
     boolean flag = true;//判断只加,顺序的第一个数,的标志位
     int i=0;
             while(i<a.length)
             {
                 if(i<a.length-1&&a[i]+1==a[i+1])//如果连续,同时没到最后一个数
                 {
                     if(flag)
                     {
                         System.out.print(a[i]);//则把第一个顺序的数加进去
                         flag = false;//状态位改为false以后的数就进不来了
                     }                    
                     i++;//继续往后数                    
                 }
                 else
                 {
                     System.out.println(a[i]);//当不符合顺序的时候,把当前的数加近来,此时他是顺序的最后一位
                     i++;     
                     flag = true;//加完顺序的最后一个数标志位再改为true
                 }
             }
    }
      

  27.   

    排顺有点错误while(k<a.length)
    {
    if(k<a.length-1&&a[k]>a[k+1])
    {
    a[k] = a[k]^a[k+1];
    a[k+1] = a[k]^a[k+1];
    a[k] = a[k]^a[k+1];
    k=0;
    continue;
    }
    k++;
    }
      

  28.   

    public static void main(String[] args) {
       int end = 0;      //邻数首
        int head =0;     //邻数尾
        int join =2;     //邻数<join视为连续数(如:1,2,5)
       StringBuffer sb = new StringBuffer();
       int num[] = new int[]{1,300,20,21,22,24,25,50,51,53,23,20001};
       Arrays.sort(num);//排顺
       for(int j=0; j<=num.length; j++){
    if(j+1<num.length){
       /*下一下标数-去当前下标数<=邻数(join),就视为连续数*/
       if(num[j+1]-num[j]<=join){
    if(head==0){
    head = num[j];//保存邻数首
    }
       }else{
    end = num[j];
    /*取得[邻数首]和[邻数尾]*/
    if(head!=0 && end!=0){
    sb.append("["+head+","+end+"]\n");
    head = 0;
    end = 0;
    }else{/*剩余的为单一数(无邻数)*/
    sb.append("["+num[j]+"]\n");
    }
    }
     }
        }
        System.out.println(sb);
    }
      

  29.   


      public static void main(String[] args) {
    // TODO Auto-generated method stub

    int[] nums = new int[]{1,300,21,22,24,25,50,51,23,53,30001};

    /**
     * 排序
     */
    int count = nums.length;
    for(int i=0;i<count;i++){
    for(int j=i+1;j<count;j++){
    if(nums[i] > nums[j]){
    int temp = nums[i];
    nums[i] = nums[j];
    nums[j] = temp;
    }
    }
    }

    /**
     * 找到连续的(排序后的)
     */
    int i = 0;
    while(i<count)
    {
    int num = nums[i];
    if(i<count-1){
    if((num+1) == nums[i+1]){
    System.out.print(num+",");     //输出连续首
    int j = i+1;
    for(;j<count-1;j++){
    if(nums[j]+1 < nums[j+1]){
    System.out.print(nums[j]);    //输出连续尾
    System.out.println();
    i = j;
    break;
    }
    }
    }
    else
    System.out.println(num);    //未连续单独输出
    }
    i++;
    }

    }不过输出后就已经排序过了...
      

  30.   

    class HeadTail{
    public static void main (String[] arg){
    int head = 0;
    int tail = 0;
    int count = 0; //连续下一个的个数
    int[] data = {1,300,20,21,22,23,24,25,50,51,52,53,23,20001};
    for(int i = 0;i < data.length; i++){
    head = data[i];
    System.out.print(head);
    while(data.length>i+1 && data[i+1]==data[i]+1 ){
    tail = data[i+1];
    i++;
    count++;
    }
    if(count != 0){
    System.out.print(tail);
    count = 0;
    }
      System.out.println();

    }
    }
    }
    --------------------Configuration: <Default>--------------------
    1
    300
    2025
    5053
    23
    20001Process completed.
      

  31.   

    class HeadTail{
              public static void main (String[] arg){
    int head = 0;
    int tail = 0;
    int count = 0; //连续下一个的个数
    int[] data = {1,300,20,21,22,23,24,25,50,51,52,53,23,20001};
    for(int i = 0;i < data.length; i++){
    head = data[i];
    System.out.print(head);
    while(data.length>i+1 && data[i+1]==data[i]+1 ){
    tail = data[i+1];
    i++;
    count++;
    }
    if(count != 0){
    System.out.print(tail);
    count = 0;
    }
      System.out.println();

    }
    }
    }--------------------Configuration: <Default>--------------------
    1
    300
    2025
    5053
    23
    20001Process completed.