給自己的目標(biāo):[LeetCode](https://leetcode.com/ "Online Judge Platform") 上每日一題
在做題的過程中記錄下解題的思路或者重要的代碼碎片以便后來翻閱妻率。
項(xiàng)目源碼:github上的Leetcode
21. Merge Two Sorted Lists
題目:給出兩個(gè)有序的字節(jié)列表呛踊,將其按照大小合并。
與題目4的解題思路相同狞换,同樣也是從兩個(gè)頭部開始逐步比較大小污呼。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(-1);
ListNode temp = head;
while (l1 != null || l2 != null) {
if (l1 == null) {
head.next = l2;
break;
} else if (l2 == null) {
head.next = l1;
break;
}
if (l1.val <= l2.val) {
head.next = l1;
l1 = l1.next;
head = head.next;
} else {
head.next = l2;
l2 = l2.next;
head = head.next;
}
}
return temp.next;
}
}
22. Generate Parentheses
題目:給出一個(gè)數(shù)值 n,求能生成所有合法結(jié)構(gòu)的組合裕坊。
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
一道基礎(chǔ)的深度優(yōu)先算法,當(dāng)左括號(hào)的個(gè)數(shù)小于右括號(hào)的個(gè)數(shù)時(shí)退出循環(huán)遞歸燕酷。當(dāng)左括號(hào)和右括號(hào)為0時(shí),加入結(jié)果集。
public class Solution {
public List<String> generateParenthesis(int n) {
List<String> temp = new ArrayList<>();
generateP("",n,n,temp);
return temp;
}
public void generateP(String str, int left, int right, List<String> list) {
if (left > right) {
return;
}
if (left > 0) {
generateP(str + "(", left - 1, right, list);
}
if (right > 0) {
generateP(str + ")", left, right - 1, list);
}
if (left == 0 && right == 0) {
list.add(str);
}
}
}
23. Merge k Sorted Lists
題目:合并多個(gè)有序的節(jié)點(diǎn)列表相赁,要求按大小排列。
最初是兩兩從頭合并導(dǎo)致 TLE声诸。之后使用歸并排序來進(jìn)行優(yōu)化。
歸并操作:將兩個(gè)順序合并成一個(gè)順序序列的方法
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeKLists(ListNode[] lists) {
List<ListNode> list = new ArrayList<>();
Collections.addAll(list, lists);
return mergeKLists(list);
}
public ListNode mergeKLists(List<ListNode> lists) {
if (lists == null || lists.size() == 0) return null;
if (lists.size() == 1) return lists.get(0);
int length = lists.size();
int mid = (length - 1) / 2;
ListNode l1 = mergeKLists(lists.subList(0, mid + 1));
ListNode l2 = mergeKLists(lists.subList(mid + 1, length));
return merge2Lists(l1, l2);
}
public ListNode merge2Lists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(-1);
ListNode temp = head;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
temp.next = l1;
l1 = l1.next;
} else {
temp.next = l2;
l2 = l2.next;
}
temp = temp.next;
}
if (l1 != null) {
temp.next = l1;
} else if (l2 != null) {
temp.next = l2;
}
return head.next;
}
}
24. Swap Nodes in Pairs
題目: 給出一個(gè)字節(jié)列表苹享,兩個(gè)數(shù)為一組進(jìn)行交換。節(jié)點(diǎn)里面的 val 是無法被改變的浴麻。
Given 1->2->3->4, you should return the list as 2->1->4->3.
交換時(shí)要知道四個(gè)數(shù)得问,交換的i1和i2,i1前一個(gè)數(shù)i0,i2的后一個(gè)數(shù)i3.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode v = new ListNode(-1);
v.next = head;
ListNode res = v;
while (v.next != null && v.next.next != null) {
ListNode temp = v.next;
ListNode l1 = temp.next;
ListNode l2 = temp.next.next;
l1.next = temp;
temp.next = l2;
v.next = l1;
v = v.next.next;
}
return res.next;
}
}
25. Reverse Nodes in k-Group
題目:給出一組節(jié)點(diǎn)列表和一個(gè)數(shù)字软免,按組來反轉(zhuǎn)節(jié)點(diǎn)宫纬,組里面數(shù)字的個(gè)數(shù)為輸入時(shí)給出的數(shù)字。
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
我的解法是先將所有 ListNode 放入隊(duì)列中膏萧,再按照K值來進(jìn)行反轉(zhuǎn)漓骚。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null) return head;
List<ListNode> nodeList = new ArrayList<>();
while (head != null) {
nodeList.add(head);
head = head.next;
}
int size = nodeList.size();
int start = 0;
while (size >= k) {
swapNodes(nodeList, start, start + k - 1);
size = size - k;
start = start + k;
}
ListNode v = nodeList.get(0);
ListNode temp = v;
for (int i = 1; i < nodeList.size(); i++) {
temp.next = nodeList.get(i);
temp = temp.next;
}
temp.next = null;
return v;
}
public void swapNodes(List<ListNode> list, int start, int end) {
if (end >= list.size()) {
return;
}
while (start < end) {
ListNode temp = list.get(start);
list.set(start, list.get(end));
list.set(end, temp);
start++;
end--;
}
}
}