Compare commits

..

3 Commits

Author SHA1 Message Date
811f2f3f15 部分题目 2025-09-06 00:37:50 +08:00
3650ccd44b 部分题目 2025-08-31 22:29:14 +08:00
4cd15619f9 部分题目 2025-08-31 22:29:07 +08:00
16 changed files with 465 additions and 5 deletions

View File

@@ -0,0 +1,4 @@
package cn.celess.common;
public class ListNode {
}

View File

@@ -0,0 +1,36 @@
package cn.celess.leetcode.test125;
class Solution {
public boolean isPalindrome(String s) {
int left =0;
int right = s.length()-1;
while(left <= right){
char leftChar = Character.toLowerCase(s.charAt(left));
char rightChar = Character.toLowerCase(s.charAt(right));
if(!((int)leftChar>=(int)'0' && (int)leftChar<=(int)'9') && !((int)leftChar>=(int)'a' && (int)leftChar<=(int)'z')){
left++;
continue;
}
if(!((int)rightChar>=(int)'0' && (int)rightChar<=(int)'9') && !((int)rightChar>=(int)'a' && (int)rightChar<=(int)'z')){
right--;
continue;
}
if (leftChar != rightChar){
return false;
}
left++;
right--;
}
return true;
}
}
class Main {
public static void main(String[] args) {
System.out.println(new Solution().isPalindrome("race a car"));
System.out.println(new Solution().isPalindrome("A man, a plan, a canal: Panama"));
}
}

View File

@@ -0,0 +1,15 @@
package cn.celess.leetcode.test147;
import cn.celess.common.ListNode;
class Solution {
public boolean hasCycle(ListNode head) {
return true;
}
}
class Main {
public static void main(String[] args) {
}
}

View File

@@ -1,4 +1,4 @@
package cn.celess.leetcode.topinterview150.test26;
package cn.celess.leetcode.test26;
public class Main {
public static void main(String[] args) {

View File

@@ -1,4 +1,4 @@
package cn.celess.leetcode.topinterview150.test26;
package cn.celess.leetcode.test26;
class Solution {
public int removeDuplicates(int[] nums) {

View File

@@ -1,4 +1,4 @@
package cn.celess.leetcode.topinterview150.test27;
package cn.celess.leetcode.test27;
import java.util.Arrays;

View File

@@ -1,4 +1,4 @@
package cn.celess.leetcode.topinterview150.test27;
package cn.celess.leetcode.test27;
class Solution {
public int removeElement(int[] nums, int val) {

View File

@@ -1,4 +1,4 @@
package cn.celess.leetcode.topinterview150.test80;
package cn.celess.leetcode.test80;
public class Solution {

View File

@@ -0,0 +1,56 @@
//给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
//
// 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
//
// 返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
//
//
//
// 示例 1
//
//
//输入:[7,1,5,3,6,4]
//输出5
//解释:在第 2 天(股票价格 = 1的时候买入在第 5 天(股票价格 = 6的时候卖出最大利润 = 6-1 = 5 。
// 注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
//
//
// 示例 2
//
//
//输入prices = [7,6,4,3,1]
//输出0
//解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
//
//
//
//
// 提示:
//
//
// 1 <= prices.length <= 10⁵
// 0 <= prices[i] <= 10⁴
//
//
// Related Topics 数组 动态规划 👍 3876 👎 0
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int maxProfit = 0;
for (int price : prices) {
minPrice = Math.min(minPrice, price);
maxProfit = Math.max(maxProfit, price - minPrice);
}
return maxProfit;
}
}
//leetcode submit region end(Prohibit modification and deletion)
void main() {
System.out.println(new Solution().maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
System.out.println(new Solution().maxProfit(new int[]{7,6,4,3,1}));
System.out.println(new Solution().maxProfit(new int[]{3,2,6,5,0,3}));
}

View File

@@ -0,0 +1,49 @@
//给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
//
// 你可以假设数组是非空的,并且给定的数组总是存在多数元素。
//
//
//
// 示例 1
//
//
//输入nums = [3,2,3]
//输出3
//
// 示例 2
//
//
//输入nums = [2,2,1,1,1,2,2]
//输出2
//
//
//
//提示:
//
//
// n == nums.length
// 1 <= n <= 5 * 10⁴
// -10⁹ <= nums[i] <= 10⁹
//
//
//
//
// 进阶:尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。
//
// Related Topics 数组 哈希表 分治 计数 排序 👍 2542 👎 0
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
}
//leetcode submit region end(Prohibit modification and deletion)
public void main(String[] args) {
System.out.println(new Solution().majorityElement(new int[]{3,2,3}));
System.out.println(new Solution().majorityElement(new int[]{2,2,1,1,1,2,2}));
}

View File

@@ -0,0 +1,73 @@
//给定一个整数数组 nums将数组中的元素向右轮转 k 个位置,其中 k 是非负数。
//
//
//
// 示例 1:
//
//
//输入: nums = [1,2,3,4,5,6,7], k = 3
//输出: [5,6,7,1,2,3,4]
//解释:
//向右轮转 1 步: [7,1,2,3,4,5,6]
//向右轮转 2 步: [6,7,1,2,3,4,5]
//向右轮转 3 步: [5,6,7,1,2,3,4]
//
//
// 示例 2:
//
//
//输入nums = [-1,-100,3,99], k = 2
//输出:[3,99,-1,-100]
//解释:
//向右轮转 1 步: [99,-1,-100,3]
//向右轮转 2 步: [3,99,-1,-100]
//
//
//
// 提示:
//
//
// 1 <= nums.length <= 10⁵
// -2³¹ <= nums[i] <= 2³¹ - 1
// 0 <= k <= 10⁵
//
//
//
//
// 进阶:
//
//
// 尽可能想出更多的解决方案,至少有 三种 不同的方法可以解决这个问题。
// 你可以使用空间复杂度为 O(1) 的 原地 算法解决这个问题吗?
//
//
// Related Topics 数组 数学 双指针 👍 2425 👎 0
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public void rotate(int[] nums, int k) {
k = k % nums.length;
int[] arr = new int[k];
System.arraycopy(nums, nums.length - k, arr, 0, k);
// int right = nums.length - 1;
// while (right >= k) {
// nums[right] = nums[right - k];
// right--;
// }
System.arraycopy(nums, 0, nums, k, nums.length - k);
System.arraycopy(arr, 0, nums, 0, k);
}
}
//leetcode submit region end(Prohibit modification and deletion)
void main() {
// System.out.println(-3 % 1);
// [1,2] -> [2,1] -> [1,2]
int[] arr = new int[]{1,2,3,4,5,6,7};
new Solution().rotate(arr, 3);
System.out.println(Arrays.toString(arr));
}

View File

@@ -0,0 +1,35 @@
<p>给定一个数组 <code>prices</code> ,它的第&nbsp;<code>i</code> 个元素&nbsp;<code>prices[i]</code> 表示一支给定股票第 <code>i</code> 天的价格。</p>
<p>你只能选择 <strong>某一天</strong> 买入这只股票,并选择在 <strong>未来的某一个不同的日子</strong> 卖出该股票。设计一个算法来计算你所能获取的最大利润。</p>
<p>返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 <code>0</code></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>[7,1,5,3,6,4]
<strong>输出:</strong>5
<strong>解释:</strong>在第 2 天(股票价格 = 1的时候买入在第 5 天(股票价格 = 6的时候卖出最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>prices = [7,6,4,3,1]
<strong>输出:</strong>0
<strong>解释:</strong>在这种情况下, 没有交易完成, 所以最大利润为 0。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= prices.length &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= prices[i] &lt;= 10<sup>4</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>动态规划</li></div></div><br><div><li>👍 3876</li><li>👎 0</li></div>

View File

@@ -0,0 +1,42 @@
<p>给你一个整数数组 <code>prices</code> ,其中&nbsp;<code>prices[i]</code> 表示某支股票第 <code>i</code> 天的价格。</p>
<p>在每一天,你可以决定是否购买和/或出售股票。你在任何时候&nbsp;<strong>最多</strong>&nbsp;只能持有 <strong>一股</strong> 股票。你也可以先购买,然后在 <strong>同一天</strong> 出售。</p>
<p>返回 <em>你能获得的 <strong>最大</strong> 利润</em>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>prices = [7,1,5,3,6,4]
<strong>输出:</strong>7
<strong>解释:</strong>在第 2 天(股票价格 = 1的时候买入在第 3 天(股票价格 = 5的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4。
随后,在第 4 天(股票价格 = 3的时候买入在第 5 天(股票价格 = 6的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3。
最大总利润为 4 + 3 = 7 。</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>prices = [1,2,3,4,5]
<strong>输出:</strong>4
<strong>解释:</strong>在第 1 天(股票价格 = 1的时候买入在第 5 天 (股票价格 = 5的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4。
最大总利润为 4 。</pre>
<p><strong>示例&nbsp;3</strong></p>
<pre>
<strong>输入:</strong>prices = [7,6,4,3,1]
<strong>输出:</strong>0
<strong>解释:</strong>在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= prices.length &lt;= 3 * 10<sup>4</sup></code></li>
<li><code>0 &lt;= prices[i] &lt;= 10<sup>4</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>动态规划</li></div></div><br><div><li>👍 2774</li><li>👎 0</li></div>

View File

@@ -0,0 +1,33 @@
<p>给定一个大小为 <code>n</code><em> </em>的数组&nbsp;<code>nums</code> ,返回其中的多数元素。多数元素是指在数组中出现次数 <strong>大于</strong>&nbsp;<code>⌊ n/2 ⌋</code>&nbsp;的元素。</p>
<p>你可以假设数组是非空的,并且给定的数组总是存在多数元素。</p>
<p>&nbsp;</p>
<p><strong>示例&nbsp;1</strong></p>
<pre>
<strong>输入:</strong>nums = [3,2,3]
<strong>输出:</strong>3</pre>
<p><strong>示例&nbsp;2</strong></p>
<pre>
<strong>输入:</strong>nums = [2,2,1,1,1,2,2]
<strong>输出:</strong>2
</pre>
<p>&nbsp;</p>
<strong>提示:</strong>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>
<p>&nbsp;</p>
<p><strong>进阶:</strong>尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。</p>
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li><li>分治</li><li>计数</li><li>排序</li></div></div><br><div><li>👍 2542</li><li>👎 0</li></div>

View File

@@ -0,0 +1,73 @@
<p>罗马数字包含以下七种字符:&nbsp;<code>I</code>&nbsp;<code>V</code>&nbsp;<code>X</code>&nbsp;<code>L</code><code>C</code><code>D</code>&nbsp;&nbsp;<code>M</code></p>
<pre>
<strong>字符</strong> <strong>数值</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>例如, 罗马数字 <code>2</code> 写做&nbsp;<code>II</code>&nbsp;,即为两个并列的 1 。<code>12</code> 写做&nbsp;<code>XII</code>&nbsp;,即为&nbsp;<code>X</code>&nbsp;+&nbsp;<code>II</code>&nbsp;<code>27</code> 写做&nbsp;&nbsp;<code>XXVII</code>, 即为&nbsp;<code>XX</code>&nbsp;+&nbsp;<code>V</code>&nbsp;+&nbsp;<code>II</code>&nbsp;</p>
<p>通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做&nbsp;<code>IIII</code>,而是&nbsp;<code>IV</code>。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为&nbsp;<code>IX</code>。这个特殊的规则只适用于以下六种情况:</p>
<ul>
<li><code>I</code>&nbsp;可以放在&nbsp;<code>V</code>&nbsp;(5) 和&nbsp;<code>X</code>&nbsp;(10) 的左边,来表示 4 和 9。</li>
<li><code>X</code>&nbsp;可以放在&nbsp;<code>L</code>&nbsp;(50) 和&nbsp;<code>C</code>&nbsp;(100) 的左边,来表示 40 和&nbsp;90。&nbsp;</li>
<li><code>C</code>&nbsp;可以放在&nbsp;<code>D</code>&nbsp;(500) 和&nbsp;<code>M</code>&nbsp;(1000) 的左边,来表示&nbsp;400 和&nbsp;900。</li>
</ul>
<p>给定一个罗马数字,将其转换成整数。</p>
<p>&nbsp;</p>
<p><strong>示例&nbsp;1:</strong></p>
<pre>
<strong>输入:</strong>&nbsp;s = "III"
<strong>输出:</strong> 3</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre>
<strong>输入:</strong>&nbsp;s = "IV"
<strong>输出:</strong> 4</pre>
<p><strong>示例&nbsp;3:</strong></p>
<pre>
<strong>输入:</strong>&nbsp;s = "IX"
<strong>输出:</strong> 9</pre>
<p><strong>示例&nbsp;4:</strong></p>
<pre>
<strong>输入:</strong>&nbsp;s = "LVIII"
<strong>输出:</strong> 58
<strong>解释:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong>示例&nbsp;5:</strong></p>
<pre>
<strong>输入:</strong>&nbsp;s = "MCMXCIV"
<strong>输出:</strong> 1994
<strong>解释:</strong> M = 1000, CM = 900, XC = 90, IV = 4.</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 15</code></li>
<li><code>s</code> 仅含字符 <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code></li>
<li>题目数据保证 <code>s</code> 是一个有效的罗马数字,且表示整数在范围 <code>[1, 3999]</code></li>
<li>题目所给测试用例皆符合罗马数字书写规则,不会出现跨位等情况。</li>
<li>IL 和 IM 这样的例子并不符合题目要求49 应该写作 XLIX999 应该写作 CMXCIX 。</li>
<li>关于罗马数字的详尽书写规则,可以参考 <a href="https://baike.baidu.com/item/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97/772296">罗马数字 - 百度百科</a></li>
</ul>
<div><div>Related Topics</div><div><li>哈希表</li><li>数学</li><li>字符串</li></div></div><br><div><li>👍 2988</li><li>👎 0</li></div>

View File

@@ -0,0 +1,44 @@
<p>给定一个整数数组 <code>nums</code>,将数组中的元素向右轮转 <code>k</code><em>&nbsp;</em>个位置,其中&nbsp;<code>k</code><em>&nbsp;</em>是非负数。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> nums = [1,2,3,4,5,6,7], k = 3
<strong>输出:</strong> <span><code>[5,6,7,1,2,3,4]</code></span>
<strong>解释:</strong>
向右轮转 1 步: <span><code>[7,1,2,3,4,5,6]</code></span>
向右轮转 2 步: <span><code>[6,7,1,2,3,4,5]
</code></span>向右轮转 3 步: <span><code>[5,6,7,1,2,3,4]</code></span>
</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre>
<strong>输入:</strong>nums = [-1,-100,3,99], k = 2
<strong>输出:</strong>[3,99,-1,-100]
<strong>解释:</strong>
向右轮转 1 步: [99,-1,-100,3]
向右轮转 2 步: [3,99,-1,-100]</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
<li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>
<p>&nbsp;</p>
<p><strong>进阶:</strong></p>
<ul>
<li>尽可能想出更多的解决方案,至少有 <strong>三种</strong> 不同的方法可以解决这个问题。</li>
<li>你可以使用空间复杂度为&nbsp;<code>O(1)</code>&nbsp;<strong>原地&nbsp;</strong>算法解决这个问题吗?</li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>数学</li><li>双指针</li></div></div><br><div><li>👍 2425</li><li>👎 0</li></div>