使用隊列求解楊輝三角的第K層的所有元素

來源:互聯網
上載者:User

標籤:druid   springmvc   mybatis   maven   bootstrap   

Java代碼  650) this.width=650;" class="star" src="http://iamyida.iteye.com/images/icon_star.png" alt="收藏代碼" style="border:0px;" />

  1. package queue;  

  2.   

  3. import java.util.concurrent.ConcurrentLinkedDeque;  

  4.   

  5. /** 

  6.  * Created by Lanxiaowei 

  7.  * Craated on 2016/12/12 9:03 

  8.  * 求解楊輝三角的第K層的所有元素 

  9.  * 使用隊列求解 

  10.  */  

  11. public class YHTriangleWithQueue {  

  12.     public static void main(String[] args) throws InterruptedException {  

  13.         int k = 6;  

  14.         int[][] nums = kTier(k);  

  15.   

  16.         for(int j=0; j < k; j++) {  

  17.             System.out.print(nums[k % 2][j] + " ");  

  18.         }  

  19.     }  

  20.   下載

  21.     public static int[][] kTier(int k) throws InterruptedException {  

  22.         int[][] nums = new int[2][k];  

  23.         if(k <= 0) {  

  24.             throw new IllegalArgumentException("Argument k MUST be > 0.");  

  25.         }  

  26.         ConcurrentLinkedDeque<Event> queue = new ConcurrentLinkedDeque<Event>();  

  27.         Event head = new Event(1,1);  

  28.         Event tail = new Event();  

  29.         int level = 0;  

  30.         int pos = 0;  

  31.         queue.add(head);  

  32.         while (!queue.isEmpty()) {  

  33.             head = queue.getFirst();  

  34.             level = head.getLevel();  

  35.             pos = head.getPos();  

  36.             if(pos == 1 || pos == level) {  

  37.                 nums[level % 2][pos - 1] = 1;  

  38.             } else {  

  39.                 nums[level % 2][pos - 1] = nums[(level - 1) % 2][pos - 1] + nums[(level - 1) % 2][pos - 2];  

  40.             }  

  41.             if(level < k) {  

  42.                 Event tt = new Event(level + 1,pos);  

  43.                 queue.addLast(tt);  

  44.                 if(pos == level) {  

  45.                     Event t = new Event(tt.getLevel(),pos + 1);  

  46.                     queue.addLast(t);  

  47.                 }  

  48.             }  

  49.             queue.pop();  

  50.         }  

  51.         return nums;  

  52.     }  

  53.   下載

  54.     public static class Event {  

  55.         //表示第幾層  

  56.         private int level;  

  57.         //第幾位  

  58.         private int pos;  

  59.   

  60.         public Event() {}  

  61.   

  62.         public Event(int level, int pos) {  

  63.             this.level = level;  

  64.             this.pos = pos;  

  65.         }  

  66.         public int getLevel() {  

  67.             return level;  

  68.         }  

  69.   

  70.         public void setLevel(int level) {  

  71.             this.level = level;  

  72.         }  

  73.   

  74.         public int getPos() {  

  75.             return pos;  

  76.         }  

  77.   

  78.         public void setPos(int pos) {  

  79.             this.pos = pos;  

  80.         }  

  81.     }  

  82. }  


使用隊列求解楊輝三角的第K層的所有元素

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.