Merge pull request #1 from wangzheng0822/master

更新数据
This commit is contained in:
hkui 2019-09-23 17:49:15 +08:00 committed by GitHub
commit a8ab74397d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 120 additions and 11 deletions

View File

@ -37,7 +37,7 @@ public class CircularQueue {
public void printAll() {
if (0 == n) return;
for (int i = head; i % n != tail; ++i) {
for (int i = head; i % n != tail; i = (i + 1) % n) {
System.out.print(items[i] + " ");
}
System.out.println();

View File

@ -1,6 +1,5 @@
package skiplist;
import java.util.Random;
/**
* 跳表的一种实现方法
@ -10,14 +9,13 @@ import java.util.Random;
*/
public class SkipList {
private static final float SKIPLIST_P = 0.5f;
private static final int MAX_LEVEL = 16;
private int levelCount = 1;
private Node head = new Node(); // 带头链表
private Random r = new Random();
public Node find(int value) {
Node p = head;
for (int i = levelCount - 1; i >= 0; --i) {
@ -86,15 +84,17 @@ public class SkipList {
}
// 随机 level 如果是奇数层数 +1防止伪随机
// 理论来讲一级索引中元素个数应该占原始数据的 50%二级索引中元素个数占 25%三级索引12.5% 一直到最顶层
// 因为这里每一层的晋升概率是 50%对于每一个新插入的节点都需要调用 randomLevel 生成一个合理的层数
// randomLevel 方法会随机生成 1~MAX_LEVEL 之间的数
// 50%的概率返回 1
// 25%的概率返回 2
// 12.5%的概率返回 3 ...
private int randomLevel() {
int level = 1;
for (int i = 1; i < MAX_LEVEL; ++i) {
if (r.nextInt() % 2 == 1) {
level++;
}
}
while (Math.random() < SKIPLIST_P && level < MAX_LEVEL)
level += 1;
return level;
}

View File

@ -0,0 +1,20 @@
fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
let mut dp = vec![amount+1; (amount+1) as usize];
dp[0] = 0;
for i in 1..=amount as usize {
for &coin in coins.iter() {
if i as i32 >= coin {
dp[i] = dp[i].min(dp[i-coin as usize] + 1);
}
}
}
let last = *dp.last().unwrap();
if last > amount { -1 } else { last }
}
fn main() {
let coins = vec![1, 3, 5];
let m = coin_change(coins, 9);
println!("{}", m); // 3
}

View File

@ -0,0 +1,38 @@
fn min_dis_path(matrix: Vec<Vec<i32>>) -> i32 {
let m_len = matrix.len();
if m_len == 0 { return 0; }
let mut states = vec![vec![0; m_len]; m_len];
let mut sum = 0;
// 初始化第一行数据
for j in 0..m_len {
sum += matrix[0][j];
states[0][j] = sum;
}
sum = 0;
// 初始化第一列数据
for i in 0..m_len {
sum += matrix[i][0];
states[i][0] = sum;
}
for i in 1..m_len {
for j in 1..m_len {
states[i][j] = matrix[i][j] + states[i-1][j].min(states[i][j-1])
}
}
states[m_len-1][m_len-1]
}
fn main() {
let matrix = vec![
vec![1, 3, 5, 9],
vec![2, 1, 3, 4],
vec![5, 2, 6, 7],
vec![6, 8, 4, 3],
];
let m = min_dis_path(matrix);
println!("{}", m);
}

View File

@ -0,0 +1,29 @@
// leetcode 72 [edit_distance](https://leetcode.com/problems/edit-distance/)
fn edit_distance(word1: &str, word2: &str) -> i32 {
let word1_chars: Vec<char> = word1.chars().collect();
let word2_chars: Vec<char> = word2.chars().collect();
let m = word1.len();
let n = word2.len();
let mut dp = vec![vec![0; m+1]; n+1];
// 初始化第一行
for i in 0..=m { dp[i][0] = i; }
// 初始化第一列
for j in 0..=n { dp[0][j] = j; }
for i in 1..=m {
for j in 1..=n {
let mut step = 0;
if word1_chars[i-1] != word2_chars[j-1] { step = 1; }
dp[i][j] = (dp[i][j-1] + 1).min(dp[i-1][j] + 1).min(dp[i-1][j-1] + step);
}
}
dp[m][n] as i32
}
fn main() {
let word1 = "mitcmu";
let word2 = "mtacnu";
let m = edit_distance(word1, word2);
println!("{:?}", m);
}

View File

@ -0,0 +1,22 @@
// leetcode 300 [longest_increasing_subsequence](https://leetcode.com/problems/longest-increasing-subsequence)
fn longest_increasing_subsequence(nums: Vec<i32>) -> i32 {
if nums.len() <= 1 { return nums.len() as i32; }
let mut dp = vec![1; nums.len()];
let mut max_list = 1;
for i in 0..nums.len() {
for j in 0..i {
if nums[i] > nums[j] {
dp[i] = dp[i].max(dp[j]+1);
}
}
max_list = max_list.max(dp[i]);
}
max_list
}
fn main() {
let nums = vec![2, 9, 3, 6, 5, 1, 7];
let m = longest_increasing_subsequence(nums);
println!("{:?}", m);
}