Skip to content

Commit 9d0eb5e

Browse files
committed
提交
1 parent a2f8546 commit 9d0eb5e

File tree

5 files changed

+233
-0
lines changed

5 files changed

+233
-0
lines changed

160.相交链表.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @lc app=leetcode.cn id=160 lang=java
3+
*
4+
* [160] 相交链表
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(int x) {
14+
* val = x;
15+
* next = null;
16+
* }
17+
* }
18+
*/
19+
public class Solution {
20+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
21+
ListNode temp = null;
22+
ListNode head1 = headA;
23+
ListNode head2 = headB;
24+
int countA = 0;
25+
int countB= 0;
26+
while(head1 != null){
27+
countA++;
28+
head1 = head1.next;
29+
}
30+
while(head2 != null){
31+
countB++;
32+
head2 = head2.next;
33+
}
34+
35+
head1 = headA;
36+
head2= headB;
37+
if(countA > countB){
38+
while(countA>countB){
39+
head1 = head1.next;
40+
countA--;
41+
}
42+
}else if(countB > countA){
43+
while(countB > countA){
44+
head2 = head2.next;
45+
countB--;
46+
}
47+
}
48+
49+
while(head1!=null && head2!=null){
50+
if(head1 == head2){
51+
temp = head1;
52+
break;
53+
}
54+
head1 =head1.next;
55+
head2 = head2.next;
56+
}
57+
58+
return temp;
59+
}
60+
}
61+
// @lc code=end
62+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode.cn id=235 lang=java
3+
*
4+
* [235] 二叉搜索树的最近公共祖先
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(int x) { val = x; }
15+
* }
16+
*/
17+
class Solution {
18+
TreeNode temp;
19+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
20+
las(root, p, q);
21+
return temp;
22+
}
23+
private void las(TreeNode root, TreeNode p, TreeNode q){
24+
if((root.val - p.val) * (root.val - q.val) <=0){
25+
temp = root;
26+
}
27+
if(root.val > p.val && root.val > q.val){
28+
las(root.left,p,q);
29+
}
30+
if(root.val < p.val && root.val < q.val){
31+
las(root.right, p, q);
32+
}
33+
}
34+
}
35+
// @lc code=end
36+

532.数组中的k-diff数对.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.HashSet;
2+
/*
3+
* @lc app=leetcode.cn id=532 lang=java
4+
*
5+
* [532] 数组中的K-diff数对
6+
*/
7+
import java.util.Map;
8+
9+
// @lc code=start
10+
class Solution {
11+
12+
public int findPairs(int[] nums, int k) {
13+
Map<Integer,Integer> map = new HashMap<>();
14+
int count = 0;
15+
if(k < 0){
16+
return 0;
17+
}
18+
for(int i = 0; i < nums.length;i++){
19+
map.putIfAbsent(nums[i], 0);
20+
map.put(nums[i], map.get(nums[i]) + 1);
21+
}
22+
for(int i:map.keySet()){
23+
if(k == 0){
24+
if(map.get(i) > 1){
25+
count++;
26+
}
27+
}else{
28+
if(map.containsKey(i + k)){
29+
count++;
30+
}
31+
}
32+
}
33+
return count;
34+
}
35+
}
36+
// @lc code=end
37+

729.我的日程安排表-i.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.ArrayList;
2+
3+
/*
4+
* @lc app=leetcode.cn id=729 lang=java
5+
*
6+
* [729] 我的日程安排表 I
7+
*/
8+
9+
// @lc code=start
10+
class MyCalendar {
11+
12+
List<int[]> list;
13+
14+
public MyCalendar() {
15+
list = new ArrayList<>();
16+
}
17+
18+
public boolean book(int start, int end) {
19+
for(int[] array:list){
20+
if(start < array[1] && end > array[0] ){
21+
return false;
22+
}
23+
}
24+
list.add(new int[]{start,end});
25+
return true;
26+
}
27+
}
28+
29+
/**
30+
* Your MyCalendar object will be instantiated and called as such:
31+
* MyCalendar obj = new MyCalendar();
32+
* boolean param_1 = obj.book(start,end);
33+
*/
34+
// @lc code=end
35+

笔记.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 笔记
2+
3+
## #235 二叉搜索树的最近公共祖先
4+
5+
```java
6+
class Solution {
7+
TreeNode temp;
8+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
9+
las(root, p, q);
10+
return temp;
11+
}
12+
private void las(TreeNode root, TreeNode p, TreeNode q){
13+
if((root.val - p.val) * (root.val - q.val) <=0){
14+
temp = root;
15+
}
16+
if(root.val > p.val && root.val > q.val){
17+
las(root.left,p,q);
18+
}
19+
if(root.val < p.val && root.val < q.val){
20+
las(root.right, p, q);
21+
}
22+
}
23+
}
24+
```
25+
26+
​ 此算法利用到了二叉搜索树的性质。如果搜寻的两个节点都要大于根节点,那么其公共祖先必定在根节点的右手边。同理当两个节点都要小于根节点的话,其公共祖先必定在左手边。
27+
28+
​ 当要搜寻的两个节点位于根节点的左右两边的话((root.val - p.val) * (root.val - q.val) <= 0),该跟节点为他们的公共祖先。
29+
30+
​ 若不使用该方法的话,可以先使用两个linkedlist分别存储寻找p和q节点过程中经过的节点,然后再进行对比,找到最后一个相同位置的节点。
31+
32+
## #532 数组中的K-diff数对
33+
34+
```java
35+
class Solution {
36+
37+
public int findPairs(int[] nums, int k) {
38+
Map<Integer,Integer> map = new HashMap<>();
39+
int count = 0;
40+
if(k < 0){
41+
return 0;
42+
}
43+
for(int i = 0; i < nums.length;i++){
44+
map.putIfAbsent(nums[i], 0);
45+
map.put(nums[i], map.get(nums[i]) + 1);
46+
}
47+
for(int i:map.keySet()){
48+
if(k == 0){
49+
if(map.get(i) > 1){
50+
count++;
51+
}
52+
}else{
53+
if(map.containsKey(i + k)){
54+
count++;
55+
}
56+
}
57+
}
58+
return count;
59+
}
60+
}
61+
```
62+
63+
跟两数之和类似,使用map进行数量的统计。

0 commit comments

Comments
 (0)