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

Merge pull request #404 from bin-albin/master · feixiangcode/algorithm@7df6f40 · GitHub

Commit 7df6f40

Browse files
Merge pull request algorithm001#404 from bin-albin/master
commit leetcode week2
2 parents 23d3e29 + b5162b8 commit 7df6f40

5 files changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode mergeTwoLists(ListNode n1, ListNode n2) {
11+
ListNode result = new ListNode(0);
12+
ListNode n0 = result;
13+
while (n1 != null && n1 != null) {
14+
//System.out.println("n1-----" + n1.val + "--------n2-------" + n2.val
15+
if (n1.val < n2.val) {
16+
n0.next = new ListNode(n1.val);
17+
n1 = n1.next;
18+
} else {
19+
n0.next = new ListNode(n2.val);
20+
n2 = n2.next;
21+
}
22+
23+
n0 = n0.next;
24+
}
25+
if(n1 != null) n0.next = n1;
26+
else n0.next = n2;
27+
return result.next;
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode deleteDuplicates(ListNode head) {
11+
ListNode pre = head;
12+
ListNode pre_next = pre.next;
13+
if(pre == null || pre_next == null){
14+
return head;
15+
}
16+
while(pre_next != null){
17+
if(pre.val == pre_next.val){
18+
pre_next = pre_next.next;
19+
pre = pre_next;
20+
}else{
21+
pre = pre_next;
22+
pre_next = pre_next.next;
23+
}
24+
}
25+
return head;
26+
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public boolean isAnagram(String s, String t) {
3+
boolean result = false;
4+
if (s != null && t != null) {
5+
if (s.length() == t.length()) {
6+
char[] s_char = s.toCharArray();
7+
char[] t_char = t.toCharArray();
8+
int r = 0;
9+
for (int i = 0; i < s_char.length; i++) {
10+
for (int j = 0; j < t_char.length; j++) {
11+
if (s_char[i] == t_char[j]) {
12+
r++;
13+
break;
14+
}
15+
}
16+
}
17+
if (r == s.length() && r == t.length()) {
18+
result = true;
19+
}
20+
}
21+
}
22+
return result;
23+
}
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int findSecondMinimumValue(TreeNode root) {
12+
return traversal(root, root.val);
13+
}
14+
15+
private static int traversal(TreeNode root, int rootVal) {
16+
if (root == null) {
17+
return -1;
18+
}
19+
if (root.val > rootVal) {
20+
return root.val;
21+
}
22+
23+
int l = traversal(root.left, rootVal); // 遍历左节点值是否有大于根节点的值 3
24+
System.out.println("left ----> " + l);
25+
int r = traversal(root.right, rootVal); // 遍历右节点值是否有大于根节点的值 7
26+
System.out.println("right --->" + r);
27+
// 如果左右节点都比跟节点大的话,取最小的
28+
if (l >= 0 && r >= 0) {
29+
return Math.min(l, r);
30+
}
31+
// 否则取最大的
32+
return Math.max(l, r);
33+
}
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
int result = 101;
12+
public int minDiffInBST(TreeNode root) {
13+
if (root != null) {
14+
if (root.left != null) {
15+
int i = Math.abs(root.val - root.left.val);
16+
result = i < result ? i : result;
17+
}
18+
if (root.right != null) {
19+
int i = Math.abs(root.val - root.right.val);
20+
result = i < result ? i : result;
21+
}
22+
int l = minDiffInBST(root.left);
23+
int r = minDiffInBST(root.right);
24+
if (l <= r) {
25+
result = l;
26+
} else {
27+
return r;
28+
}
29+
result = l <= r ? l : r;
30+
}
31+
return result;
32+
}
33+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL