Skip to content

Commit 3fa8903

Browse files
committed
150523
1 parent aca38cb commit 3fa8903

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

src/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import leetcode.editor.en.Q1259.HandshakesThatDontCross;
44
import leetcode.editor.en.Q1498.NumberOfSubsequencesThatSatisfyTheGivenSumCondition;
55
import leetcode.editor.en.Q1697.CheckingExistenceOfEdgeLengthLimitedPaths;
6+
import leetcode.editor.en.Q1721.SwappingNodesInALinkedList;
67
import leetcode.editor.en.Q1752.CheckIfArrayIsSortedAndRotated;
78
import leetcode.editor.en.Q1799.MaximizeScoreAfterNOperations;
89
import leetcode.editor.en.Q188.BestTimeToBuyAndSellStockIv;
@@ -15,6 +16,7 @@
1516
import leetcode.editor.en.Q474.OnesAndZeroes;
1617
import leetcode.editor.en.Q518.CoinChangeII;
1718
import leetcode.editor.en.Q54.SpiralMatrix;
19+
import leetcode.editor.en.Q639.DecodeWaysIi;
1820
import leetcode.editor.en.Q649.Dota2Senate;
1921
import leetcode.editor.en.Q651.FourKeysKeyboard;
2022
import leetcode.editor.en.Q91.DecodeWays;
@@ -32,7 +34,7 @@
3234
public class Main {
3335
public static void main(String[] args) throws IOException {
3436

35-
System.out.println(new MaximizeScoreAfterNOperations().maxScore(toIntArray("[370435,481435,953948,282360,691237,574616,638525,764832]\n")));
37+
System.out.println(new DecodeWaysIi().numDecodings("********"));
3638

3739
}
3840

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package leetcode.editor.en.Q1721;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
class ListNode {
8+
int val;
9+
ListNode next;
10+
11+
ListNode() {
12+
}
13+
14+
ListNode(int val) {
15+
this.val = val;
16+
}
17+
18+
ListNode(int val, ListNode next) {
19+
this.val = val;
20+
this.next = next;
21+
}
22+
}
23+
//leetcode submit region begin(Prohibit modification and deletion)
24+
25+
/**
26+
* Definition for singly-linked list.
27+
* public class ListNode {
28+
* int val;
29+
* ListNode next;
30+
* ListNode() {}
31+
* ListNode(int val) { this.val = val; }
32+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
33+
* }
34+
*/
35+
class Solution {
36+
public ListNode swapNodes(ListNode head, int k) {
37+
if (head == null) return null;
38+
int length = 0;
39+
ListNode current = head;
40+
ListNode beginning = head;
41+
ListNode end = head;
42+
while (current != null) {
43+
length++;
44+
current = current.next;
45+
}
46+
k %= length;
47+
for (int i = 0; i < k - 1; i++) {
48+
if (beginning.next == null) break;
49+
beginning = beginning.next;
50+
}
51+
for (int i = 0; i < length - k; i++) {
52+
if (end.next == null) break;
53+
end = end.next;
54+
}
55+
56+
int temp = beginning.val;
57+
beginning.val = end.val;
58+
end.val = temp;
59+
return head;
60+
61+
62+
}
63+
}
64+
//leetcode submit region end(Prohibit modification and deletion)
65+
66+
67+
public class SwappingNodesInALinkedList extends Solution {
68+
69+
public ListNode swapNodes() {
70+
return super.swapNodes(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))), 2);
71+
}
72+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package leetcode.editor.en.Q639;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
10+
Integer[][] cache;
11+
12+
private int MOD = 1000000007;
13+
14+
public int numDecodings(String s) {
15+
if (
16+
s.charAt(0) == '0' ||
17+
s.contains("30" ) ||
18+
s.contains("40" ) ||
19+
s.contains("50" ) ||
20+
s.contains("60" ) ||
21+
s.contains("70" ) ||
22+
s.contains("80" ) ||
23+
s.contains("90" ) ||
24+
s.contains("00" )
25+
) return 0;
26+
cache = new Integer[s.length()][11];
27+
28+
29+
return (numDecodingsHelper(s.length() - 1, new StringBuilder(s)) % MOD);
30+
31+
32+
}
33+
34+
private int numDecodingsHelper(int i, StringBuilder s) {
35+
if (i < 0) return 1;
36+
int ans = 0;
37+
char c = s.charAt(i);
38+
int cacheKey = c == '*' ? 10 : c - '0';
39+
40+
41+
if (cache[i][cacheKey] != null) {
42+
return cache[i][cacheKey];
43+
}
44+
45+
if (c == '*') {
46+
for (int j = 1; j <= 9; j++) {
47+
s.setCharAt(i, String.valueOf(j).charAt(0));
48+
ans = (ans % MOD + (numDecodingsHelper(i, s) % MOD));
49+
}
50+
s.setCharAt(i, '*');
51+
cache[i][cacheKey] = ans;
52+
return ans;
53+
}
54+
int current = Character.getNumericValue(c);
55+
if (current == 0) {
56+
char prevC = s.charAt(i - 1);
57+
if (prevC == '*') {
58+
ans = (2 * (numDecodingsHelper(i - 2, s) % MOD) % MOD);
59+
} else {
60+
ans = (numDecodingsHelper(i - 2, s) % MOD);
61+
}
62+
cache[i][cacheKey] = ans;
63+
return ans;
64+
}
65+
ans = numDecodingsHelper(i - 1, s) % MOD;
66+
if (i > 0) {
67+
char prevC = s.charAt(i - 1);
68+
if (prevC == '*') {
69+
for (int j = 1; j <= 9; j++) {
70+
int prev = j;
71+
int combination = (prev * 10) + current;
72+
if (combination >= 1 && combination <= 26) {
73+
ans = (ans % MOD + (numDecodingsHelper(i - 2, s) % MOD));
74+
}
75+
}
76+
} else {
77+
int prev = Character.getNumericValue(prevC);
78+
int combination = (prev * 10) + current;
79+
if (combination >= 10 && combination <= 26) {
80+
ans = (ans % MOD + (numDecodingsHelper(i - 2, s) % MOD));
81+
}
82+
}
83+
}
84+
cache[i][cacheKey] = ans;
85+
return ans;
86+
87+
88+
}
89+
}
90+
//leetcode submit region end(Prohibit modification and deletion)
91+
92+
93+
public class DecodeWaysIi extends Solution {
94+
}

0 commit comments

Comments
 (0)