21. 合并两个有序链表
问题
我的解答
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
// 新建结果头结点的前节点
ListNode resultNode = new ListNode();
// 定义游标节点,并设置初始值为结果头结点的前节点
ListNode cursor = new ListNode();
resultNode = cursor;
// 判断两个链表是否同时为空
while (list1 != null && list2 != null) {
// 若都不为空
// 比较链表头结点值的大小,取小的作为游标节点的下一节点,并将游标节点和所取节点均指向下一节点
if (list1.val <= list2.val) {
cursor.next = list1;
list1 = list1.next;
cursor = cursor.next;
} else {
cursor.next = list2;
list2 = list2.next;
cursor = cursor.next;
}
}
// 如果两链表已经为空,则返回结果节点的后一节点
if (list1 == null && list2 == null) {
return resultNode.next;
}
// 若list1链表为空,则将list2链表的后续节点加入结果链表
if (list1 == null) {
cursor.next = list2;
}
// 若list2链表为空,则将list1链表的后续节点加入结果链表
if (list2 == null) {
cursor.next = list1;
}
// 返回结果节点的后一节点
return resultNode.next;
}
}
官方解答
方法一:递归
代码
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
} else if (l2 == null) {
return l1;
} else if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}
方法二:迭代
代码
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode prehead = new ListNode(-1);
ListNode prev = prehead;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
prev.next = l1;
l1 = l1.next;
} else {
prev.next = l2;
l2 = l2.next;
}
prev = prev.next;
}
// 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
prev.next = l1 == null ? l2 : l1;
return prehead.next;
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/merge-two-sorted-lists/solutions/226408/he-bing-liang-ge-you-xu-lian-biao-by-leetcode-solu/
来源:力扣(LeetCode)
22. 括号生成
我的解答
不会。。。。。。(理不直气不壮)
根据网友的思路进行了稍微的修改,将直接使用String
改成了StringBuilder
class Solution {
// 新建结果列表
List<String> res = new ArrayList<>();
public List<String> generateParenthesis(int n) {
// 若n小于等于0,则返回空列表
if (n <= 0) {
return res;
}
// 新建字符串缓冲器存放结果字符串
StringBuilder sb = new StringBuilder();
// 调用方法
getParenthesis(sb, n, n);
return res;
}
// 定义方法
private void getParenthesis(StringBuilder sb, int left, int right) {
// 若剩余左括号数量和剩余右括号数量接为0时,则将结果字符串添加至结果列表
if (left == 0 && right == 0) {
res.add(sb.toString());
return;
}
if (left == right) {
//剩余左右括号数相等,下一个只能用左括号
getParenthesis(sb.append("("), left - 1, right);
// 用完回退
sb.deleteCharAt(sb.length()-1);
} else if (left < right) {
//剩余左括号小于右括号,下一个可以用左括号也可以用右括号
if (left > 0) {
getParenthesis(sb.append("("), left - 1, right);
// 用完回退
sb.deleteCharAt(sb.length()-1);
}
getParenthesis(sb.append(")"), left, right - 1);
// 用完回退
sb.deleteCharAt(sb.length()-1);
}
}
}
官方解答
方法一:暴力法
代码
class Solution {
public List<String> generateParenthesis(int n) {
List<String> combinations = new ArrayList<String>();
generateAll(new char[2 * n], 0, combinations);
return combinations;
}
public void generateAll(char[] current, int pos, List<String> result) {
if (pos == current.length) {
if (valid(current)) {
result.add(new String(current));
}
} else {
current[pos] = '(';
generateAll(current, pos + 1, result);
current[pos] = ')';
generateAll(current, pos + 1, result);
}
}
public boolean valid(char[] current) {
int balance = 0;
for (char c: current) {
if (c == '(') {
++balance;
} else {
--balance;
}
if (balance < 0) {
return false;
}
}
return balance == 0;
}
}
方法二:回溯法
代码
class Solution {
public List<String> generateParenthesis(int n) {
List<String> ans = new ArrayList<String>();
backtrack(ans, new StringBuilder(), 0, 0, n);
return ans;
}
public void backtrack(List<String> ans, StringBuilder cur, int open, int close, int max) {
if (cur.length() == max * 2) {
ans.add(cur.toString());
return;
}
if (open < max) {
cur.append('(');
backtrack(ans, cur, open + 1, close, max);
cur.deleteCharAt(cur.length() - 1);
}
if (close < open) {
cur.append(')');
backtrack(ans, cur, open, close + 1, max);
cur.deleteCharAt(cur.length() - 1);
}
}
}
方法三:按括号序列的长度递归
代码
class Solution {
ArrayList[] cache = new ArrayList[100];
public List<String> generate(int n) {
if (cache[n] != null) {
return cache[n];
}
ArrayList<String> ans = new ArrayList<String>();
if (n == 0) {
ans.add("");
} else {
for (int c = 0; c < n; ++c) {
for (String left: generate(c)) {
for (String right: generate(n - 1 - c)) {
ans.add("(" + left + ")" + right);
}
}
}
}
cache[n] = ans;
return ans;
}
public List<String> generateParenthesis(int n) {
return generate(n);
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/generate-parentheses/solutions/192912/gua-hao-sheng-cheng-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
网友解答
代码
class Solution {
List<String> res = new ArrayList<>();
public List<String> generateParenthesis(int n) {
if(n <= 0){
return res;
}
getParenthesis("",n,n);
return res;
}
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);
}
}
}