短视频制作app,wordpress语言切换 seo,wordpress统计代码,企业信用信息公示官网刷题顺序及部分思路来源于代码随想录#xff0c;网站地址#xff1a;https://programmercarl.com 部分思路来源于力扣官方题解#xff0c;作者主页#xff1a;https://leetcode.cn/u/leetcode-solution/ 242. 有效的字母异位词
给定两个字符串 s 和 t #xff0c;编写一个…刷题顺序及部分思路来源于代码随想录网站地址https://programmercarl.com 部分思路来源于力扣官方题解作者主页https://leetcode.cn/u/leetcode-solution/ 242. 有效的字母异位词
给定两个字符串 s 和 t 编写一个函数来判断 t 是否是 s 的字母异位词。
注意若 s 和 t 中每个字符出现的次数都相同则称 s 和 t 互为字母异位词。
示例
输入: s anagram, t nagaram
输出: true
import java.util.Scanner;/*** author light* Description 有效字母异位词* create 2023-08-01 13:52*/
public class IsAnagramTest {public static void main(String[] args) {Scanner inputnew Scanner(System.in);String str1input.next();String str2input.next();boolean resisAnagram(str1,str2);System.out.println(res);}public static boolean isAnagram(String s, String t) {if(s.length()!t.length()){return false;}int[] numsnew int[26];for (int i 0; i s.length(); i) {nums[s.charAt(i)-a];}for (int i 0; i t.length(); i) {nums[t.charAt(i)-a]--;if(nums[t.charAt(i)-a]0){return false;}}return true;}
}49. 字母异位词分组
给你一个字符串数组请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
字母异位词 是由重新排列源单词的所有字母得到的一个新单词。
输入: strs [eat, tea, tan, ate, nat, bat]
输出: [[bat],[nat,tan],[ate,eat,tea]]
import java.util.*;/*** author light* Description 字母异位词分组*** 方法一排序* 由于互为字母异位词的两个字符串包含的字母相同* 因此对两个字符串分别进行排序之后得到的字符串一定是相同的* 故可以将排序之后的字符串作为哈希表的键。* create 2023-08-01 14:37*/
public class GroupAnagramsTest {public static void main(String[] args) {Scanner inputnew Scanner(System.in);String[] strsinput.next().split(,);ListListString resgroupAnagrams(strs);System.out.println(res);}public static ListListString groupAnagrams(String[] strs){MapString,ListString mapnew HashMap();for(String str:strs){char[] arraystr.toCharArray();Arrays.sort(array);String keyArrays.toString(array);ListString listmap.getOrDefault(key,new ArrayList());list.add(str);map.put(key,list);}return new ArrayListListString(map.values());}
}438. 找到字符串中所有字母异位词
给定两个字符串 s 和 p找到 s 中所有 p 的 异位词 的子串返回这些子串的起始索引。不考虑答案输出的顺序。
异位词 指由相同字母重排列形成的字符串包括相同的字符串
输入: s cbaebabacd, p abc
输出: [0,6]
解释:
起始索引等于 0 的子串是 cba, 它是 abc 的异位词。
起始索引等于 6 的子串是 bac, 它是 abc 的异位词
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;/*** author light* Description 找到字符串中所有字母异位词** 给定两个字符串s和p,找到s中所有p的异位词的子串返回这些子串的起始索引。* 不考虑答案输出的顺序。* 异位词 指由相同字母重排列形成的字符串包括相同的字符串。** 滑动窗口解法* 根据题目要求我们需要在字符串 s 寻找字符串 p 的异位词。* 因为字符串 p 的异位词的长度一定与字符串 p 的长度相同所以我们可以在字符串 s 中* 构造一个长度为与字符串 p 的长度相同的滑动窗口并在滑动中维护窗口中每种字母的数量* 当窗口中每种字母的数量与字符串 p 中每种字母的数量相同时则说明当前窗口为字符串 p 的异位词。** 作者力扣官方题解* 链接https://leetcode.cn/problems/find-all-anagrams-in-a-string/solutions/1123971/zhao-dao-zi-fu-chuan-zhong-suo-you-zi-mu-xzin/* 来源力扣LeetCode* create 2023-08-01 15:01*/
public class FindAnagramsTest {public static void main(String[] args) {Scanner inputnew Scanner(System.in);String sinput.next();String pinput.next();ListInteger listfindAnagrams(s,p);System.out.println(list);}public static ListInteger findAnagrams(String s, String p) {if(s.length()p.length()){return new ArrayListInteger();}int[] sCountnew int[26];int[] pCountnew int[26];ListInteger listnew ArrayList();for (int i 0; i p.length(); i) {sCount[s.charAt(i)-a];pCount[p.charAt(i)-a];}if(Arrays.equals(sCount,pCount)){list.add(0);}for (int i 0; i s.length() - p.length(); i) {//维护滑动窗口sCount[s.charAt(i)-a]--;sCount[s.charAt(ip.length())-a];if(Arrays.equals(sCount,pCount)){list.add(i1);}}return list;}
}349. 两个数组的交集
给定两个数组 nums1 和 nums2 返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。
输入nums1 [1,2,2,1], nums2 [2,2]
输出[2]
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;/*** author light* Description 两个数组的交集* create 2023-08-01 17:34*/
public class IntersectionTest {public static void main(String[] args) {Scanner inputnew Scanner(System.in);int ninput.nextInt();int[] nums1new int[n];int minput.nextInt();int[] nums2new int[m];for (int i 0; i n; i) {nums1[i]input.nextInt();}for (int i 0; i m; i) {nums2[i]input.nextInt();}int[] resintersection(nums1,nums2);System.out.println(Arrays.toString(res));}public static int[] intersection(int[] nums1, int[] nums2) {SetInteger setnew HashSet();SetInteger resnew HashSet();for(int num:nums1){set.add(num);}for(int num:nums2){if(set.contains(num)){res.add(num);}}int[] arrnew int[res.size()];int i0;for(int num:res){arr[i]num;}return arr;}
}350. 两个数组的交集 II
给你两个整数数组 nums1 和 nums2 请你以数组形式返回两数组的交集。返回结果中每个元素出现的次数应与元素在两个数组中都出现的次数一致如果出现次数不一致则考虑取较小值。可以不考虑输出结果的顺序。
输入nums1 [1,2,2,1], nums2 [2,2]
输出[2,2]
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;/*** author light* Description 两个数组的交集 II* create 2023-08-01 18:04*/
public class IntersectTest {public static void main(String[] args) {Scanner inputnew Scanner(System.in);int ninput.nextInt();int[] nums1new int[n];int minput.nextInt();int[] nums2new int[m];for (int i 0; i n; i) {nums1[i]input.nextInt();}for (int i 0; i m; i) {nums2[i]input.nextInt();}int[] resintersect(nums1,nums2);System.out.println(Arrays.toString(res));}public static int[] intersect(int[] nums1, int[] nums2) {MapInteger,Integer mapnew HashMap();for(int num:nums1){map.put(num,map.getOrDefault(num,0)1);}int[] resnew int[nums1.length];int i0;for(int num:nums2){if(map.containsKey(num)map.get(num)0){res[i]num;map.put(num,map.get(num)-1);}}return Arrays.copyOfRange(res,0,i);}
} 文章转载自: http://www.morning.rbjf.cn.gov.cn.rbjf.cn http://www.morning.hmhdn.cn.gov.cn.hmhdn.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.jwwfk.cn.gov.cn.jwwfk.cn http://www.morning.ckhry.cn.gov.cn.ckhry.cn http://www.morning.pmwhj.cn.gov.cn.pmwhj.cn http://www.morning.phxdc.cn.gov.cn.phxdc.cn http://www.morning.jwncx.cn.gov.cn.jwncx.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn http://www.morning.lqjpb.cn.gov.cn.lqjpb.cn http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.mcpdn.cn.gov.cn.mcpdn.cn http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.qgtbx.cn.gov.cn.qgtbx.cn http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.kwwkm.cn.gov.cn.kwwkm.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.qfkxj.cn.gov.cn.qfkxj.cn http://www.morning.wrtpk.cn.gov.cn.wrtpk.cn http://www.morning.fewhope.com.gov.cn.fewhope.com http://www.morning.ztrht.cn.gov.cn.ztrht.cn http://www.morning.xnkh.cn.gov.cn.xnkh.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.hylbz.cn.gov.cn.hylbz.cn http://www.morning.hrypl.cn.gov.cn.hrypl.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.mxmtt.cn.gov.cn.mxmtt.cn http://www.morning.sbrxm.cn.gov.cn.sbrxm.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.glwyn.cn.gov.cn.glwyn.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.btqrz.cn.gov.cn.btqrz.cn http://www.morning.nyqzz.cn.gov.cn.nyqzz.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.wspjn.cn.gov.cn.wspjn.cn http://www.morning.hnrls.cn.gov.cn.hnrls.cn http://www.morning.zkqsc.cn.gov.cn.zkqsc.cn http://www.morning.mwjwy.cn.gov.cn.mwjwy.cn http://www.morning.rhzzf.cn.gov.cn.rhzzf.cn http://www.morning.ztfzm.cn.gov.cn.ztfzm.cn http://www.morning.qsbcg.cn.gov.cn.qsbcg.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.yqpck.cn.gov.cn.yqpck.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.mpgfk.cn.gov.cn.mpgfk.cn http://www.morning.hcwlq.cn.gov.cn.hcwlq.cn http://www.morning.tlrxp.cn.gov.cn.tlrxp.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.wjtxt.cn.gov.cn.wjtxt.cn http://www.morning.zrhhb.cn.gov.cn.zrhhb.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.gjqnn.cn.gov.cn.gjqnn.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.mhnrx.cn.gov.cn.mhnrx.cn http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn http://www.morning.kpbgp.cn.gov.cn.kpbgp.cn http://www.morning.beiyishengxin.cn.gov.cn.beiyishengxin.cn http://www.morning.swkzk.cn.gov.cn.swkzk.cn http://www.morning.zlnf.cn.gov.cn.zlnf.cn http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.nqyzg.cn.gov.cn.nqyzg.cn http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.xnnpy.cn.gov.cn.xnnpy.cn http://www.morning.hmgqy.cn.gov.cn.hmgqy.cn http://www.morning.zwppm.cn.gov.cn.zwppm.cn http://www.morning.zhoer.com.gov.cn.zhoer.com http://www.morning.kchwr.cn.gov.cn.kchwr.cn http://www.morning.qprtm.cn.gov.cn.qprtm.cn