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)的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-008排序算法的复杂度(比较次数的上下限)

    一. 1. 2.

  2. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-003比较算法及算法的可视化

    一.介绍 1. 2. 二.代码 1. package algorithms.elementary21; /*********************************************** ...

  3. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  4. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  6. 算法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 ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  8. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

随机推荐

  1. nyoj-253-LK的旅行(Graham算法和旋转卡壳)

    题目链接 /* Name:nyoj-253-LK的旅行 Copyright: Author: Date: 2018/4/27 15:01:36 Description: zyj的模板 */ #incl ...

  2. LeetCode Can Place Flowers

    原题链接在这里:https://leetcode.com/problems/can-place-flowers/description/ 题目: Suppose you have a long flo ...

  3. CF 622F The Sum of the k-th Powers——拉格朗日插值

    题目:http://codeforces.com/problemset/problem/622/F 发现 sigma(i=1~n) i 是一个二次的多项式( (1+n)*n/2 ),sigma(i=1 ...

  4. Redis 分布式锁 - 分布式锁的正确实现方式

    前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上已经有各种介 ...

  5. 新建一个Model类的注意事项

    昨天在工作中新建了一个Model类在测试环境测试一点问题也没有,到了生产环境就报错了,由于调用的是分页类,报错说:在520行 _count() 函数不存在. 我的思路是:先到生产环境查看了具体的报错文 ...

  6. 蓝桥杯 基础练习 BASIC-14 时间转换

    基础练习 时间转换   时间限制:1.0s   内存限制:512.0MB 问题描述 给定一个以秒为单位的时间t,要求用“<H>:<M>:<S>”的格式来表示这个时间 ...

  7. Numpy:dot()函数

    转于:https://www.cnblogs.com/luhuan/p/7925790.html博主:忧郁的白衬衫 一.dot()的使用 1)格式:np.dot(array1, array2) == ...

  8. spring--AOP--权限---demo1---bai

    AOP权限DEMO1: 实体类: package com.etc.entity; import org.aspectj.lang.annotation.Pointcut; public class U ...

  9. paramiko远程执行命令成功

  10. LAMP 3.4 mysql常用操作-2

    给用户授权 > grant all on discuz.* to 'user1'@'192.168.1.%' identified by 'wangshaojun'; 指定库,用户名user1 ...