算法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 ...
随机推荐
- 201621123014《Java程序设计》第十二周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书馆管理系统或购物车. 2.1 简述如何 ...
- Redis 高可用及分片集群,说了你也不懂
Redis 简介 Memcached: 优点:高性能读写.单一数据类型.支持客户端式分布式集群.一致性hash 多核结构.多线程读写性能高. 缺点:无持久化.节点故障可能出现缓存穿透.分布式需要客户端 ...
- PHP学习之数组Array操作和键值对操作函数(一)
PHP 中的数组实际上是一个有序映射.映射是一种把 values关联到 keys 的类型.此类型在很多方面做了优化,因此可以把它当成真正的数组,或列表(向量),散列表(是映射的一种实现),字典,集合, ...
- STL容器共性机制和使用场景
一.STL容器共性机制 STL容器所提供的都是值(value)寓意,而非引用(reference)寓意,也就是说当我们给容器中插入元素的时候,容器内部实施了拷贝动作,将我们要插入的元素再另行拷贝一份放 ...
- 什么是 PCB 的压适孔
引用 AMOBBS 1 再举一个高成本控制的例子:有类PCB产品对孔径要求极度严格,这类孔叫压适孔,这类孔的作用类似于显卡内存条的插座,能刚刚好被元件插上,而且元件不会掉,PTH的压适孔公差要求为-0 ...
- Xml日志记录文件最优方案(附源代码)
Xml作为数据存储的一种方式,当数据非常大的时候,我们将碰到很多Xml处理的问题.通常,我们对Xml文件进行编辑的最直接的方式是将xml文件加载到XmlDocument,在内存中来对XmlDocume ...
- 使用cef
win10的同学注意了按右键以管理员模式启动cmake-gui.exe在Where is the source code:里填上你解压的CEF3路径,如:F:\cef3\cef_binary_3.23 ...
- Java基础--对象克隆
对象拷贝用于在内存中复制对象,无需构造器便可创建对象. 需要注意的是 1.clone方法提供的只是简单的值拷贝和地址拷贝,若类中包含HashMap等类型时,需要手工编写拷贝过程 2.如果父类没有提供正 ...
- Day3-Python基础3--局部变量和全局变量
一.局部变量 def test(name): print("before change:",name) name = "maqing" #局部变量name,只能 ...
- Vue开发模板简介
1. 传统发开模式的问题 用传统模式引用vue.js以及其他的js文件的开发方式,会产生一些问题. 基于页面的开发模式:传统的引用vue.js以及其他的js文件的开发方式,限定了我们的开发模式是 ...