随笔分类 -  leetcode

算法题
摘要:leetcode_打卡2 1071. 字符串的最大公因子 思路: 该题的答案一定是两个字符串的公共前缀,找到最大公共前缀,并且验证这个前缀能否被两个字符串除尽! class Solution { public String gcdOfStrings(String str1, String str2) 阅读全文
posted @ 2023-04-13 13:18 ZLey 阅读(8) 评论(0) 推荐(0) 编辑
摘要:leetcode_打卡3 题目:1431. 拥有最多糖果的孩子 解答: class Solution { public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) { int max=0; int n=candies. 阅读全文
posted @ 2023-04-13 13:17 ZLey 阅读(12) 评论(0) 推荐(0) 编辑
摘要:leetcode_打卡1 题目:1768. 交替合并字符串 解答: 思路: 模拟即可,字符串的提取: a.charAt(i) class Solution { public String mergeAlternately(String word1, String word2) { String re 阅读全文
posted @ 2023-04-11 11:09 ZLey 阅读(7) 评论(0) 推荐(0) 编辑
摘要:题目:70. 爬楼梯 思路: 转移方程:斐波那契数列 代码: class Solution { public int climbStairs(int n) { //a[n]=a[n-1]+a[n-2],a1=1,a2=2; 关键 return A(n); } int result[]=new int 阅读全文
posted @ 2023-03-07 13:52 ZLey 阅读(9) 评论(0) 推荐(0) 编辑
摘要:每日一题9 题目:5. 最长回文子串 思路:dp public class Solution { public String longestPalindrome(String s) { int len = s.length(); if (len < 2) { return s; } int maxL 阅读全文
posted @ 2023-03-04 22:02 ZLey 阅读(11) 评论(0) 推荐(0) 编辑
摘要:题目:3. 无重复字符的最长子串 解题思路:滑动窗口 代码: class Solution { public int lengthOfLongestSubstring(String s) { Set<Character> set=new HashSet<Character>(); // 使用Hash 阅读全文
posted @ 2023-03-02 22:05 ZLey 阅读(20) 评论(0) 推荐(0) 编辑
摘要:题目:2. 两数相加 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.v 阅读全文
posted @ 2023-03-01 16:58 ZLey 阅读(10) 评论(0) 推荐(0) 编辑
摘要:题目:14. 最长公共前缀 思路:分离出字符串数组中的每个字符串,每个字符串的第一个字符两两比较,如果全部相同,则拼接到结果字符串中,然后比较第二个位置上的字符。依次进行即可 class Solution { public String longestCommonPrefix(String[] st 阅读全文
posted @ 2023-02-28 09:34 ZLey 阅读(12) 评论(0) 推荐(0) 编辑
摘要:题目:13. 罗马数字转整数 思路: 代码: class Solution { Map<Character, Integer> symbolValues = new HashMap<Character, Integer>() {{ put('I', 1); put('V', 5); put('X', 阅读全文
posted @ 2023-02-27 14:21 ZLey 阅读(13) 评论(0) 推荐(0) 编辑
摘要:题目:26. 删除有序数组中的重复项 解答: 本人思路:再创建一个数组result,然后把nums数组的内容复制到result中,nums数据全部赋值10001(大于10000就行),然后两个数据依次比对(先把nums[0]=result[0]),把不相同的数据加入到nums中即可,最后得到目标数组 阅读全文
posted @ 2023-02-21 13:47 ZLey 阅读(9) 评论(0) 推荐(0) 编辑
摘要:题目: 16. 最接近的三数之和 class Solution { public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); //数组排序 int min = Integer.MAX_VALUE; //整数最大值 阅读全文
posted @ 2023-02-21 13:46 ZLey 阅读(15) 评论(0) 推荐(0) 编辑
摘要:题目:15. 三数之和 初始版: 遇到了问题,在进行了T.add(S)后,最终的T却是空值。 import java.util.*; class S2 { public List<List<Integer>> threeSum(int[] nums) { int i=0,j=1,k=2; List< 阅读全文
posted @ 2023-02-19 15:51 ZLey 阅读(17) 评论(0) 推荐(0) 编辑
摘要:题目:11. 盛最多水的容器 采用双指针法: 两端向中间移动,每次移动短板,计算此时的容量,和res进行比较,最后留下最大的即可。 class Solution { public int maxArea(int[] height) { int n=height.length; int i=0; in 阅读全文
posted @ 2023-02-18 16:45 ZLey 阅读(21) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示