![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
java
文浪浪浪
这个作者很懒,什么都没留下…
展开
-
77. 组合
77. 组合典型dfs+剪枝class Solution { //结果表 List<List<Integer>> res = new ArrayList<List<Integer>>(); //子集 List<Integer> tmp = new ArrayList<Integer>(); public List<List<Integer>> combine(int n, i原创 2021-12-28 23:35:58 · 121 阅读 · 0 评论 -
57. 插入区间
57. 插入区间— 没搞懂属于什么问题类型class Solution { public int[][] insert(int[][] intervals, int[] newInterval) { int left = newInterval[0]; int right = newInterval[1]; boolean placed = false; List<int[]> ansList = new ArrayLi原创 2021-12-16 23:52:09 · 70 阅读 · 0 评论 -
跳跃游戏 II
45. 跳跃游戏 IIclass Solution { public int jump(int[] nums) { int len = nums.length; int start = 0; int end = 0; int maxIndex = 0; int count = 0; //当end边界超出数组长度时跳出 while(end < len - 1){原创 2021-12-15 22:37:25 · 3396 阅读 · 0 评论 -
字符串 全排列
剑指 Offer 38. 字符串的排列class Solution { //存储结果 List<String> rec = new ArrayList<String>(); //判断当前元素是否被访问过 boolean[] vis; public String[] permutation(String s) { int n = s.length(); vis = new boolean[n]; char[原创 2021-12-14 23:41:21 · 183 阅读 · 0 评论 -
279. 完全平方数 ---动态规划
279. 完全平方数class Solution { int res = Integer.MAX_VALUE; public int numSquares(int n) { if(n == 1) return 1; int[] dp = new int[n + 1]; //默认装状态为最差情况,当前数拆分为n个1 Arrays.fill(dp, n); for(int i = 1; i <原创 2021-12-14 00:04:43 · 204 阅读 · 0 评论 -
Java 动态规划 III
剑指 Offer 46. 把数字翻译成字符串class Solution { public int translateNum(int num) { String[] res = Integer.toString(num).split(""); int len = res.length; if(len == 0) return 0; int[] dp = new int[len + 1]; dp[0]原创 2021-12-10 00:08:21 · 150 阅读 · 0 评论 -
java 动态规划例题 II
1、连续子数组最大和2、礼物的最大值原创 2021-12-06 23:55:49 · 285 阅读 · 0 评论 -
java 动态规划例题
动态规划1、剑指 Offer 10- I. 斐波那契数列class Solution { public int fib(int n) { if(n == 0) return 0; int[] df = new int[n + 1]; df[0] = 0; df[1] = 1; for(int i = 2; i < n + 1; i++){ df[i] = (df[i - 1] + df[原创 2021-11-30 23:49:39 · 394 阅读 · 0 评论 -
26. 树的子结构
剑指 Offer 26. 树的子结构class Solution { public boolean isSubStructure(TreeNode A, TreeNode B) { return (A != null && B != null) && (dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B)); } public boolean dfs(原创 2021-11-30 23:48:08 · 116 阅读 · 0 评论 -
26. 树的子结构
剑指 Offer 26. 树的子结构class Solution { public boolean isSubStructure(TreeNode A, TreeNode B) { return (A != null && B != null) && (dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B)); } public boolean dfs(原创 2021-11-24 00:11:30 · 32 阅读 · 0 评论 -
从上到下打印二叉树 III
剑指 Offer 32 - III. 从上到下打印二叉树 IIIclass Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<List<Integer>>(); Queue<TreeNode> myQueue = new L原创 2021-11-23 00:54:37 · 173 阅读 · 1 评论 -
深度优先与广度优先 java
深度优先与广度优先 //广度优先遍历 public void BroadFirstSearch(TreeNode nodeHead) { if(nodeHead==null) { return; } Queue<TreeNode> myQueue=new LinkedList<>(); myQueue.add(nodeHead); while(!myQueue.is原创 2021-11-22 23:46:13 · 416 阅读 · 0 评论 -
HashMap存储频数
使用哈希表存储频数,并返回第一个仅出现一次的字母class Solution { public char firstUniqChar(String s) { Map<Character, Integer> frequency = new HashMap<Character, Integer>(); for (int i = 0; i < s.length(); ++i) { char ch = s.charAt(原创 2021-11-19 00:34:29 · 118 阅读 · 0 评论 -
二维数组中的查找 hashset方法
二维数组中的查找 hashset方法class Solution { public boolean findNumberIn2DArray(int[][] matrix, int target) { int row = matrix.length; if(matrix.length == 0){ return false; } int colum = matrix[0].length; Hash原创 2021-11-18 23:40:34 · 491 阅读 · 0 评论 -
字符串操作
字符串操作例 s = “abcdefg”截取String str1 = s.substring(0,2)//outputabString str2 = s.substring(2)//outputcdefg拼接s2+s1//outputcdefgab替换s.repalce("c", "%20");//outputab%20defg原创 2021-11-16 23:27:37 · 45 阅读 · 0 评论 -
复杂链表的复制
35 复杂链表的复制class Solution { Map<Node, Node> mapOfNode = new HashMap<Node, Node>(); public Node copyRandomList(Node head) { if(head == null) return null; while(!mapOfNode.containsKey(head)){ Node headNew = new原创 2021-11-16 00:17:00 · 358 阅读 · 0 评论 -
MIN函数栈
MIN函数栈class MinStack { Stack<Integer> stk1; Stack<Integer> stk2; /** initialize your data structure here. */ public MinStack() { stk1 = new Stack<Integer>(); stk2 = new Stack<Integer>(); }原创 2021-11-12 00:17:12 · 185 阅读 · 0 评论 -
双栈实现队列
双栈实现队列class CQueue { Stack<Integer> stk1 = new Stack<Integer>(); Stack<Integer> stk2 = new Stack<Integer>(); public CQueue() { } public void appendTail(int value) { stk1.push(value); }原创 2021-11-11 23:51:57 · 51 阅读 · 0 评论 -
260260260
260垃圾解法,也不是不能用class Solution { public int[] singleNumber(int[] nums) { int len = nums.length; LinkedList<Integer> numList = new LinkedList<Integer>(); int[] res = new int[2]; int counter = 0; for(in原创 2021-11-05 00:06:08 · 54 阅读 · 0 评论 -
919191
91class Solution { public int numDecodings(String s) { int n = s.length(); int[] f = new int[n + 1]; f[0] = 1; for(int i = 1; i <= n; i++){ if(s.charAt(i - 1) != '0'){ f[i] += f[i - 1];原创 2021-11-04 00:26:34 · 293 阅读 · 0 评论 -
402402402
402class Solution { public String removeKdigits(String num, int k) { Deque<Character> deque = new LinkedList<Character>(); int len = num.length(); for(int i = 0; i < len; i++){ char digit = num.charAt原创 2021-11-03 00:37:10 · 46 阅读 · 0 评论 -
210210210
210210与207类似,拓扑排序//DFSclass Solution { // 存储有向图 List<List<Integer>> edges; // 标记每个节点的状态:0=未搜索,1=搜索中,2=已完成 int[] visited; // 用数组来模拟栈,下标 n-1 为栈底,0 为栈顶 int[] result; // 判断有向图中是否有环 boolean valid = true; // 栈下标原创 2021-11-01 23:37:59 · 71 阅读 · 0 评论 -
208208208
208208208 字典树class Trie { private Trie[] children; private boolean isEnd; public Trie() { children = new Trie[26]; isEnd = false; } public void insert(String word) { Trie node = this; for(int i = 0原创 2021-10-27 00:19:20 · 53 阅读 · 0 评论 -
747474
747474class Solution { boolean res = false; public boolean searchMatrix(int[][] matrix, int target) { int y = matrix.length; int x = matrix[0].length; int yTmp = 0; if(target < matrix[0][0] || target > matrix原创 2021-10-25 23:56:47 · 418 阅读 · 0 评论 -
207207207
207/*拓扑排序,没整明白*/class Solution { List<List<Integer>> edges; int[] visited; boolean valid = true; public boolean canFinish(int numCourses, int[][] prerequisites) { edges = new ArrayList<List<Integer>>();原创 2021-10-22 00:30:55 · 45 阅读 · 0 评论 -
134134134
134class Solution { int res = 0; int begin; boolean flag = false; public int canCompleteCircuit(int[] gas, int[] cost) { for(int startIndex = 0; startIndex < gas.length; startIndex++){ begin = startIndex;原创 2021-10-21 00:04:40 · 56 阅读 · 0 评论 -
123912391239
1239class Solution { StringBuilder stringbuilder = new StringBuilder(); int res = 0; public int maxLength(List<String> arr) { dfs(arr, 0); return res; } public void dfs(List<String> arr, int start){原创 2021-10-19 00:34:19 · 42 阅读 · 0 评论 -
560560560
560class Solution { int res = 0; public int subarraySum(int[] nums, int k) { int index = 0; int len = nums.length; for(; index < len; index++){ int tmp = 0; for(int flow = index;flow < len; flo原创 2021-10-14 00:14:26 · 52 阅读 · 0 评论 -
189189189
189class Solution { public void rotate(int[] nums, int k) { int len = nums.length; if(len < k) k = k%len; int[] res = new int[len]; System.arraycopy(nums, len - k, res, 0, k); System.arraycopy(nums, 0, res, k原创 2021-10-12 23:37:51 · 74 阅读 · 0 评论 -
165165165
165class Solution { int res = 0; int flag = 1; String[] versionlong; String[] versionshort; public int compareVersion(String version1, String version2) { String[] version1_sep = version1.split("\\."); String[] version2_原创 2021-10-12 00:11:07 · 43 阅读 · 0 评论 -
443443443
443class Solution { public int compress(char[] chars) { int len = chars.length; int left = 0, write = 0; for(int i = 0; i < len; i++){ if((i == len - 1) || (chars[i] != chars[i + 1])){ chars[write原创 2021-09-29 00:28:12 · 42 阅读 · 0 评论 -
153153153
153class Solution { public int findMin(int[] nums) { int len = nums.length; if(len == 1) return nums[0]; int i = 0; while(i < len - 1){ if(nums[i + 1] < nums[i]){ return nums[i + 1];原创 2021-09-26 23:06:16 · 68 阅读 · 0 评论 -
277277277
277public class Solution extends Relation { public int findCelebrity(int n) { int idx = -1; int[] whoKnowHe = new int[n]; int[] HeKnowWho = new int[n]; int[][] relationMap = new int[n][n]; for(int i = 0; i <原创 2021-09-23 23:32:03 · 47 阅读 · 0 评论 -
445445445
445class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { Deque<Integer> stack1 = new LinkedList<Integer>(); Deque<Integer> stack2 = new LinkedList<Integer>(); while (l1 != null) {原创 2021-09-23 00:27:27 · 70 阅读 · 0 评论 -
838383
83 删除排序链表中重复元素class Solution { public ListNode deleteDuplicates(ListNode head) { if(head == null) return head; ListNode dumpNode= head; while(dumpNode.next != null){ if(dumpNode.val == dumpNode.next.val){原创 2021-09-15 23:35:38 · 64 阅读 · 0 评论 -
797979
79class Solution { boolean res = false; char[] resString = new char[15]; boolean[][] visit; public boolean exist(char[][] board, String word) { char[] w = word.toCharArray(); int len = w.length; int x = board.length原创 2021-09-14 23:32:00 · 112 阅读 · 0 评论 -
116116116
116class Solution { Queue<Node> res = new LinkedList<Node>(); public Node connect(Node root) { if(root == null){ return root; } //层序遍历操作所有节点的next指向 res.offer(root); root.next = null;原创 2021-09-14 00:04:49 · 42 阅读 · 0 评论 -
253253253
253class Solution { public int minMeetingRooms(int[][] intervals) { //对二维矩阵按照每组第一个元素进行排序 Arrays.sort(intervals,(i,j)->i[0]-j[0]); int numOfMeeting = intervals.length; //路子非常野,由于结果矩阵只用第一列,所以此处仅记录行数就行 int[][] numOfRo原创 2021-09-10 00:24:25 · 43 阅读 · 0 评论 -
144814481448
1448class Solution { int numsOfGoodNodes = 0; int lastNodeVal; public int goodNodes(TreeNode root) { lastNodeVal = root.val; dfs(root, lastNodeVal); return numsOfGoodNodes; } public void dfs(TreeNode root, int l原创 2021-09-08 22:54:19 · 49 阅读 · 0 评论 -
2021-09-07
138class Solution { HashMap<Node, Node> res = new HashMap<Node, Node>(); public Node copyRandomList(Node head) { if(head == null) return null; //如果不包含,则添加 if(!res.containsKey(head)){ Node tmp = new N原创 2021-09-07 00:48:14 · 31 阅读 · 0 评论