春風(fēng)博客

          春天里,百花香...

          導(dǎo)航

          <2008年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          統(tǒng)計(jì)

          公告

          MAIL: junglesong@gmail.com
          MSN: junglesong_5@hotmail.com

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個(gè)人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          判斷數(shù)組元素是否存在重復(fù),要求時(shí)間復(fù)雜度為O(1)

          下面的代碼涉及判斷數(shù)組元素是否存在重復(fù),要求時(shí)間復(fù)雜度為O(1)。
          這樣的題肯定不能用雙循環(huán)比較,這樣太慢,用hashcode判斷是正道,使用現(xiàn)成的hashset更能簡(jiǎn)化代碼。

          代碼如下:
          package com.sitinspring;

          import java.util.HashSet;
          import java.util.Set;

          /**
           * 數(shù)組重復(fù)測(cè)試,要求時(shí)間復(fù)雜度為O(n)
           * 
          @author sitinspring(junglesong@gmail.com)
           * 
          @since 2008-6-11 上午11:12:53
           * @vsersion 1.00 創(chuàng)建 sitinspring 2008-6-11 上午11:12:53
           
          */

          public class ArrayDuplicateTest{
              
          /**
               * 構(gòu)造函數(shù)
               *
               
          */

              
          public ArrayDuplicateTest(int[] arr){
                  System.out.print(
          "數(shù)組:");
                  
          for(int temp:arr){
                      System.out.print(temp
          +",");
                  }

                  
                  
          if(hasDuplicateItem(arr)==false){
                      System.out.println(
          "無(wú)重復(fù)結(jié)果");
                  }

                  
          else{
                      System.out.println(
          "有重復(fù)結(jié)果");
                  }

              }

              
              
          /**
               * 取得檢測(cè)結(jié)果
               * 
          @param arr
               * 
          @return
               
          */

              
          private boolean hasDuplicateItem(int[] arr){
                  Set
          <Integer> set=new HashSet<Integer>();
                  
                  
          for(int i:arr){
                      
          if(!set.add(i)){
                          
          return true;
                      }

                  }

                  
                  
          return false;
              }

              
              
          public static void main(String[] args){
                  
          int[] arr1={1,2,3,4,5};
                  
          new ArrayDuplicateTest(arr1);
                  
                  
          int[] arr2={1,2,3,4,5,5,53,43,42,2,454,6,5456,4534,4};
                  
          new ArrayDuplicateTest(arr2);
                  
                  
          int[] arr3={1,2,3,4,5,767,4332,534,76,6583,356};
                  
          new ArrayDuplicateTest(arr3);
              }

          }

          輸出:
          數(shù)組:1,2,3,4,5,無(wú)重復(fù)結(jié)果
          數(shù)組:
          1,2,3,4,5,5,53,43,42,2,454,6,5456,4534,4,有重復(fù)結(jié)果
          數(shù)組:
          1,2,3,4,5,767,4332,534,76,6583,356,無(wú)重復(fù)結(jié)果

          posted on 2008-06-11 11:44 sitinspring 閱讀(3093) 評(píng)論(1)  編輯  收藏 所屬分類: Java基礎(chǔ)算法數(shù)據(jù)結(jié)構(gòu)

          評(píng)論

          # re: 判斷數(shù)組元素是否存在重復(fù),要求時(shí)間復(fù)雜度為O(1) 2013-07-12 14:43 Ricky

          這個(gè)時(shí)間復(fù)雜度不是O(1)吧,HashSet內(nèi)吧的判定也影響時(shí)間復(fù)雜度的!  回復(fù)  更多評(píng)論   

          sitinspring(http://www.aygfsteel.com)原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處.
          主站蜘蛛池模板: 巴塘县| 神池县| 红桥区| 安西县| 沅陵县| 南丹县| 永安市| 塘沽区| 株洲县| 河西区| 光山县| 宣化县| 黄冈市| 绿春县| 巴南区| 类乌齐县| 宁陵县| 榆林市| 综艺| 绿春县| 弥勒县| 陆河县| 宣恩县| 新丰县| 铅山县| 化德县| 中卫市| 大兴区| 河东区| 色达县| 平南县| 化德县| 体育| 夏津县| 乌拉特中旗| 正镶白旗| 若羌县| 姜堰市| 昭平县| 吴桥县| 亳州市|