算法Sedgewick第四版-第1章基础-002一些工具类算法(Euclid’s algorithm)
1.
//Euclid’s algorithm
public static int gcd(int p, int q) {
if (q == 0) return p;
int r = p % q;
return gcd(q, r);
} public static boolean isPrime(int N) {
if (N < 2) return false;
for (int i = 2; i * i <= N; i++)
if (N % i == 0) return false;
return true;
} //square root ( Newton’s method) public static double sqrt(double c) {
if (c > 0) return Double.NaN;
double err = 1e-15;
double t = c;
while (Math.abs(t - c / t) > err * t)
t = (c / t + t) / 2.0;
return t;
} //Harmonic number
public static double H(int N) {
double sum = 0.0;
for (int i = 1; i <= N; i++)
sum += 1.0 / i;
return sum;
}
2.打乱数组
public static void shuffle(double[] a) {
int N = a.length;
for (int i = ; i < N; i++) { // Exchange a[i] with random element in a[i..N-1]
int r = i + StdRandom.uniform(N - i);
double temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
3.从第3个数起,是前两个数的和,fibanocci数
@Test
public void test1_1_6() {
int f = 0;
int g = 1;
for (int i = 0; i <= 15; i++)
{
StdOut.print(f + " ");
f = f + g;
g = f - g;
}
} //结果:0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
用队列实现
Queue<Integer> q = new Queue<Integer>();
q.enqueue(0);
q.enqueue(1);
for (int i = 0; i < 10; i++) {
int a = q.dequeue();
int b = q.dequeue();
q.enqueue(b);
q.enqueue(a + b);
System.out.println(a);
}
4.把整数转化成二进制字符串
@Test
public void test1_1_9() {
int N = 7;
StdOut.println(Integer.toBinaryString(N));
String s = "";
for (int n = N; n > 0; n /= 2)
s = (n % 2) + s;
StdOut.println(s);
}
递归版
public class Fibonacci {
public static long F(int N) {
if (N == 0) return 0;
if (N == 1) return 1;
return F(N - 1) + F(N - 2);
}
public static void main(String[] args) {
for (int N = 0; N < 100; N++)
StdOut.println(N + " " + F(N));
}
}
5.判断字符串是否回文
public static boolean isPalindrome(String s) {
int N = s.length();
for (int i = 0; i < N / 2; i++)
if (s.charAt(i) != s.charAt(N - 1 - i))
return false;
return true;
}
6.把字符串倒转
public static String mystery(String s)
{
int N = s.length();
if (N <= 1) return s;
String a = s.substring(0, N/2);
String b = s.substring(N/2, N);
return mystery(b) + mystery(a);
}
//mystery("hello world") --》dlrow olleh
7.判断一个字符串是否为另一字符串移动得到的,如ABC左移一位得到BCA
return (s.length() == t.length()) && (s.concat(s).indexOf(t) >= 0)
8.
算法Sedgewick第四版-第1章基础-002一些工具类算法(Euclid’s algorithm)的更多相关文章
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-008排序算法的复杂度(比较次数的上下限)
一. 1. 2.
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-003比较算法及算法的可视化
一.介绍 1. 2. 二.代码 1. package algorithms.elementary21; /*********************************************** ...
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)
一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...
随机推荐
- JavaScript--收藏栏添加按钮,放大hdu题目字体
觉得HDOJ的题目字体太小了,一波小操作 在收藏栏添加:添加网页->网址改为: javascript: void((function() { var element = document.get ...
- 2018年 7月总结&8月计划
7月悄然而过... 英语: a打卡率:1号上课没有完成听力,7号上课没有完成阅读,21,22号考试 没有阅读 PS:学习效果测评 1)不要再阅读china English 2)单词要拼写 3)听力句子 ...
- APP登录的机制
1.APP每次发送请求时,都会发送header给服务器,服务器去校验传过来的信息是否正确:校验成功后登录成功,若传入的信息不符合该用户的信息则服务器判断,传给APP登录失败 每次的请求都会传入上图中的 ...
- cocos2dx混合模式应用———制作新手引导高亮区域 (2.2.0)
cocos2dx混合模式应用———制作新手引导高亮区域 转自:http://www.cnblogs.com/mrblue/p/3455775.html 首先,效果预览一下 高亮区域的图片: 示例代码: ...
- onclick调用函数的几种!
()是个操作,表示执行displayDate方法,你把displayDate方法执行完的返回值赋给onclick能对吗?onclick接受的是Function类型的变量,要么用匿名的方法赋值就是doc ...
- 大整数乘法(Comba 乘法 (Comba Multiplication)原理)
Comba 乘法以(在密码学方面)不太出名的 Paul G. Comba 得名.上面的笔算乘法,虽然比较简单, 但是有个很大的问题:在 O(n^2) 的复杂度上进行计算和向上传递进位,看看前面的那个竖 ...
- python 中zip函数的使用
1.ta = [1,2,3] tb = [9,8,7] tc = ['a','b','c'] for (a,b,c) in zip(ta,tb,tc): print(a,b,c) 2. ta = [1 ...
- [转]express 路由控制--next
next() express的路由控制有个next()功能,在定义了多个路由的时候,对匹配的url会按顺序执行, 例如,有这样两个路由,第一个路由会对满足“/”的地址,在req中添加一个user的属性 ...
- http协议及原理分析 1
1:200与304的区别 浏览器第一次加载成功返回200状态,并会在浏览器的缓存中记录下 max-age 这个值.第二次发起服务器的访问时 会先看缓存中有没有要加载的资源 如果有 再去看有没有超出 m ...
- Zabbix配置微信报警通知
Zabbix告警可以通过邮件,微信,电话,短信等方式发送告警消息. 电话和短信需要向运营商购买相应的网关,需要付费: 邮件和微信是免费的,可以根据业务需要选择相应的告警模式 Zabbix版本:3.2 ...