-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liguanghua
committed
Oct 20, 2021
1 parent
94713b0
commit 0f856d5
Showing
3 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import java.util.Stack; | ||
|
||
/** | ||
* @author dell | ||
* @version 1.0 | ||
* @description: 有效的括号 | ||
* | ||
* 给定一个只包括 '(',')','{','}','[',']'的字符串 s ,判断字符串是否有效。 | ||
* 有效字符串需满足: | ||
* 左括号必须用相同类型的右括号闭合。 | ||
* 左括号必须以正确的顺序闭合。 | ||
* 示例 1: | ||
* 输入:s = "()" | ||
* 输出:true | ||
* 示例2: | ||
* 输入:s = "()[]{}" | ||
* 输出:true | ||
* 示例3: | ||
* 输入:s = "(]" | ||
* 输出:false | ||
* 示例4: | ||
* 输入:s = "([)]" | ||
* 输出:false | ||
* 示例5: | ||
* 输入:s = "{[]}" | ||
* 输出:true | ||
* | ||
* 来源:力扣(LeetCode) | ||
* 链接:https://leetcode-cn.com/problems/valid-parentheses | ||
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
* @date 2021/10/20 10:09 | ||
*/ | ||
public class LeetCode20 { | ||
|
||
/** | ||
* @title | ||
* @description 思路: | ||
* 1循环便利字符串,然后构建一个栈,遇见{ ,[ ,( 这边就直接进栈 。(反向) | ||
* 2然后判断栈是否为空,空表示只有 },],)右边元素显然不成立;然后进行出栈,出栈的时候判断是否和字符串相等。因为栈是满足先进后出的原则 | ||
* | ||
* 时间复杂度为O(n) | ||
* @author admin | ||
* @updateTime 2021/10/20 10:19 | ||
* @throws | ||
*/ | ||
public boolean isValid(String s) { | ||
Stack<Character> stack = new Stack<>(); | ||
for (char c : s.toCharArray()){ | ||
if (c == '{') stack.push('}'); | ||
else if (c == '[') stack.push(']'); | ||
else if (c == '(') stack.push(')'); | ||
else if (stack.isEmpty() || c != stack.pop()) return false; // 这里意味着前面有一部分成对,后面没有了。或者stack本身就是空的 | ||
} | ||
return stack.isEmpty(); // 空表示有成对的 | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(new LeetCode20().isValid("(()())")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* @author dell | ||
* @version 1.0 | ||
* @description: 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 | ||
* | ||
* 输入:l1 = [1,2,4], l2 = [1,3,4] | ||
* 输出:[1,1,2,3,4,4] | ||
* 示例 2: | ||
* | ||
* 输入:l1 = [], l2 = [] | ||
* 输出:[] | ||
* 示例 3: | ||
* | ||
* 输入:l1 = [], l2 = [0] | ||
* 输出:[0] | ||
* | ||
* 来源:力扣(LeetCode) | ||
* 链接:https://leetcode-cn.com/problems/merge-two-sorted-lists | ||
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
* @date 2021/10/20 10:35 | ||
*/ | ||
public class LeetCode21 { | ||
|
||
class ListNode { | ||
int var; | ||
ListNode next; | ||
|
||
ListNode() { | ||
} | ||
|
||
ListNode(int var) { | ||
this.var = var; | ||
} | ||
|
||
ListNode(int var, ListNode next) { | ||
this.var = var; | ||
this.next = next; | ||
} | ||
|
||
public String toString(){ | ||
if (this.next!=null){ | ||
return var+","+ next; | ||
} | ||
return var+""; | ||
} | ||
} | ||
|
||
/** | ||
* 使用递归 | ||
* @param l1 | ||
* @param l2 | ||
* @return | ||
*/ | ||
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { | ||
if (l1 == null) return l2; | ||
else if (l2 == null) return l1; | ||
else if (l1.var < l2.var){ | ||
l1.next = mergeTwoLists(l1.next,l2); | ||
return l1; | ||
}else{ | ||
l2.next = mergeTwoLists(l1,l2.next); | ||
return l2; | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
LeetCode21 L = new LeetCode21(); | ||
ListNode l1 = null; | ||
ListNode l2 = L.new ListNode(1); | ||
System.out.println(new LeetCode21().mergeTwoLists(l1,l2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author dell | ||
* @version 1.0 | ||
* @description: 括号生成 | ||
* | ||
* 数字 n代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。 | ||
* | ||
* 有效括号组合需满足:左括号必须以正确的顺序闭合。 | ||
* | ||
* 示例 1: | ||
* 输入:n = 3 | ||
* 输出:["((()))","(()())","(())()","()(())","()()()"] | ||
* 示例 2: | ||
* 输入:n = 1 | ||
* 输出:["()"] | ||
* | ||
* 来源:力扣(LeetCode) | ||
* 链接:https://leetcode-cn.com/problems/generate-parentheses | ||
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
* @date 2021/10/20 11:26 | ||
*/ | ||
public class LeetCode22 { | ||
|
||
List<String> res = new ArrayList<>(); | ||
|
||
public List<String> generateParenthesis(int n) { | ||
if (n <= 0) { | ||
return res; | ||
} | ||
getParenthesis("", n, n); | ||
return res; | ||
} | ||
|
||
/** | ||
* @title | ||
* @description 递归判断 str + "(" | ||
* @author admin | ||
* @updateTime 2021/10/20 11:28 | ||
* @throws | ||
*/ | ||
private void getParenthesis(String str, int left, int right) { | ||
if (left == 0 && right == 0) { | ||
res.add(str); | ||
return; | ||
} | ||
if (left == right) { | ||
//剩余左右括号数相等,下一个只能用左括号 | ||
getParenthesis(str + "(", left - 1, right); | ||
} else if (left < right) { | ||
//剩余左括号小于右括号,下一个可以用左括号也可以用右括号 | ||
if (left > 0) { | ||
getParenthesis(str + "(", left - 1, right); | ||
} | ||
getParenthesis(str + ")", left, right - 1); | ||
} | ||
} | ||
} |