新增 LeetCode 题目解答: 206, 287, 225, 28, 2487, 15, 202

This commit is contained in:
whaifree 2024-07-24 00:11:03 +08:00
parent 6783c55d61
commit a91dc82012
7 changed files with 344 additions and 0 deletions

View File

@ -0,0 +1,58 @@
package cn.whaifree.redo.redo_all_240721;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/23 22:46
* @注释
*/
public class LeetCode15 {
public static void main(String[] args) {
int[] nums = new int[]{-1, 0, 1, 2, -1, -4};
System.out.println(threeSum(nums));
System.out.println(threeSum(new int[]{1,2,1}));
}
public static List<List<Integer>> threeSum(int[] nums) {
int len = nums.length;
ArrayList<List<Integer>> res = new ArrayList<>();
if (len < 3) {
return res;
}
Arrays.sort(nums);
for (int left = 0; left < len - 2; left++) {
int mid = left + 1;
int right = len - 1;
if (left > 0 && nums[left] == nums[left - 1]) {
break;
}
while (mid < right) {
int sum = nums[left] + nums[mid] + nums[right];
if (sum == 0) {
res.add(Arrays.asList(nums[left], nums[mid], nums[right]));
while (mid < right && nums[mid] == nums[mid + 1]) {
mid++;
}
while (mid < right && nums[right] == nums[right - 1]) {
right--;
}
// 如果有重复的必然要跳过
mid++;
right--;
} else if (sum > 0) {
right--;
} else {
mid++;
}
}
}
return res;
}
}

View File

@ -0,0 +1,46 @@
package cn.whaifree.redo.redo_all_240721;
import java.util.HashSet;
import java.util.Set;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/22 23:18
* @注释
*/
public class LeetCode202 {
public static void main(String[] args)
{
int n = 2;
System.out.println(isHappy(n));
System.out.println(isHappy(19));
}
/**
* 2 4 16 1+36=37 9+47=56 25+36=61 36+1=37
* @param n
* @return
*/
public static boolean isHappy(int n)
{
Set<Integer> set = new HashSet<>();
while (n != 1) {
int tmpSum = 0;
while (n > 0) {
int retail = n % 10;
tmpSum += retail * retail;
n /= 10;
}
if (set.contains(tmpSum)) {
return false;
}
set.add(tmpSum);
n = tmpSum;
}
return true;
}
}

View File

@ -0,0 +1,43 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.ListNode;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/22 22:56
* @注释
*/
public class LeetCode206 {
public static void main(String[] args)
{
ListNode listNode = ListNode.listNodeFromArray(new int[]{1, 2, 3, 4, 5});
ListNode.printList(reverseList(listNode));
}
/**
* 递归
* @param head
* @return
*/
public static ListNode reverseList(ListNode head)
{
return reverse(null, head);
}
/**
* a b c d
* @param pre
* @param after
* @return
*/
public static ListNode reverse(ListNode pre, ListNode after) {
if (after == null) {
return pre;
}
ListNode tmp = after.next;
after.next = pre;
return reverse(after, tmp);
}
}

View File

@ -0,0 +1,67 @@
package cn.whaifree.redo.redo_all_240721;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/23 23:43
* @注释
*/
public class LeetCode225 {
public static void main(String[] args)
{
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
System.out.println(myStack.top());
System.out.println(myStack.pop());
System.out.println(myStack.empty());
System.out.println(myStack.pop());
System.out.println(myStack.empty());
}
static class MyStack {
Deque<Integer> q1;
Deque<Integer> q2;
public MyStack() {
q1 = new LinkedList<Integer>();
q2 = new LinkedList<Integer>();
}
public void push(int x) {
q1.add(x);
}
public int pop() {
while (q1.size() > 1) {
q2.add(q1.pop());
}
int tmp = q1.pop();
while (!q2.isEmpty()) {
q1.add(q2.pop());
}
return tmp;
}
public int top() {
while (q1.size() > 1) {
q2.add(q1.pop());
}
int tmp = q1.pop();
q2.add(tmp);
while (!q2.isEmpty()) {
q1.add(q2.pop());
}
return tmp;
}
public boolean empty() {
return q1.isEmpty();
}
}
}

View File

@ -0,0 +1,49 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.ListNode;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/23 23:09
* @注释
*/
public class LeetCode2487 {
public static void main(String[] args) {
ListNode listNode = ListNode.listNodeFromArray(new int[]{99, 2, 13, 3, 8});
removeNodes(listNode).printList();
}
public static ListNode removeNodes(ListNode head) {
head = reverseList(new ListNode(-1, head));
ListNode index = head;
while (index.next != null) {
if (index.val > index.next.val) {
index.next = index.next.next;
continue;
}
index = index.next;
}
return reverseList(head);
}
public static ListNode reverseList(ListNode head) {
return reverse(null, head);
}
/**
* a b c
* @param pre
* @param after
* @return
*/
public static ListNode reverse(ListNode pre, ListNode after) {
if (after == null) {
return pre;
}
ListNode tmp = after.next;
after.next = pre;
return reverse(after, tmp);
}
}

View File

@ -0,0 +1,29 @@
package cn.whaifree.redo.redo_all_240721;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/23 23:37
* @注释
*/
public class LeetCode28 {
public static void main(String[] args)
{
String haystack = "hello";
String needle = "ll";
System.out.println(strStr(haystack, needle));
System.out.println(strStr("a", "a"));
System.out.println(strStr("mississippi", "issip"));
}
public static int strStr(String haystack, String needle)
{
int len = needle.length();
for (int i = 0; i <= haystack.length() - len; i++) {
String substring = haystack.substring(i, i + len);
if (substring.equals(needle)) {
return i;
}
}
return -1;
}
}

View File

@ -0,0 +1,52 @@
package cn.whaifree.redo.redo_all_240721;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/22 23:12
* @注释
*/
public class LeetCode287
{
public static void main(String[] args)
{
int[] nums = {3,1,3,4,2};
System.out.println(findDuplicate(nums));
int[] nums2 = {1,3,4,2,2};
System.out.println(findDuplicate(nums2));
int[] nums3 = {1,1};
System.out.println(findDuplicate(nums3));
}
/**
* 包含 n + 1 个整数的数组 nums 其数字都在 [1, n] 范围内包括 1 n
*
* 3,1,3,4,2
* 0 1 2 3 4
*
* fast = nums[nums[fast]]
* slow = nums[slow]
*
*
* @param nums
* @return
*/
public static int findDuplicate(int[] nums)
{
// 快慢指针
int fast = 0;
int slow = 0;
do {
fast = nums[nums[fast]];
slow = nums[slow];
} while (slow != fast);
fast = 0;
while (fast != slow) {
fast = nums[fast];
slow = nums[slow];
}
return slow;
}
}