力扣爆刷第112天之CodeTop100五连刷46-50
文章目录
-
-
- 力扣爆刷第112天之CodeTop100五连刷46-50
- [一、148. 排序链表](#一、148. 排序链表)
- [二、22. 括号生成](#二、22. 括号生成)
- [三、70. 爬楼梯](#三、70. 爬楼梯)
- [四、2. 两数相加](#四、2. 两数相加)
- [五、165. 比较版本号](#五、165. 比较版本号)
-
一、148. 排序链表
题目链接:https://leetcode.cn/problems/sort-list/description/
思路:链表的归并排序,先递归分割链表,然后比较合并,注意分割操作,和边界条件。
java
class Solution {
public ListNode sortList(ListNode head) {
return cutList(head, null);
}
ListNode cutList(ListNode head, ListNode end) {
if(head == null) return head;
if(head.next == end) {
head.next = null;
return head;
}
ListNode slow = head, fast = head;
while(fast != end) {
slow = slow.next;
fast = fast.next;
if(fast != end) {
fast = fast.next;
}
}
ListNode left = cutList(head, slow);
ListNode right = cutList(slow, end);
return merge(left, right);
}
ListNode merge(ListNode node1, ListNode node2) {
ListNode root = new ListNode(), temp = root;
while(node1 != null && node2 != null) {
if(node1.val <= node2.val) {
temp.next = node1;
node1 = node1.next;
}else{
temp.next = node2;
node2 = node2.next;
}
temp = temp.next;
}
if(node1 != null) {
temp.next = node1;
}
if(node2 != null) {
temp.next = node2;
}
return root.next;
}
}
二、22. 括号生成
题目链接:https://leetcode.cn/problems/generate-parentheses/description/
思路:排列题,但是不需要考虑去重,只需要正常排列,然后收获结果的时候进行判断是否合法。
java
class Solution {
char[] source = {'(', ')'};
List<String> list = new ArrayList<>();
StringBuilder builder = new StringBuilder();
public List<String> generateParenthesis(int n) {
backTracking(n*2);
return list;
}
void backTracking(int n) {
if(builder.length() == n) {
if(isTrue(builder)) {
list.add(builder.toString());
}
return;
}
for(int i = 0; i < 2; i++) {
builder.append(source[i]);
backTracking(n);
builder.deleteCharAt(builder.length()-1);
}
}
boolean isTrue(StringBuilder builder) {
int n = 0;
for (int i = 0; i < builder.length(); i++) {
if(builder.charAt(i) == '(') {
n++;
}else{
n--;
}
if(n < 0) return false;
}
return n == 0;
}
}
三、70. 爬楼梯
题目链接:https://leetcode.cn/problems/climbing-stairs/description/
思路:最简单的动态规划,每一个位置依赖于前两个位置,分别为跨一步和跨两步。
java
class Solution {
public int climbStairs(int n) {
if(n < 4) return n;
int a = 1, b = 2, c = 0;
for(int i = 3; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return c;
}
}
四、2. 两数相加
题目链接:https://leetcode.cn/problems/add-two-numbers/description/
思路:遍历相加,然后用一个变量记录进制值,下次计算时加进去。
java
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int res = 0;
ListNode root = new ListNode(), p = root;
while(l1 != null || l2 != null) {
int a = l1 != null ? l1.val : 0;
int b = l2 != null ? l2.val : 0;
int sum = a+b+res;
res = sum / 10;
ListNode t = new ListNode(sum%10);
p.next = t;
p = t;
l1 = l1 != null ? l1.next : null;
l2 = l2 != null ? l2.next : null;
}
if(res == 1) {
p.next = new ListNode(1);
}
return root.next;
}
}
五、165. 比较版本号
题目链接:https://leetcode.cn/problems/compare-version-numbers/description/
思路:利用乘10循环计数的方法,前导0乘10后还是0,剩下的就简单轻松了,不等就判断返回,等就进行下一轮计算。
java
class Solution {
public int compareVersion(String version1, String version2) {
int i = 0, j = 0, m = version1.length(), n = version2.length();
while(i < m || j < n) {
int x = 0;
for(; i < m && version1.charAt(i) != '.'; i++) {
x = x * 10 + version1.charAt(i) - '0';
}
i++;
int y = 0;
for(; j < n && version2.charAt(j) != '.'; j++) {
y = y * 10 + version2.charAt(j) - '0';
}
j++;
if(x != y) {
return x > y ? 1 : -1;
}
}
return 0;
}
}