Skip to content
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*.so
*.dylib
*.DS_Store

algorithms4th/out/
# Test binary, built with `go test -c`
*.test

Expand Down
12 changes: 12 additions & 0 deletions algorithms4th/REAEME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 算法第 4 版本

是时候来学习这门课程了

项目依赖的 jar 包在 [deplib](deplib) 文件夹下


辅助配套学习资料

1. [可视化算法资料](https://visualgo.net/en/bitmask)
2. [配套学习网站](https://introcs.cs.princeton.edu/java/stdlib/)

21 changes: 21 additions & 0 deletions algorithms4th/algorithms4th.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="algs4" level="project" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/20.1.0/annotations-20.1.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
Binary file added algorithms4th/deplib/algs4.jar
Binary file not shown.
Binary file added algorithms4th/deplib/stdlib.jar
Binary file not shown.
40 changes: 40 additions & 0 deletions algorithms4th/src/com/xx/GCD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 求两个数的最大公约数
*/
package com.xx;

import edu.princeton.cs.algs4.StdOut;

public class GCD {
/**
* @param a 输入 数 a
* @param b 输入 b
* @return
* @desc 计算 两个数的最大公约数
*/
public static int gcd(int a, int b) {
int temp = 0;
if (a > b) {
temp = a;
a = b;
b = temp;
}
// 如果其中一个为 0,直接返回 0
if (a == 0 || b == 0) {
return 0;
}
while (b > 0) {
temp = a % b; // a % b 等价于 a - (a/b)*b
a = b;
b = temp;
}
return a;
}

public static void main(String[] args) {
StdOut.println(gcd(24, 3));
StdOut.println(gcd(24, 6));
StdOut.println(gcd(24, 1));
StdOut.println(gcd(24, 0));
}
}
31 changes: 31 additions & 0 deletions algorithms4th/src/com/xx/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.xx;

import edu.princeton.cs.algs4.StdOut;

public class Main {
public static int gcd(int m, int n) {
int rem;
while (n > 0) {
rem = m % n;
m = n;
n = rem;
}
return m;
}

public static void main(String[] args) {
System.out.println("study algorithms 4th code");
StdOut.println(StdOut.class.toString());
System.out.println(System.class.toString()); // 输出类名
System.out.println(gcd(24, 4));
System.out.println(gcd(24, 1));
System.out.println(gcd(24, 2));
System.out.println(gcd(24, 3));
System.out.println(Main.isMain);

}

final static int a = 1;
static final int b = 2;
final static public boolean isMain = true && ((a ^ b ) == 0) ;
}
48 changes: 48 additions & 0 deletions algorithms4th/src/com/xx/character1/Evaluate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.xx.character1;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

import java.util.Stack;

public class Evaluate {
public static void main(String[] args) {
Stack<String> ops = new Stack<String>(); // 操作符号
Stack<Double> vals = new Stack<Double>(); // 操作数

while (!StdIn.isEmpty()) {
String s = StdIn.readString();
if (s.equals("(")) {
;
} else if (s.equals("+")) {
ops.push(s);
} else if (s.equals("-")) {
ops.push(s);
} else if (s.equals("*")) {
ops.push(s);
} else if (s.equals("/")) {
ops.push(s);
} else if (s.equals("sqrt")) {
ops.push(s);
} else if (s.equals(")")) { // 获取的是右括号
String op = ops.pop(); // 获取操作符号
double v = vals.pop(); // 获取操作数
if (op.equals("+")) { // 如果操作符号是 +
v = vals.pop() + v; // 再弹一个栈并且相加
} else if (op.equals("-")) { // 如果当前的操作符号是 -
v = vals.pop() - v; // 再弹出一个操作数并且相减
} else if (op.equals("*")) {
v = vals.pop() * v;
} else if (op.equals("/")) {
v = vals.pop() / v;
} else {
v = Math.sqrt(v);
}
vals.push(v);
} else {
vals.push(Double.parseDouble(s));
}
}
StdOut.println(vals.pop());
}
}
54 changes: 54 additions & 0 deletions algorithms4th/src/com/xx/character1/Parenthsses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.xx.character1;

import com.xx.mycontainer.Stack;

public class Parenthsses {
public static void main(String[] args) {
System.out.println(isOk("{ ]"));
System.out.println(isOk("{ }"));
System.out.println(isOk("{ {"));
System.out.println(isOk("{ { } )"));
System.out.println(isOk(""));
}

public static boolean isOk(String data) {
String[] sp = data.split(" ");
Stack<String> ss = new Stack<>();
String tmp;
for (String s : sp) {
if (s.equals(")")) {
tmp = ss.pop();
if (tmp == null) {
return false;
}
if (!tmp.equals("(")) {
return false;
}
} else if (s.equals("]")) {
tmp = ss.pop();
if (tmp == null) {
return false;
}
if (!tmp.equals("[")) {
return false;
}
} else if (s.equals("}")) {
tmp = ss.pop();
if (tmp == null) {
return false;
}
if (!tmp.equals("{")) {
return false;
}
} else {
ss.push(s);
}
}
return ss.isEmpty();
}

private class Item {
private String data /* "{[)}"*/;
private boolean isOk;
}
}
89 changes: 89 additions & 0 deletions algorithms4th/src/com/xx/character1/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.xx.character1;

import org.jetbrains.annotations.NotNull;

import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;

public class Stack<Item> implements Iterable<Item> {
public static void main(String[] args) {
Stack<Integer> is = new Stack<>();
is.push(23);
is.push(3);
is.push(1);
for (Integer i : is) {
System.out.println(i.toString());
}
}

private class Node {
Item item;
Node next;
}

private Node first;
private int N;

public boolean isEmpty() {
return first == null;
}

public int size() {
return this.N;
}

public void push(Item item) {
Node oldFirst = first;
first = new Node();
first.item = item;
first.next = oldFirst;
this.N++;
}

public Item pop() {
Item item = first.item;
first = first.next;
this.N--;
return item;

}


@NotNull
@Override
public Iterator<Item> iterator() {
return new ListIterator();
}

@Override
public void forEach(Consumer<? super Item> action) {

}

@Override
public Spliterator<Item> spliterator() {
return null;
}

private class ListIterator implements Iterator<Item> {
private Node current = first;

@Override
public boolean hasNext() {
return current != null;
}

@Override
public Item next() {
Item item = current.item;
current = current.next;
return item;
}

@Override
public void remove() {

}
}
}
48 changes: 48 additions & 0 deletions algorithms4th/src/com/xx/character2/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
java 中的排序模板
*/

package com.xx.character2;

public class Example {

// 排序函数
public static void sort(Comparable[] a) {
// todo
}

// 判断是否小于
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}

// 交换待比较中的元素
private static void exch(Comparable[] a, int i, int j) {
Comparable tem = a[i];
a[i] = a[j];
a[j] = tem;
}

// 打印数组
private static void show(Comparable[] a) {
for (Comparable ca : a) {
System.out.print(ca);
}
System.out.println();
}

/**
* 判断是否已经有序
*
* @param a 入参
* @return true 有序, false 无序
*/
public static boolean isSorted(Comparable[] a) {
for (int i = 1; i < a.length; i++) {
if (less(a[i], a[i - 1])) {
return false;
}
}
return true;
}
}
Loading