算法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 ...
随机推荐
- 配合Jenkins自动化构建,bat脚本(二)
批量通过模板,拷贝文件,然后替换模板文件中的标记位为预制的内容. 1 Set servicePath=Ehong.MedicareReview.Web\地区配置\ Set webPath=Ehong. ...
- LeetCode OJ:Binary Tree Level Order Traversal II(二叉树的层序遍历)
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 浅议Windows 2000/XP Pagefile组织管理
任何时候系统内存资源相对磁盘空间来说都是相形见拙的.因为虚拟内存机制,使我们可以有相对丰富的地址资源(通常32bit的虚拟地址,可以有4G的寻址 空间),而这些资源对物理内存来说一般情况是总是绰绰有余 ...
- 第一章计算机网络和因特网-day02
1.互联网中的时延:处理时延.排队时延.传输时延.传播时延. 处理时延:检查分组首部和决定该分组导向何处的时间. 排队时延:分组在链路上等待传输的时延. 传输时延:分组经过路由器与交换机的过程的时延. ...
- 第11篇 PSR-0 规范
Mandatory A fully-qualified namespace and class must have the following structure \<Vendor Name&g ...
- Azure上采用Powershell从已有的VHD创建VM
刚刚的一篇Blog采用Json Template的方式从已有的VHD创建了一台新的VM.由于Json Template封装的比较好,可以改的内容不多. 下面将介绍通过用Powershell来从已有的V ...
- HDU2579(bfs迷宫)
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- c#在sql中存取图片image示例
这篇文章主要介绍了c#在sql中存取图片image示例,需要的朋友可以参考下 (1)控制台应用程序下演示插入图片 复制代码 代码如下: public void InsertIMG() { //将需要存 ...
- web编程的初步认识
一直以后, 只知道打开浏览器, 输入网址便可以上网浏览网页, 但是当认真琢磨起这web编程的时候, 对于很多细节却是感觉很迷惑, 在慢慢的学习中, 才逐渐有了些了解. web有client/serve ...
- python django ORM 性能优化 select_related & prefetch_related
q = models.UserInfo.objects.all() select * from userinfo select * from userinfo inner join usertype ...