add
This commit is contained in:
parent
0ff3d79282
commit
5e355d9935
62
5.leetcode/src/com/fanxb/common/Q142.java
Normal file
62
5.leetcode/src/com/fanxb/common/Q142.java
Normal file
@ -0,0 +1,62 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 合并两个有序数组
|
||||
* 题目地址:https://leetcode-cn.com/problems/merge-sorted-array/
|
||||
* 解题思路:
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q142 {
|
||||
|
||||
public static class ListNode {
|
||||
int val;
|
||||
ListNode next;
|
||||
|
||||
ListNode(int x) {
|
||||
val = x;
|
||||
next = null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ListNode solution(ListNode head) {
|
||||
Set<ListNode> set = new HashSet<>();
|
||||
while (head != null) {
|
||||
if (set.contains(head)) {
|
||||
return head;
|
||||
} else {
|
||||
set.add(head);
|
||||
head = head.next;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ListNode betterSolution(ListNode head) {
|
||||
ListNode slow = head, fast = head;
|
||||
do {
|
||||
if (fast == null || fast.next == null) {
|
||||
return null;
|
||||
}
|
||||
slow = slow.next;
|
||||
fast = fast.next.next;
|
||||
} while (slow != fast);
|
||||
//此时快慢指针相遇,再加一个指针c
|
||||
ListNode c = head;
|
||||
while (c != slow) {
|
||||
slow = slow.next;
|
||||
c = c.next;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
36
5.leetcode/src/com/fanxb/common/Q167.java
Normal file
36
5.leetcode/src/com/fanxb/common/Q167.java
Normal file
@ -0,0 +1,36 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 两数之和 II - 输入有序数组
|
||||
* <p>
|
||||
* 题目地址:https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/
|
||||
* <p>
|
||||
* 解题思路:
|
||||
* 两个指针从两端开始望中间搜索
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q167 {
|
||||
|
||||
public static int[] solution(int[] numbers, int target) {
|
||||
int i = 0, j = numbers.length - 1;
|
||||
int num;
|
||||
while ((num = numbers[i] + numbers[j]) != target) {
|
||||
if (num > target) {
|
||||
j--;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return new int[]{i+1, j+1};
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] numbers = {2, 7, 11, 15};
|
||||
System.out.println(Arrays.toString(solution(numbers, 9)));
|
||||
}
|
||||
}
|
41
5.leetcode/src/com/fanxb/common/Q88.java
Normal file
41
5.leetcode/src/com/fanxb/common/Q88.java
Normal file
@ -0,0 +1,41 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* 合并两个有序数组
|
||||
* 题目地址:https://leetcode-cn.com/problems/merge-sorted-array/
|
||||
* 解题思路:
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q88 {
|
||||
|
||||
public static void solution(int[] nums1, int m, int[] nums2, int n) {
|
||||
int[] res = new int[nums1.length];
|
||||
int i = 0, j = 0;
|
||||
int count = 0;
|
||||
while (count < m + n) {
|
||||
if (i == m) {
|
||||
res[count++] = nums2[j++];
|
||||
} else if (j == n) {
|
||||
res[count++] = nums1[i++];
|
||||
} else if (nums1[i] < nums2[j]) {
|
||||
res[count++] = nums1[i++];
|
||||
} else {
|
||||
res[count++] = nums2[j++];
|
||||
}
|
||||
}
|
||||
System.arraycopy(res, 0, nums1, 0, count);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums1 = {1, 2, 3, 0, 0, 0};
|
||||
int[] nums2 = {2, 5, 6};
|
||||
solution(nums1, 3, nums2, 3);
|
||||
System.out.println(Arrays.toString(nums1));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user