【Java实现 LeetCode 652 寻找重复的子树(两个map的DFS)】教程文章相关的互联网学习教程文章

LeetCode-1021 Remove Outermost Parentheses Solution(with Java)【代码】【图】

1. Description:Notes:2. Examples:3.Solutions: 1 /**2 * Created by sheepcore on 2018-12-243 */4 class Solution {5 public String removeOuterParentheses(String S) {6 StringBuilder s = new StringBuilder();7 int opened = 0;8 for (char c : S.toCharArray()) {9 if (c == ( && opened++ > 0) s.append(c); 10 if (c == ) && opened-- > 1) s.append(c); 11 ...

LeetCode-657 Robot Return to Origin Solution (with Java)【代码】【图】

1. Description:2. Examples:3.Solutions: 1 /**2 * Created by sheepcore on 2019-02-243 * best time complexity: O(n)4 * average time complexity: O(n)5 * worst time complexity: O(n)6 * space complexity: O(4);7 */8 class Solution {9 public boolean judgeCircle(String moves) { 10 int lefts = 0, rights = 0, ups = 0, downs = 0; 11 for (int i = 0; i < moves.length(); i++) { 12...

LeetCode-832 Flipping an Image Solution (with Java)【代码】【图】

1. Description: Notes:2. Examples:3.Solutions: 1 /**2 * Created by sheepcore on 2019-02-243 */4 class Solution {5 public int[][] flipAndInvertImage(int[][] A) {6 for (int i = 0; i < A.length; i++)7 for (int j = 0; j < A[0].length / 2; j++) { //flip each row and revert the image simultaneously.8 int temp = A[i][j] ^ 1;9 A[i][j] = A[i][A[0].leng...

LeetCode-557 Reverse Words in a String III Solution (with Java)【代码】【图】

1. Description:2.Solutions: 1 /**2 * Created by sheepcore on 2019-02-243 */4 class Solution {5 public String reverseWords(String s) {6 String[] splitStr = s.split(" "); 7 String temp = "";8 for (String str : splitStr) 9 temp += new StringBuffer(str).reverse().toString() + " "; 10 return temp.substring(0, temp.length() - 1); 11 } 12 }

LeetCode-961 N-Repeated Element in Size 2N Array Solution (with Java)【代码】【图】

1. Description: Notes:2. Examples:3.Solutions: 1 /**2 * Created by sheepcore on 2019-02-243 */4 class Solution {5 public int repeatedNTimes(int[] A) {6 int[] count = new int[10000];7 for(int i=0;i<A.length;i++)8 if(count[A[i]]++ == 1)9 return A[i]; 10 return -1; 11 } 12 }

LeetCode-503 Next Greater Element II Solution (with Java)【代码】【图】

1. Description:Notes: 2. Examples:3.Solutions: 1 /**2 * Created by sheepcore on 2019-05-103 */4 class Solution {5 public int[] nextGreaterElements(int[] A) {6 int n = A.length, res[] = new int[n];7 Arrays.fill(res, -1);8 Stack<Integer> stack = new Stack<>();9 //We need to loop twice to iterate circularly 10 for (int i = 0; i < n * 2; i++) { 11 ...

LeetCode-921 Minimum Add to Make Parentheses Valid Solution (with Java)【代码】【图】

1. Description: Notes: 2. Examples: 3.Solutions: 1 /**2 * Created by sheepcore on 2019-05-073 */4 class Solution {5 public int minAddToMakeValid(String s) {6 Stack<Character> stack = new Stack<Character>();7 int addnum = 0;8 for(int i = 0; i < s.length(); i++){9 char ch = s.charAt(i); 10 switch(ch){ 11 case (: stack.push(ch); b...

LeetCode-155 Min Stack Solution (with Java)【代码】【图】

1. Description: 2. Examples:3.Solutions: 1 /**2 * Created by sheepcore on 2019-05-073 * Your MinStack object will be instantiated and called as such:4 * MinStack obj = new MinStack();5 * obj.push(x);6 * obj.pop();7 * int param_3 = obj.top();8 * int param_4 = obj.getMin();9 */ 10 class MinStack { 11 /** initialize your data structure here. */ 12 private Stack<Integer> stack; 13 1...

Java实现 LeetCode 224 基本计算器【代码】

224. 基本计算器 实现一个基本的计算器来计算一个简单的字符串表达式的值。 字符串表达式可以包含左括号 ( ,右括号 ),加号 + ,减号 -,非负整数和空格 。 示例 1: 输入: “1 + 1” 输出: 2 示例 2: 输入: " 2-1 + 2 " 输出: 3 示例 3: 输入: “(1+(4+5+2)-3)+(6+8)” 输出: 23 说明: 你可以假设所给定的表达式都是有效的。 请不要使用内置的库函数 eval。 class Solution {public int calculate(String s) {Stack<Integer> st...

【LeetCode】 18. 4Sum 四数之和(Medium)(JAVA)【代码】

【LeetCode】 18. 4Sum 四数之和(Medium)(JAVA) 题目地址: https://leetcode.com/problems/4sum/ 题目描述: Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain duplicate quadruplets. Example: Given array nums ...

Java实现 LeetCode 218 天际线问题【代码】【图】

218. 天际线问题 城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓。现在,假设您获得了城市风光照片(图A)上显示的所有建筑物的位置和高度,请编写一个程序以输出由这些建筑物形成的天际线(图B)。Buildings Skyline Contour 每个建筑物的几何信息用三元组 [Li,Ri,Hi] 表示,其中 Li 和 Ri 分别是第 i 座建筑物左右边缘的 x 坐标,Hi 是其高度。可以保证 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX 和 Ri - ...

LeetCode 40. Combination Sum II 组合总和 II (C++/Java)【代码】

题目: Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note:All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations.Example 1: Input: candidates = [10,...

Java实现 LeetCode 212 单词搜索 II(二)【代码】

212. 单词搜索 II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。 示例: 输入: words = ["oath","pea","eat","rain"] and board = [['o','a','a','n'],['e','t','a','e'],['i','h','k','r'],['i','f','l','v']...

LeetCode 57. Insert Interval 插入区间 (C++/Java)【代码】

题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Input: intervals = [[1,3],[6,9]], newInterval = [2,5] Output: [[1,5],[6,9]]Example 2: Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] Output: [[1,2],[3,10],[12,16...

LeetCode 56. Merge Intervals 合并区间 (C++/Java)【代码】

题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].Example 2: Input: [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4] and [4,5] are considered overlapping. NOTE: input types have been changed on April 15, 2019. Pl...