上一页 1 ··· 19 20 21 22 23 24 25 26 27 ··· 35 下一页
摘要: 1.4.1证明从N个数中取三个整数的不同组合的总数为N(N-1)(N-2)/6。提示:使用数学归纳法。答:基础步骤:N>=3时才有可能有三个整数的组合,将N=3代入上式得3*2*1/6=1,而N=3时有且只有一个三整数组合,基础步骤成立。归纳步骤:当N=k时不同的组合个数为C(k,3)= k(k-1 阅读全文
posted @ 2018-10-26 08:56 修电脑的龙生 阅读(979) 评论(0) 推荐(0) 编辑
摘要: public class DoublingRatio{ public static double timeTrial(int N) { int MAX=1000000; int[] a=new int[N]; for(int i=0;i<N;i++) a[i]=StdRandom.uniform(- 阅读全文
posted @ 2018-10-26 08:54 修电脑的龙生 阅读(131) 评论(0) 推荐(0) 编辑
摘要: import java.util.Arrays;public class TwoSumFast{ public static int count(int[] a) { Arrays.sort(a); int N=a.length; int cnt=0; for(int i=0;i<N;i++) if 阅读全文
posted @ 2018-10-26 08:53 修电脑的龙生 阅读(150) 评论(0) 推荐(0) 编辑
摘要: import java.util.Arrays;public class ThreeSumFast{ public static int count(int[] a) { Arrays.sort(a); int N=a.length; int cnt=0; for(int i=0;i<N;i++) 阅读全文
posted @ 2018-10-26 08:53 修电脑的龙生 阅读(117) 评论(0) 推荐(0) 编辑
摘要: public class ThreeSum{ public static int count(int[] a) { int N=a.length; int cnt=0; for (int i=0;i<N;i++) for (int j=i+1;j<N;j++) for(int k=j+1;k<N;k 阅读全文
posted @ 2018-10-26 08:52 修电脑的龙生 阅读(83) 评论(0) 推荐(0) 编辑
摘要: public class TwoSum{ public static int count(int[] a) { int N=a.length; int cnt=0; for (int i=0;i<N;i++) for (int j=i+1;j<N;j++) if(a[i]+a[j]==0) cnt+ 阅读全文
posted @ 2018-10-26 08:52 修电脑的龙生 阅读(90) 评论(0) 推荐(0) 编辑
摘要: 1.3.50快速出错的迭代器。修改Stack的迭代器代码,确保一旦用例在迭代器中(通过push()或pop()操作)修改集合数据就抛出一个java.util.ConcurrentModificationException异常。解答:用一个计数器记录push()和pop()操作的次数。在创建迭代器时, 阅读全文
posted @ 2018-10-26 08:51 修电脑的龙生 阅读(317) 评论(0) 推荐(0) 编辑
摘要: 早期版本题目:1.3.49 Queue with three stacks. Implement a queue with three stacks so that each queue operation takes a constant (worst-case) number of stack 阅读全文
posted @ 2018-10-26 08:50 修电脑的龙生 阅读(266) 评论(2) 推荐(0) 编辑
摘要: public class QueueWithTwoStack<Item>{ Stack<Item> s1=new Stack<Item>(); Stack<Item> s2=new Stack<Item>(); public boolean isEmpty() { return s1.isEmpty 阅读全文
posted @ 2018-10-26 08:49 修电脑的龙生 阅读(194) 评论(0) 推荐(0) 编辑
摘要: 1.3.47可连接的队列、栈或steque。为队列、栈或steque(请见练习1.3.32)添加一个能够(破坏性地)连接两个同类对象的额外操作catenation。答:Queue 代码:import java.util.Iterator;public class Queue<Item> implem 阅读全文
posted @ 2018-10-26 08:47 修电脑的龙生 阅读(259) 评论(0) 推荐(0) 编辑
上一页 1 ··· 19 20 21 22 23 24 25 26 27 ··· 35 下一页