使用队列求解杨辉三角的第K层的所有元素

Java代码  

  1. package queue;
  2. import java.util.concurrent.ConcurrentLinkedDeque;
  3. /**
  4. * Created by Lanxiaowei
  5. * Craated on 2016/12/12 9:03
  6. * 求解杨辉三角的第K层的所有元素
  7. * 使用队列求解
  8. */
  9. public class YHTriangleWithQueue {
  10. public static void main(String[] args) throws InterruptedException {
  11. int k = 6;
  12. int[][] nums = kTier(k);
  13. for(int j=0; j < k; j++) {
  14. System.out.print(nums[k % 2][j] + " ");
  15. }
  16. }
  17. 下载
  18. public static int[][] kTier(int k) throws InterruptedException {
  19. int[][] nums = new int[2][k];
  20. if(k <= 0) {
  21. throw new IllegalArgumentException("Argument k MUST be > 0.");
  22. }
  23. ConcurrentLinkedDeque<Event> queue = new ConcurrentLinkedDeque<Event>();
  24. Event head = new Event(1,1);
  25. Event tail = new Event();
  26. int level = 0;
  27. int pos = 0;
  28. queue.add(head);
  29. while (!queue.isEmpty()) {
  30. head = queue.getFirst();
  31. level = head.getLevel();
  32. pos = head.getPos();
  33. if(pos == 1 || pos == level) {
  34. nums[level % 2][pos - 1] = 1;
  35. } else {
  36. nums[level % 2][pos - 1] = nums[(level - 1) % 2][pos - 1] + nums[(level - 1) % 2][pos - 2];
  37. }
  38. if(level < k) {
  39. Event tt = new Event(level + 1,pos);
  40. queue.addLast(tt);
  41. if(pos == level) {
  42. Event t = new Event(tt.getLevel(),pos + 1);
  43. queue.addLast(t);
  44. }
  45. }
  46. queue.pop();
  47. }
  48. return nums;
  49. }
  50. 下载
  51. public static class Event {
  52. //表示第几层
  53. private int level;
  54. //第几位
  55. private int pos;
  56. public Event() {}
  57. public Event(int level, int pos) {
  58. this.level = level;
  59. this.pos = pos;
  60. }
  61. public int getLevel() {
  62. return level;
  63. }
  64. public void setLevel(int level) {
  65. this.level = level;
  66. }
  67. public int getPos() {
  68. return pos;
  69. }
  70. public void setPos(int pos) {
  71. this.pos = pos;
  72. }
  73. }
  74. }
时间: 2024-08-05 15:24:55

使用队列求解杨辉三角的第K层的所有元素的相关文章

用队列打印杨辉三角

#include<iostream> #include<cstdio> #include<queue> #include<algorithm> using namespace std; queue<int> Q; int temp; void tringle(const int n) { Q.push(1); for(int i=2;i<=n;i++) { Q.push(1); for(int j=1;j<=i-2;j++) { te

算法练习之杨辉三角,杨辉三角的第 k 行

1. 杨辉三角 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] java class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> rs = n

数据结构总结系列(四)——循环队列之杨辉三角

今天我们来写一个循环队列的应用哦! 解决的是杨辉三角问题~~ 对于这样一个上下多层之间有密切联系的数据,如果只是用数组和循环来解决的话,显然会浪费大量的空间和时间, 所以我们用队列来解决这一问题: 之所以选用循环队列也是因为它对于空间的利用是非常有效的,方便我们的工作: 开始定义结构体: typedef struct //定义循环队列 { int data[MAXMIZE]; int Front; int Rear; }RollQueue; 这里的最大值(MAXMIZE)大家可以用宏定义来自己定

yhTriangle_LinkQueue(队列实现杨辉三角)

#include"LinkQueue.h" void yhTriangle(int n) { LinkQueue<int> A; int s,t; A.Inqueue(1);A.Inqueue(1); cout<<1<<endl; cout<<1<<"\t"<<1<<endl; for(int i=3;i<=n;i++) { A.Inqueue(1); A.Outqueue(s

给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。

从第0行开始,输出第k行,传的参数为第几行,所以在方法中先将所传参数加1,然后将最后一行加入集合中返回. 代码如下: public static List<Integer> generateII(int row){ ++row; List<Integer> list = new ArrayList<Integer>(); int[][] arr = new int[row][row]; for(int j = 0;j<row;j++) { for(int k =

数据结构上机4队列-杨辉三角2

#include <stdio.h> #include <malloc.h> #define OK 1 #define OVERFLOW -1 #define ERROR 0 typedef int Status, QElemType; //队列结构定义 typedef struct { QElemType *base; int front; int rear; } SqQueue; //初始化队列 Status InitQueue(SqQueue *Q, int Qsize) {

数据结构上机4队列-杨辉三角1

#include <stdio.h> #include <malloc.h> #define OK 1 #define OVERFLOW -1 #define ERROR 0 typedef int Status, QElemType; typedef struct { QElemType *base; int front; int rear; } SqQueue; Status InitQueue(SqQueue *Q, int Qsize) { Q->base = (QE

杨辉三角——队列的应用

杨辉三角形是形如: 1 1   1 1   2   1 1   3   3   1 1   4   6   4   1 的三角形,其实质是二项式(a+b)的n次方展开后各项的系数排成的三角形,它的特点是左右两边全是1,从第二行起,中间的每一个数是上一行里相邻两个数之和. 使用<队列>的思想来实现杨辉三角的流程: 1>首先,需要初始化一个队列,即对头=队尾=0: 2>将第一行的元素1入队,接着操作第二行(一二行不需要求和操作,直接将元素入队即可): 3>从第三行开始,现在的对头

LeetCode Pascal&#39;s Triangle II (杨辉三角)

题意:给出杨辉三角的层数k,返回最后一层.k=0时就是只有一个数字1. 思路:滚动数组计算前一半出来,返回时再复制另一半.简单但是每一句都挺长的. 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 if(rowIndex==0) return vector<int>(1,1); //0和1特殊处理 5 if(rowIndex==1) return vector<int>(2,1); 6