From 7bbd47f65f11e1be939d0ddc8969f1b5b09175d7 Mon Sep 17 00:00:00 2001 From: wu xiangkai Date: Wed, 22 Oct 2025 16:35:11 +0800 Subject: [PATCH] =?UTF-8?q?leetcode=E8=A7=A3=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 125.valid-palindrome.java | 40 +++++++++++++++++++++++++++++++++++++ 136.single-number.java | 26 ++++++++++++++++++++++++ 141.linked-list-cycle.java | 41 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 125.valid-palindrome.java create mode 100644 136.single-number.java create mode 100644 141.linked-list-cycle.java diff --git a/125.valid-palindrome.java b/125.valid-palindrome.java new file mode 100644 index 0000000..147f21c --- /dev/null +++ b/125.valid-palindrome.java @@ -0,0 +1,40 @@ +/* + * @lc app=leetcode id=125 lang=java + * + * [125] Valid Palindrome + */ + +// @lc code=start +class Solution { + public boolean isPalindrome(String s) { + int i = 0, j = s.length()-1; + boolean r = true; + while(i < j) { + while(i= 'a' && c <= 'z' || + c >= 'A' && c <= 'Z' || c >='0' && c <= '9'; + } + + private boolean isSameLeterIgnoreCase(char a, char b) { + return a == b || a - b == 'z' - 'a' || a - b == 'a' - 'z'; + } +} +// @lc code=end + diff --git a/136.single-number.java b/136.single-number.java new file mode 100644 index 0000000..22dc401 --- /dev/null +++ b/136.single-number.java @@ -0,0 +1,26 @@ +/* + * @lc app=leetcode id=136 lang=java + * + * [136] Single Number + */ + +// @lc code=start +class Solution { + /** + * 可以通过对数组中所有整数进行异或来找出该数, + * 因为对于出现两次的整数,异或之后都为0, + * 而对于只出现过一次的整数,和0异或之后为其本身 + * + * @param nums + * @return + */ + public int singleNumber(int[] nums) { + int r = 0; + for(int i = 0; i < nums.length; i++) { + r = r ^ nums[i]; + } + return r; + } +} +// @lc code=end + diff --git a/141.linked-list-cycle.java b/141.linked-list-cycle.java new file mode 100644 index 0000000..9dc0f9b --- /dev/null +++ b/141.linked-list-cycle.java @@ -0,0 +1,41 @@ +/* + * @lc app=leetcode id=141 lang=java + * + * [141] Linked List Cycle + */ + +// @lc code=start +/** + * Definition for singly-linked list. + * class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ +public class Solution { + /** + * 快慢指针,若链表中存在环,则快指针一次动两步,慢指针一次动一步,快指针一定会赶上慢指针 + * + * @param head + * @return + */ + public boolean hasCycle(ListNode head) { + ListNode slow = head, fast = head; + boolean isCycle = false; + while(fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + if(fast == slow) { + isCycle = true; + break; + } + } + return isCycle; + } +} +// @lc code=end +