Skip to content

Commit

Permalink
LeetCode 每日1题
Browse files Browse the repository at this point in the history
  • Loading branch information
nibnait committed Mar 19, 2020
1 parent 8d8c77d commit 18349ca
Show file tree
Hide file tree
Showing 13 changed files with 652 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,47 @@ public class P01_斐波那契数列 extends TestCase {

@Test
public void testCase() {
// O(2^N)
System.out.println(f1(4));

// O(N)
System.out.println(f2(4));

//
}



private int f2(int n) {
if (n < 1) {
return 0;
}

if (n == 1 || n == 2) {
return 1;
}

int n1 = 1; // n - 1
int n2 = 1; // n - 2
int result = 0;
for (int i = 3; i <= n; i++) {
result = n1 + n2;
n2 = n1;
n1 = result;
}

return result;
}

private int f1(int n) {
if (n < 1) {
return 0;
}

if (n == 1 || n == 2) {
return 1;
}

return f1(n - 1) + f1(n - 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package algorithm_practice.Coding_Interview_Guide_2ndEdition.Chapter_04_递归和动态规划;

import junit.framework.TestCase;
import org.junit.Test;

/*
Created by nibnait on 2020-03-15
*/
public class P02_矩阵的最小路径和 extends TestCase {

@Test
public void testCase() {
int[][] matrix = new int[][]{
{1, 2, 8, 9},
{2, 4, 9, 12},
{4, 7, 10, 13},
{6, 8, 11, 15}
};
System.out.println(minPathSum1(matrix));
System.out.println(minPathSum2(matrix));
System.out.println(minPathSum3(matrix));

int[][] matrix1 = new int[][]{
{1, 3, 5, 9},
{8, 1, 3, 4},
{5, 0, 6, 1},
{8, 8, 4, 0}
};
System.out.println(minPathSum1(matrix1));
System.out.println(minPathSum2(matrix1));
System.out.println(minPathSum3(matrix1));


}

private int minPathSum3(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
return 0;
}

boolean rowMoreColumn = matrix.length > matrix[0].length;
int shorterArrayLength = rowMoreColumn ? matrix[0].length : matrix.length;
int longerArrayLength = rowMoreColumn ? matrix[0].length : matrix.length;

int[] dp = new int[shorterArrayLength];
dp[0] = matrix[0][0];

for (int i = 1; i < shorterArrayLength; i++) {
dp[i] = dp[i - 1] + (rowMoreColumn ? matrix[0][i] : matrix[i][0]);
}

for (int i = 1; i < longerArrayLength; i++) {
dp[0] = dp[0] + (rowMoreColumn ? matrix[i][0] : matrix[0][i]);
for (int j = 1; j < shorterArrayLength; j++) {
int currentValue = rowMoreColumn ? matrix[i][j] : matrix[j][i];
dp[j] = Math.min(dp[j - 1], dp[j]) + currentValue;
}
}

return dp[shorterArrayLength - 1];
}

private int minPathSum2(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
return 0;
}

int[] dp = new int[matrix[0].length];
dp[0] = matrix[0][0];
for (int i = 1; i < matrix[0].length; i++) {
dp[i] = dp[i - 1] + matrix[0][i];
}

for (int i = 1; i < matrix.length; i++) {
dp[0] = dp[0] + matrix[i][0];
for (int j = 1; j < matrix[0].length; j++) {
dp[j] = Math.min(dp[j - 1], dp[j]) + matrix[i][j];
}
}

return dp[dp.length - 1];
}

private int minPathSum1(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
return 0;
}

int[][] dp = new int[matrix.length][matrix[0].length];
dp[0][0] = matrix[0][0];

for (int i = 1; i < matrix.length; i++) {
dp[i][0] = dp[i - 1][0] + matrix[i][0];
}

for (int i = 1; i < matrix[0].length; i++) {
dp[0][i] = dp[0][i - 1] + matrix[0][i];
}

for (int i = 1; i < matrix.length; i++) {
for (int j = 1; j < matrix[0].length; j++) {
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + matrix[i][j];
}
}

return dp[matrix.length - 1][matrix[0].length - 1];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package algorithm_practice.Coding_Interview_Guide_2ndEdition.Chapter_04_递归和动态规划;

import junit.framework.TestCase;
import org.junit.Test;

/*
N 个位置,(N >= 2)
机器人在 M 位置上,(1 <= M <= N)
只走 K 步,
到达 P 位置。(1 <= P <= N)
C
reated by nibnait on 2020-03-15
*/
public class P03_机器人达到指定位置方法数 extends TestCase {

@Test
public void testCase() {
int N = 5, M = 2, K = 3, P = 3;
System.out.println(way1(N, M, K, P));
System.out.println(way2(N, M, K, P));

int N1 = 3, M1 = 1, K1 = 3, P1 = 3;
System.out.println(way1(N1, M1, K1, P1));
System.out.println(way2(N1, M1, K1, P1));

int N2 = 7, M2 = 4, K2 = 9, P2 = 5;
System.out.println(way1(N2, M2, K2, P2));
System.out.println(way2(N2, M2, K2, P2));

}

private int way2(int N, int M, int K, int P) {
if (N < 2 || M < 1 || M > N || K < 1 || P < 1 || P > N) {
return 0;
}

int[][] dp = new int[M][K];

return 0;

}


private int way1(int N, int M, int K, int P) {
if (N < 2 || M < 1 || M > N || K < 1 || P < 1 || P > N) {
return 0;
}

this.N = N;
this.P = P;
return walk(M, K);
}

/**
* 暴力递归
*
* @param n 一共N个位置
* @param cur 当前位置cur
* @param rest 还剩rest步
* @param p 到达p位置
* @return 方法数
*/
private int N = 0;
private int P = 0;

private int walk(int cur, int rest) {
if (rest == 0) {
return cur == P ? 1 : 0;
}

if (cur == 1) {
return walk(cur + 1, rest - 1);
}

if (cur == N) {
return walk(cur - 1, rest - 1);
}

return walk(cur + 1, rest - 1) + walk(cur - 1, rest - 1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package algorithm_practice.Coding_Interview_Guide_2ndEdition.Chapter_04_递归和动态规划;

import junit.framework.TestCase;
import org.junit.Test;

/*
Created by nibnait on 2020-03-15
*/
public class P04_换钱的最少货币数 extends TestCase {

@Test
public void testCase() {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package algorithm_practice.LeetCode.code100;

import com.alibaba.fastjson.JSON;
import common.datastruct.TreeNode;
import common.util.ConstructBinaryTree;
import common.util.PrintBinaryTree;
import junit.framework.TestCase;
import org.junit.Test;

import java.util.*;

/*
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例:
输入: [1,2,3,null,5,null,4]
输出: [1, 3, 4]
解释:
1 <---
/ \
2 3 <---
\ \
5 4 <---
Created by nibnait on 2020-03-19
*/
public class M199_二叉树的右视图 extends TestCase {

@Test
public void testCase() {
Integer[] root = {1, 2, 3, null, 5, null, 4};
System.out.println(rightSideView(ConstructBinaryTree.constructByBFSArray(root)));

Integer[] root1 = {1, 2, 3, null, 5, null, 4};
System.out.println(rightSideView(ConstructBinaryTree.constructByBFSArray(root1)));


}


/**
* 1. 层先法遍历二叉树
* 2. 然后取第1、2、4、8、16...个结点上的val
*/
public List<Integer> rightSideView(TreeNode root) {
List<Integer> bfsArray = new ArrayList<>();

bfsTravel(root, bfsArray);
// System.out.println(JSON.toJSONString(bfsArray));

List<Integer> result = new ArrayList<>();
int row = 0;
int index = Double.valueOf(Math.pow(2, row)).intValue();
while (index <= bfsArray.size()) {
int rightIndex = index - 1;
Integer node = bfsArray.get(rightIndex);
if (node != null) {
result.add(node);
} else {
while (bfsArray.get(--rightIndex) != null) {
result.add(node);
}
}

index += Double.valueOf(Math.pow(2, ++row)).intValue();
}

return bfsArray;
}

private void bfsTravel(TreeNode root, List<Integer> bfsArray) {

bfsArray.add(root.val);

LinkedList<TreeNode> queue = new LinkedList<>();
queue.addLast(root.left);
queue.addLast(root.right);

while (!queue.isEmpty()) {

TreeNode treeNode = queue.pollFirst();
bfsArray.add(treeNode.val);

if (isNotLeafNode(treeNode)) {
queue.addLast(treeNode.left);
queue.addLast(treeNode.right);
}
}

}

private boolean isNotLeafNode(TreeNode treeNode) {
return treeNode.left != null && treeNode.right != null;
}
}
Loading

0 comments on commit 18349ca

Please sign in to comment.