diff --git a/5.leetcode/src/com/fanxb/common/Q46.java b/5.leetcode/src/com/fanxb/common/Q46.java index 16fc1a7..9ed38be 100644 --- a/5.leetcode/src/com/fanxb/common/Q46.java +++ b/5.leetcode/src/com/fanxb/common/Q46.java @@ -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> permute(int[] nums) { + List> 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 temp, boolean[] used, List> 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})); } } diff --git a/5.leetcode/src/com/fanxb/common/Q47.java b/5.leetcode/src/com/fanxb/common/Q47.java new file mode 100644 index 0000000..5e262d5 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q47.java @@ -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> permuteUnique(int[] nums) { + List> 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 temp, boolean[] used, List> 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})); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q62.java b/5.leetcode/src/com/fanxb/common/Q62.java new file mode 100644 index 0000000..d7c51ff --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q62.java @@ -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)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q63.java b/5.leetcode/src/com/fanxb/common/Q63.java new file mode 100644 index 0000000..faad793 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q63.java @@ -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}})); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q70.java b/5.leetcode/src/com/fanxb/common/Q70.java new file mode 100644 index 0000000..5109278 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q70.java @@ -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)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q78.java b/5.leetcode/src/com/fanxb/common/Q78.java new file mode 100644 index 0000000..181fd7f --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q78.java @@ -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> subsets(int[] nums) { + List> 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 temp, List> 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})); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q90.java b/5.leetcode/src/com/fanxb/common/Q90.java new file mode 100644 index 0000000..bf2d2ca --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q90.java @@ -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> subsetsWithDup(int[] nums) { + List> 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 temp, List> 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})); + } +}