-
Notifications
You must be signed in to change notification settings - Fork 149
014-第一周作业 #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aiter
wants to merge
38
commits into
algorithm001:master
Choose a base branch
from
aiter:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
014-第一周作业 #95
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
0cece46
push test
9f9fc2d
array sort
33d9233
add leetcode running's comments
64fe4e6
922
7bd3fb2
merge-two-sorted-lists
2db7ce0
delete duplicate node of sorted list
7f00b0f
binary tree's longest-univalue-path. use recursion
3668950
stack: valid-parentheses
29b8d6d
add README
74e8b0d
array valid-anagram
18ed825
add array valid-anagram
b71a3ce
week 1 note
0c9e841
format NOTE
1078f8e
hash+list https://leetcode-cn.com/problems/top-k-frequent-words/submi…
aiter 718872c
binary tree https://leetcode-cn.com/problems/second-minimum-node-in-a…
aiter 7a353d5
insert_sort.c & merge_sort.c
aiter 1b82dc1
stack & bfs/dfs & heap
aiter ed8f5f7
binary search tree & https://leetcode-cn.com/problems/minimum-distanc…
aiter 8b8cf61
NOTE
aiter 9ad8a82
format NOTE
aiter 2f8ab3a
format NOTE
aiter 0dc2a2e
format NOTE
aiter a34cc7e
format NOTE
aiter 61ca47d
LeetCode_997_14.java & graph & https://leetcode-cn.com/problems/find-…
aiter 14da6aa
Merge remote-tracking branch 'origin/master'
aiter 6cdfc39
test from pad
aiter 72bbeb5
heap & top K & https://leetcode-cn.com/problems/kth-largest-element-i…
aiter 52eba87
heap & top K & https://leetcode-cn.com/problems/kth-largest-element-i…
aiter 7b8598a
heap & top K & https://leetcode-cn.com/problems/kth-largest-element-i…
aiter bebe10f
maximum-depth-of-binary-tree & https://leetcode-cn.com/problems/maxim…
aiter 0f61f0f
N叉树 & level-order-traversal & https://leetcode-cn.com/problems/n-ary-…
aiter 3625663
format
aiter 6d8b0e3
递归树、堆和排序、图、深度和广度优先搜索、字符串匹配
aiter daa95a0
trie tree & backtracking
aiter 7b3d159
greedy & assign-cookies
aiter f0d2f93
dynamic programming & https://leetcode-cn.com/problems/min-cost-climb…
aiter 9c697aa
study note
aiter 9234d1b
g Note
aiter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import java.util.Stack; | ||
|
|
||
| /** | ||
| * https://leetcode-cn.com/problems/valid-parentheses/ | ||
| * <p> 栈 stack | ||
| * <p> 简单 | ||
| */ | ||
| public class LeetCode_20_14 { | ||
|
|
||
| public static void main(String[] args) { | ||
| Solution solution = new Solution(); | ||
|
|
||
| String[] arrs = {"()" | ||
| , "()[]{}" | ||
| , "(]" | ||
| , "([)]" | ||
| , "{[]}"}; | ||
| boolean[] results = {true, true, false, false, true}; | ||
| for (int i = 0; i < arrs.length; i++) { | ||
| System.out.println(arrs[i] + ":" + (results[i] == solution.isValid(arrs[i]))); | ||
| } | ||
| } | ||
|
|
||
| static class Solution { | ||
| private final static char[] A = {'(', '{', '[', ']', '}', ')'}; | ||
|
|
||
| /** | ||
| * 利用java的Stack,遇到左括号,就入栈(push),遇到右括号就出栈(pop) | ||
| * <pre> | ||
| * 1. 如果需要出栈时,栈为空,无效表达式 | ||
| * 2. 左右括号的差值,只有1或2,直接使用当前byte和pop的做差值运算。不等于1或2,就不匹配 | ||
| * 3. <strong>性能不太好</strong> | ||
| * </pre> | ||
| * @param s | ||
| * @return | ||
| */ | ||
| public boolean isValid(String s) { | ||
| if (s == null) { | ||
| return true; | ||
| } | ||
| Stack<Character> stack = new Stack<>(); | ||
| char[] chars = s.toCharArray(); | ||
|
|
||
| for (int i = 0; i < chars.length; i++) { | ||
| if (chars[i] == A[0] || chars[i] == A[1] || chars[i] == A[2]) { | ||
| stack.push(chars[i]); | ||
| } else if (chars[i] == A[3] || chars[i] == A[4] || chars[i] == A[5]) { | ||
| if(stack.empty()){ | ||
| return false; | ||
| } | ||
| /** | ||
| * <pre> | ||
| * 40 ( | ||
| * 41 ) | ||
| * 91 [ | ||
| * 93 ] | ||
| * 123 { | ||
| * 125 } | ||
| * </pre> | ||
| */ | ||
| int tmp = chars[i] - stack.pop(); | ||
| if (tmp != 1 && tmp != 2) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return stack.empty(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| /** | ||
| * 执行用时 : 7 ms, 在Valid Parentheses的Java提交中击败了79.22% 的用户 | ||
| * 内存消耗 : 34 MB, 在Valid Parentheses的Java提交中击败了89.16% 的用户 | ||
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| /** | ||
| * https://leetcode-cn.com/problems/merge-two-sorted-lists/submissions/ | ||
| * | ||
| * <p> 链表 | ||
| * <p> 简单 | ||
| * | ||
| * @author Yunjian Liu | ||
| * @date 2019/04/17 | ||
| */ | ||
| public class LeetCode_21_14 { | ||
| public static void main(String[] args) { | ||
| Solution solution = new Solution(); | ||
|
|
||
| ListNode listNode1 = new ListNode(1); | ||
| listNode1.next = new ListNode(2); | ||
| //listNode1.next.next = new ListNode(4); | ||
| listNode1.next.next = new ListNode(2); | ||
| listNode1.next.next.next = new ListNode(2); | ||
| listNode1.next.next.next.next = new ListNode(4); | ||
| listNode1.next.next.next.next.next = new ListNode(5); | ||
| listNode1.next.next.next.next.next.next = new ListNode(5); | ||
|
|
||
| ListNode listNode2 = new ListNode(1); | ||
| listNode2.next = new ListNode(3); | ||
| listNode2.next.next = new ListNode(4); | ||
|
|
||
| ListNode listNode3 = solution.mergeTwoLists(listNode1, listNode2); | ||
|
|
||
| while (listNode3 != null) { | ||
| System.out.print(listNode3.val + "->"); | ||
| listNode3 = listNode3.next; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class Solution { | ||
| /** | ||
| * <pre> | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | --> | 2 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | --> | 3 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ||
| * cur | ||
| * | | ||
| * listNode | ||
| * | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | --> | 2 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * p | ||
| * | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | --> | 3 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ||
| * | ||
| * listNode tmp | ||
| * | | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | --> | 2 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * cur | ||
| * | | ||
| * p | ||
| * | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | --> | 3 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ||
| * listNode p | ||
| * | | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | | 2 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | | ||
| * | | ||
| * cur | ||
| * | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | --> | 3 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ||
| * | ||
| * | ||
| * listNode p | ||
| * | | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | | 2 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | | ||
| * | | ||
| * cur tmp | ||
| * | | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | --> | 3 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ||
| * listNode p | ||
| * | | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | | 2 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ^ | ||
| * | | | ||
| * cur | tmp | ||
| * | | | | ||
| * +-----+ | +-----+ +-----+ | ||
| * | 1 | -->---- | 3 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ||
| * | ||
| * listNode cur | ||
| * | | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | | 2 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ^ | ||
| * | | | ||
| * cur | p | ||
| * | | | | ||
| * +-----+ | +-----+ +-----+ | ||
| * | 1 | -->---- | 3 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ||
| * | ||
| * listNode cur tmp | ||
| * | | | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | | 2 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ^ | ||
| * | | | ||
| * cur | p | ||
| * | | | | ||
| * +-----+ | +-----+ +-----+ | ||
| * | 1 | -->---- | 3 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ||
| * | ||
| * listNode cur tmp | ||
| * | | | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | | 2 | -->----- | 4 | | ||
| * +-----+ +-----+ | +-----+ | ||
| * | ^ | | ||
| * | | | | ||
| * cur | p | ||
| * | | | | ||
| * +-----+ | +-----+ +-----+ | ||
| * | 1 | -->---- | 3 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ||
| * | ||
| * listNode p | ||
| * | | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | | 2 | -->----- | 4 | | ||
| * +-----+ +-----+ | +-----+ | ||
| * | ^ cur | ||
| * | | | | ||
| * cur | | | ||
| * | | | | ||
| * +-----+ | +-----+ +-----+ | ||
| * | 1 | -->---- | 3 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ||
| * | ||
| * listNode p | ||
| * | | | ||
| * +-----+ +-----+ +-----+ | ||
| * | 1 | | 2 | -->----- | 4 | | ||
| * +-----+ +-----+ | +-----+ | ||
| * | ^ | | ||
| * | | | | ||
| * cur | | cur | ||
| * | | | | | ||
| * +-----+ | +-----+ +-----+ | ||
| * | 1 | -->---- | 3 | --> | 4 | | ||
| * +-----+ +-----+ +-----+ | ||
| * | ||
| * </pre> | ||
| * | ||
| * 思路: | ||
| * <pre> | ||
| * 用一个指针,指向当前节点(cur)。另一个指针(p)指向,另一个链还未合并进来的第一个位置。 | ||
| * 1. 比较p的cur的next,如果p更小,那么就把p接到合并链中。 | ||
| * | ||
| * 如果一个链已经比较完,把p链直接接到最后 | ||
| * </pre> | ||
| * <p> 会破坏原来的链表 | ||
| * @param l1 | ||
| * @param l2 | ||
| * @return | ||
| */ | ||
| public ListNode mergeTwoLists(ListNode l1, ListNode l2) { | ||
| if(l1 == null) { | ||
| return l2; | ||
| } | ||
| if(l2 == null) { | ||
| return l1; | ||
| } | ||
| ListNode listNode = l1; | ||
|
|
||
| ListNode p = l2; | ||
| if (l2.val < l1.val) { | ||
| listNode = l2; | ||
| p = l1; | ||
| } | ||
| ListNode cur = listNode; | ||
|
|
||
| while (cur.next != null) { | ||
| if (p.val < cur.next.val) { | ||
| ListNode tmp = cur.next; | ||
| cur.next = p; | ||
| cur = p; | ||
| p = tmp; | ||
| } else { | ||
| cur = cur.next; | ||
| } | ||
| } | ||
| cur.next = p; | ||
|
|
||
| return listNode; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你的注释好高级!