FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

commit leetcode week2 by aibuilder-leo · Pull Request #404 · algorithm001/algorithm · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (5) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
29 changes: 29 additions & 0 deletions Week_01/id_100/LeetCode_21_100.java
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,29 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode n1, ListNode n2) {
ListNode result = new ListNode(0);
ListNode n0 = result;
while (n1 != null && n1 != null) {
//System.out.println("n1-----" + n1.val + "--------n2-------" + n2.val
if (n1.val < n2.val) {
n0.next = new ListNode(n1.val);
n1 = n1.next;
} else {
n0.next = new ListNode(n2.val);
n2 = n2.next;
}

n0 = n0.next;
}
if(n1 != null) n0.next = n1;
else n0.next = n2;
return result.next;
}
}
28 changes: 28 additions & 0 deletions Week_01/id_100/LeetCode_83_100.java
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,28 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode pre = head;
ListNode pre_next = pre.next;
if(pre == null || pre_next == null){
return head;
}
while(pre_next != null){
if(pre.val == pre_next.val){
pre_next = pre_next.next;
pre = pre_next;
}else{
pre = pre_next;
pre_next = pre_next.next;
}
}
return head;

}
}
24 changes: 24 additions & 0 deletions Week_02/id_100/LeetCode_242_100.java
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,24 @@
class Solution {
public boolean isAnagram(String s, String t) {
boolean result = false;
if (s != null && t != null) {
if (s.length() == t.length()) {
char[] s_char = s.toCharArray();
char[] t_char = t.toCharArray();
int r = 0;
for (int i = 0; i < s_char.length; i++) {
for (int j = 0; j < t_char.length; j++) {
if (s_char[i] == t_char[j]) {
r++;
break;
}
}
}
if (r == s.length() && r == t.length()) {
result = true;
}
}
}
return result;
}
}
34 changes: 34 additions & 0 deletions Week_02/id_100/LeetCode_671_100.java
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,34 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int findSecondMinimumValue(TreeNode root) {
return traversal(root, root.val);
}

private static int traversal(TreeNode root, int rootVal) {
if (root == null) {
return -1;
}
if (root.val > rootVal) {
return root.val;
}

int l = traversal(root.left, rootVal); // 遍历左节点值是否有大于根节点的值 3
System.out.println("left ----> " + l);
int r = traversal(root.right, rootVal); // 遍历右节点值是否有大于根节点的值 7
System.out.println("right --->" + r);
// 如果左右节点都比跟节点大的话,取最小的
if (l >= 0 && r >= 0) {
return Math.min(l, r);
}
// 否则取最大的
return Math.max(l, r);
}
}
33 changes: 33 additions & 0 deletions Week_02/id_100/LeetCode_783_100.java
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,33 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int result = 101;
public int minDiffInBST(TreeNode root) {
if (root != null) {
if (root.left != null) {
int i = Math.abs(root.val - root.left.val);
result = i < result ? i : result;
}
if (root.right != null) {
int i = Math.abs(root.val - root.right.val);
result = i < result ? i : result;
}
int l = minDiffInBST(root.left);
int r = minDiffInBST(root.right);
if (l <= r) {
result = l;
} else {
return r;
}
result = l <= r ? l : r;
}
return result;
}
}

Back | FazBrowse Home | New Git URL