码迷,mamicode.com
首页 > 其他好文 > 详细

the arithmetic sequence of integers

时间:2017-12-10 11:13:54      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:xtend   nta   you   tin   tor   void   instant   col   call   

 

Provide an implementation of the following interface: 
public interface Powers extends Iterator<Long> 
{ 
/* Returns the next integer a in the arithmetic sequence of integers where 
* a = m^n, m > 1 n > 1, and m and n are both integers 
* Thus, the first few outputs will be 4, 8, 9, 16, 25, 27, 32, 36, etc. 
*/ 

public Long next(); 

/* Resets the sequence to the beginning, such that the next call to next() 
* will return 4. 
*/ 
public void reset(); 
}

 

1) Use a minheap 
2) Upon reset (or instantiation), make sure heap is cleared with 4 on the top with {2,2} for m^n as metadata. 
3) When next is called, pop the min and add 8 {2,3} and 9 {3,2} 
4) Repeat for {m,n+1} when you pop {m,n}. If a {m,2} is popped, then add {m+1,2} 
5) For any existing values, push {m,n+2} instead 
6) Rinse and repeat for each next().

  

the arithmetic sequence of integers

标签:xtend   nta   you   tin   tor   void   instant   col   call   

原文地址:http://www.cnblogs.com/apanda009/p/8016120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!