Data Structures and Algorithm Analysis in Java_IntroductionTest
2014-05-23 14:17
513 查看
数据结构与算法分析 第一章
工作一段时间了,准备写点博客,一点点积累知识.最近打算学学数据结构,第一个博客,用简单的练练手.
引论
递归的四条基本法则
1. 基准情况。必须总要有某些基准情况,它无需递归就能解出。
2. 不断推进。对于那些需要递归求解的情况,每一次递归调用都必需要使状况朝向一种基准情况推进。
3. 设计法则。假设所有的递归调用都能运行。
4. 合成效益法则。在求解一个问题的同一实例时,切勿在不同的递归调用中做重复性的工作。
Java泛型
class MyClass<AnyType extends Comparable<? super AnyType>>
1.5 编写一种递归方法,它返回数N的二进制表示中1的个数.利用这样的事实:如果N是奇数,那么其1的个数等于N/2的二进制表示中1的个数加1
1.6 字符串全排列
里面用到递归和for循环,for循环是横向展开,递归是纵向深入.
工作一段时间了,准备写点博客,一点点积累知识.最近打算学学数据结构,第一个博客,用简单的练练手.
引论
递归的四条基本法则
1. 基准情况。必须总要有某些基准情况,它无需递归就能解出。
2. 不断推进。对于那些需要递归求解的情况,每一次递归调用都必需要使状况朝向一种基准情况推进。
3. 设计法则。假设所有的递归调用都能运行。
4. 合成效益法则。在求解一个问题的同一实例时,切勿在不同的递归调用中做重复性的工作。
Java泛型
class MyClass<AnyType extends Comparable<? super AnyType>>
1.5 编写一种递归方法,它返回数N的二进制表示中1的个数.利用这样的事实:如果N是奇数,那么其1的个数等于N/2的二进制表示中1的个数加1
/** * for test 1.5 求出数n在二进制表示中j出现的个数 * @param n */ private static void five(int n){ int i,j,k; // i = forfive(n); // System.out.println("数"+ n + "的二进制表示中1的个数是" + i); j = 10; k=3; i = forfive2(n,j,k); System.out.println("十进制数" +n + "用"+ j + "进制表示时" + k + "的个数是" + i ); } private static int forfive(int n){ //递归 利用这样的事实:如果n是奇数,则其1的个数是n/2的个数加1 // if(n<2)return n; // return forfive(n/2) + n%2; //循环 int i = 0; while(n>0){ if(n%2==1) i++; n = n/2; } return i; } //拓展 n用i进制表示时数字j出现的次数j private static int forfive2(int n,int i,int j){ // //递归 // if(n<i) return n==j ? 1:0; // return forfive2(n/i,i,j) + (n%i==j ? 1:0); // 循环 int k = 0; while(n>0){ if(n%i==j) k++; n = n/i; } return k; }
1.6 字符串全排列
里面用到递归和for循环,for循环是横向展开,递归是纵向深入.
/** * for 1.6 字符串全排列 * @param str */ private static void six(String str){ // 使用字符数组 char[] st = str.toCharArray(); int low = 0; int hight = st.length; forsix(st,low,hight); // ArrayList li = new ArrayList(); // ArrayList li2 = new ArrayList(); // for(int i=0;i<str.length();i++){ // li.add(str.charAt(i)); // } // forsix2(li,li2); } //递归 使用list private static void forsix2(ArrayList list,ArrayList list2){ for(int i = 0;i<list.size();i++){ ArrayList li = (ArrayList) list.clone(); ArrayList li2 = (ArrayList) list2.clone(); li2.add(li.get(i)); li.remove(i); if(li2.size()==3){ System.out.println(li2); return; } forsix2(li,li2); } } //递归 使用字符数组先确定第low位,后面的全排列 类推 private static void forsix(char[] str,int low,int hight){ if(low==hight){ System.out.println(str); return; } char temp; for(int i= low;i<str.length;i++){ char[] str2 = str.clone();//每次使用替身去处理,省去频繁的交换,但是效率应该会差一些 temp = str2[low]; str2[low] = str[i]; str2[i] = temp; forsix(str2,low+1,hight); // temp = str[low]; // str[low] = str[i]; // str[i] = temp; } }
相关文章推荐
- 《Data Structures and Algorithm Analysis in Java》Second Edition中文版笔记
- Data Structures and Algorithm Analysis in C (数据结构与算法分析) 读书总结
- Implementation codes of Data Structures and Algorithm Analysis in C (1)
- Data Structures and Algorithm Analysis in c++ 第一章笔记和部分习题
- how to compile source code of "Data Structures & Algorithm Analysis in Java" writen by Mark Allen Weiss
- Data Structures and Algorithm Analysis in C 学习之List反转
- Data Structures and Algorithm Analysis in C 学习之List
- Data Structures and Algorithm Analysis in C, Second Edition(《数据结构与算法分析》C语言版 第二版)——Mark Allen Weiss
- 摘自《Data Structures and Algorithm Analysis in C》
- 《Data Structures and Algorithm Analysis in C》学习与刷题笔记
- dynamic programming in Date Structures and Algorithm Analysis in C
- backtracking in Date Structures and Algorithm Analysis in C
- 基本数据结构实现(Data Structures and Algorithm Analysis 2rd Edition)
- Data Structures and algorithm analysis—1.1 What's the book about? (数据结构1.1—关于本书)
- Data Structures and algorithm analysis—1.3. A Brief Introduction to Recursion(数据结构—1.3 递归的简介)(之一)
- 《Data Structure And Algorithm Analysis In C++》读书笔记一
- divide and conquer in Date Structures and Algorithm Analysis in C
- Data Structures and Algorithms in Java
- 《Data Structure And Algorithm Analysis In C++》读书笔记三
- Data Structures and Algorithms in Java