算法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 ...
随机推荐
- middle school
One of the most difficult transitions faced by parents and youth is that of going from elementary to ...
- hdu4442 Physical Examination(贪心)
这种样式的最优解问题一看就是贪心.如果一下不好看,那么可以按照由特殊到一般的思维方式,先看n==2时怎么选顺序(这种由特殊到一般的思维方式是思考很多问题的入口): 有两个队时,若先选第一个,则ans= ...
- GET请求与POST请求区别
GET请求与POST请求区别 a:语义: GET:客户端想获取服务器资源 POST:客户端想传递数据给服务器 b:安全级: GET:不安全 POST:不安全 c:数据长度 GET:客户端发送数据最长1 ...
- C程序设计语言阅读笔记
预处理器 ->.i 编译器 >.s 汇编器 >.o 链接器 --可执行文件 ------------------ math.h头文件包含各种数学函数的声明,所有函数都返回一个 ...
- docker下安装 Oracle11gR2
这是第二次安装,在第一次安装过程部分内容参考自如下: http://blog.sina.com.cn/s/blog_d840ff330102v4j0.html docker下oracle11g安装 h ...
- sort--Linux下文本处理五大神器之三
转自:http://www.cnblogs.com/dong008259/archive/2011/12/08/2281214.html sort命令是帮我们依据不同的数据类型进行排序,其语法及常用参 ...
- CODE FESTIVAL 2017 qual A--C - Palindromic Matrix(模拟所有情况,注意细节)
个人心得:其实本来这题是有规律的不过当时已经将整个模拟过程都构思出来了,就打算试试,将每个字符和总和用优先队列 装起来,然后枚举每个点,同时进行位置标志,此时需要多少个点的时候拿出最大的和出来,若不满 ...
- 【LeetCode】673. Number of Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...
- [转]A记录和CNAME记录的区别
1.什么是域名解析? 域名解析就是国际域名或者国内域名以及中文域名等域名申请后做的到IP地址的转换过程.IP地址是网路上标识您站点的数字地址,为了简单好记,采用域名来代替ip地址标识站点地址.域名的解 ...
- 机器学习:PCA(实例:MNIST数据集)
一.数据 获取数据 import numpy as np from sklearn.datasets import fetch_mldata mnist = fetch_mldata("MN ...