Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 624b130

Browse files
20200825
1 parent d1afef3 commit 624b130

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

‎Java/1032.stream-of-characters.java‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* @lc app=leetcode id=1032 lang=java
3+
*
4+
* [1032] Stream of Characters
5+
*/
6+
7+
// @lc code=start
8+
9+
/*
10+
将dict中的单词逆序存入一个trie中,query的时候倒序在trie中搜索
11+
time: O(n * m) n: number of words m: length of word
12+
space: O(n * m)
13+
*/
14+
class StreamChecker {
15+
class TrieNode {
16+
TrieNode[] children;
17+
boolean isWord;
18+
public TrieNode() {
19+
children = new TrieNode[26];
20+
isWord = false;
21+
}
22+
}
23+
24+
TrieNode root;
25+
StringBuilder sb;
26+
public StreamChecker(String[] words) {
27+
root = new TrieNode();
28+
buildTrie(words);
29+
sb = new StringBuilder();
30+
}
31+
32+
public boolean query(char letter) {
33+
sb.append(letter);
34+
TrieNode cur = root;
35+
for (int i = sb.length() - 1; i >= 0 && cur != null; --i) {
36+
cur = cur.children[sb.charAt(i) - 'a'];
37+
if (cur != null && cur.isWord) {
38+
return true;
39+
}
40+
}
41+
return false;
42+
}
43+
44+
private void buildTrie(String[] words) {
45+
for (String word : words) {
46+
TrieNode cur = root;
47+
// build Trie in reverse order
48+
for (int i = word.length() - 1; i >= 0; --i) {
49+
if (cur.children[word.charAt(i) - 'a'] == null) {
50+
cur.children[word.charAt(i) - 'a'] = new TrieNode();
51+
}
52+
cur = cur.children[word.charAt(i) - 'a'];
53+
}
54+
cur.isWord = true;
55+
}
56+
}
57+
}
58+
59+
/**
60+
* Your StreamChecker object will be instantiated and called as such:
61+
* StreamChecker obj = new StreamChecker(words);
62+
* boolean param_1 = obj.query(letter);
63+
*/
64+
// @lc code=end
65+

‎Java/143.reorder-list.java‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @lc app=leetcode id=143 lang=java
3+
*
4+
* [143] Reorder List
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* public class ListNode {
11+
* int val;
12+
* ListNode next;
13+
* ListNode() {}
14+
* ListNode(int val) { this.val = val; }
15+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
16+
* }
17+
*/
18+
class Solution {
19+
/*
20+
将链表对半分,reverse后一半,再merge list]
21+
time: O(n)
22+
space: O(1)
23+
*/
24+
public void reorderList(ListNode head) {
25+
if (head == null || head.next == null) return;
26+
ListNode slow = head;
27+
ListNode fast = slow.next;
28+
while (fast != null && fast.next != null) {
29+
slow = slow.next;
30+
fast = fast.next.next;
31+
}
32+
ListNode p = slow.next;
33+
slow.next = null;
34+
p = reverse(p);
35+
36+
ListNode cur = head;
37+
while (p != null) {
38+
ListNode temp = cur.next;
39+
ListNode nextP = p.next;
40+
cur.next = p;
41+
p.next = temp;
42+
cur = temp;
43+
p = nextP;
44+
}
45+
}
46+
47+
private ListNode reverse(ListNode head) {
48+
ListNode dummy = new ListNode(0);
49+
while (head != null) {
50+
ListNode temp = head.next;
51+
ListNode tempNext = dummy.next;
52+
dummy.next = head;
53+
head.next = tempNext;
54+
head = temp;
55+
}
56+
return dummy.next;
57+
}
58+
}
59+
// @lc code=end
60+

‎Java/404.sum-of-left-leaves.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @lc app=leetcode id=404 lang=java
3+
*
4+
* [404] Sum of Left Leaves
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
public int sumOfLeftLeaves(TreeNode root) {
25+
if (root == null) return 0;
26+
int res = 0;
27+
if (root.left != null) {
28+
if (root.left.left == null && root.left.right == null) {
29+
res += root.left.val;
30+
} else {
31+
res += sumOfLeftLeaves(root.left);
32+
}
33+
}
34+
res += sumOfLeftLeaves(root.right);
35+
return res;
36+
}
37+
}
38+
// @lc code=end
39+

‎Java/905.sort-array-by-parity.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @lc app=leetcode id=905 lang=java
3+
*
4+
* [905] Sort Array By Parity
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int[] sortArrayByParity(int[] A) {
10+
int i = 0, j = A.length - 1;
11+
while (i < j) {
12+
if ((A[i] % 2 == 1) && (A[j] % 2 == 0)) {
13+
int temp = A[i];
14+
A[i] = A[j];
15+
A[j] = temp;
16+
}
17+
if (A[i] % 2 == 0) i++;
18+
if (A[j] % 2 == 1) j--;
19+
}
20+
return A;
21+
}
22+
}
23+
// @lc code=end
24+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @lc app=leetcode id=983 lang=java
3+
*
4+
* [983] Minimum Cost For Tickets
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int mincostTickets(int[] days, int[] costs) {
10+
Set<Integer> set = new HashSet<>();
11+
for (int day : days) {
12+
set.add(day);
13+
}
14+
int[] dp = new int[366];
15+
for (int i = 1; i <= 365; ++i) {
16+
if (!set.contains(i)) dp[i] = dp[i - 1];
17+
else {
18+
dp[i] = Math.min(dp[i - 1] + costs[0], Math.min(dp[Math.max(0, i - 7)] + costs[1], dp[Math.max(0, i - 30)] + costs[2]));
19+
}
20+
}
21+
return dp[365];
22+
}
23+
}
24+
// @lc code=end
25+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /