add
This commit is contained in:
parent
2a0657fa79
commit
f3adc25ddb
@ -1,5 +1,9 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
@ -8,20 +12,38 @@ package com.fanxb.common;
|
||||
*/
|
||||
public class Q46 {
|
||||
|
||||
public int translateNum(int num) {
|
||||
String str = String.valueOf(num);
|
||||
//a=f(0),b=f(1)
|
||||
int a = 1, b = 1, sum = 1;
|
||||
for (int i = 1, length = str.length(); i < length; i++) {
|
||||
String temp = str.substring(i - 1, i + 1);
|
||||
sum = temp.compareTo("10") >= 0 && temp.compareTo("25") <= 0 ? a + b : b;
|
||||
a = b;
|
||||
b = sum;
|
||||
public List<List<Integer>> permute(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
dfs(nums, new Stack<>(), new boolean[nums.length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* dfs搜索
|
||||
*
|
||||
* @param nums 源数组
|
||||
* @param temp 记录已选择的数字
|
||||
* @param used 记录已选择数字的位置
|
||||
* @param res 保存结果
|
||||
* @author fanxb
|
||||
* date 2022/3/11 14:20
|
||||
*/
|
||||
public void dfs(int[] nums, Stack<Integer> temp, boolean[] used, List<List<Integer>> res) {
|
||||
if (temp.size() == nums.length) {
|
||||
res.add(new ArrayList<>(temp));
|
||||
}
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (!used[i]) {
|
||||
temp.push(nums[i]);
|
||||
used[i] = true;
|
||||
dfs(nums, temp, used, res);
|
||||
used[i] = false;
|
||||
temp.pop();
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q46().translateNum(12258));
|
||||
System.out.println(new Q46().permute(new int[]{1, 1, 2}));
|
||||
}
|
||||
}
|
||||
|
58
5.leetcode/src/com/fanxb/common/Q47.java
Normal file
58
5.leetcode/src/com/fanxb/common/Q47.java
Normal file
@ -0,0 +1,58 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q47 {
|
||||
|
||||
public List<List<Integer>> permuteUnique(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
Arrays.sort(nums);
|
||||
dfs(nums, new Stack<>(), new boolean[nums.length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* dfs搜索
|
||||
*
|
||||
* @param nums 源数组
|
||||
* @param temp 记录已选择的数字
|
||||
* @param used 记录已选择数字的位置
|
||||
* @param res 保存结果
|
||||
* @author fanxb
|
||||
* date 2022/3/11 14:20
|
||||
*/
|
||||
public void dfs(int[] nums, Stack<Integer> temp, boolean[] used, List<List<Integer>> res) {
|
||||
if (temp.size() == nums.length) {
|
||||
res.add(new ArrayList<>(temp));
|
||||
}
|
||||
for (int i = 0; i < nums.length; ) {
|
||||
if (!used[i]) {
|
||||
temp.push(nums[i]);
|
||||
used[i] = true;
|
||||
dfs(nums, temp, used, res);
|
||||
used[i] = false;
|
||||
temp.pop();
|
||||
int tempI = i;
|
||||
while (i < nums.length && nums[tempI] == nums[i]) {
|
||||
i++;
|
||||
}
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q47().permuteUnique(new int[]{1, 1, 2}));
|
||||
}
|
||||
}
|
29
5.leetcode/src/com/fanxb/common/Q62.java
Normal file
29
5.leetcode/src/com/fanxb/common/Q62.java
Normal file
@ -0,0 +1,29 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2022/3/11 15:28
|
||||
*/
|
||||
public class Q62 {
|
||||
public int uniquePaths(int m, int n) {
|
||||
int[][] f = new int[m][n];
|
||||
Arrays.fill(f[0], 1);
|
||||
for (int i = 0; i < m; i++) {
|
||||
f[i][0] = 1;
|
||||
}
|
||||
for (int i = 1; i < m; i++) {
|
||||
for (int j = 1; j < n; j++) {
|
||||
f[i][j] = f[i - 1][j] + f[i][j - 1];
|
||||
}
|
||||
}
|
||||
return f[m - 1][n - 1];
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q62().uniquePaths(3, 7));
|
||||
}
|
||||
}
|
40
5.leetcode/src/com/fanxb/common/Q63.java
Normal file
40
5.leetcode/src/com/fanxb/common/Q63.java
Normal file
@ -0,0 +1,40 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2022/3/11 15:28
|
||||
*/
|
||||
public class Q63 {
|
||||
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
|
||||
int m = obstacleGrid.length, n = obstacleGrid[0].length;
|
||||
int[][] f = new int[m][n];
|
||||
for (int i = 0; i < m; i++) {
|
||||
if (obstacleGrid[i][0] == 1) {
|
||||
break;
|
||||
}
|
||||
f[i][0] = 1;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (obstacleGrid[0][i] == 1) {
|
||||
break;
|
||||
}
|
||||
f[0][i] = 1;
|
||||
}
|
||||
for (int i = 1; i < m; i++) {
|
||||
for (int j = 1; j < n; j++) {
|
||||
if (obstacleGrid[i][j] == 0) {
|
||||
f[i][j] = f[i - 1][j] + f[i][j - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return f[m - 1][n - 1];
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q63().uniquePathsWithObstacles(new int[][]{{0, 1}, {0, 0}}));
|
||||
}
|
||||
}
|
26
5.leetcode/src/com/fanxb/common/Q70.java
Normal file
26
5.leetcode/src/com/fanxb/common/Q70.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
/**
|
||||
* 定义f(n)表示爬n级台阶的方法
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2022/3/11 15:55
|
||||
*/
|
||||
public class Q70 {
|
||||
public int climbStairs(int n) {
|
||||
if (n <= 2) {
|
||||
return n;
|
||||
}
|
||||
int last1 = 1, last2 = 2, res = 2;
|
||||
for (int i = 3; i <= n; i++) {
|
||||
res = last1 + last2;
|
||||
last1 = last2;
|
||||
last2 = res;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q70().climbStairs(5));
|
||||
}
|
||||
}
|
43
5.leetcode/src/com/fanxb/common/Q78.java
Normal file
43
5.leetcode/src/com/fanxb/common/Q78.java
Normal file
@ -0,0 +1,43 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q78 {
|
||||
|
||||
public List<List<Integer>> subsets(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
dfs(nums, 0, new Stack<>(), res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* dfs搜索
|
||||
*
|
||||
* @param nums 源数组
|
||||
* @param temp 记录已选择的数字
|
||||
* @param res 保存结果
|
||||
* @author fanxb
|
||||
* date 2022/3/11 14:20
|
||||
*/
|
||||
public void dfs(int[] nums, int index, Stack<Integer> temp, List<List<Integer>> res) {
|
||||
res.add(new ArrayList<>(temp));
|
||||
for (int i = index; i < nums.length; i++) {
|
||||
temp.push(nums[i]);
|
||||
dfs(nums, i + 1, temp, res);
|
||||
temp.pop();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q78().subsets(new int[]{1}));
|
||||
}
|
||||
}
|
48
5.leetcode/src/com/fanxb/common/Q90.java
Normal file
48
5.leetcode/src/com/fanxb/common/Q90.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q90 {
|
||||
|
||||
public List<List<Integer>> subsetsWithDup(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
Arrays.sort(nums);
|
||||
dfs(nums, 0, new Stack<>(), res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* dfs搜索
|
||||
*
|
||||
* @param nums 源数组
|
||||
* @param temp 记录已选择的数字
|
||||
* @param res 保存结果
|
||||
* @author fanxb
|
||||
* date 2022/3/11 14:20
|
||||
*/
|
||||
public void dfs(int[] nums, int index, Stack<Integer> temp, List<List<Integer>> res) {
|
||||
res.add(new ArrayList<>(temp));
|
||||
for (int i = index; i < nums.length; ) {
|
||||
temp.push(nums[i]);
|
||||
dfs(nums, i + 1, temp, res);
|
||||
temp.pop();
|
||||
int tempI = i;
|
||||
while (i < nums.length && nums[i] == nums[tempI]) {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q90().subsetsWithDup(new int[]{1, 2, 2}));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user