1. 给定一个整数数组nums和一个目标值target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标(30分)
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; public class YILIDETest { public static void main(String[] args) { int[] intArr=new int[]{2,7,11,15}; List<CustomerIntIndex> customerIntIndexList=getArrIndex(intArr,9); System.out.println(customerIntIndexList); } public static List<CustomerIntIndex> getArrIndex(int[] intArr, int targetNum){ int length = intArr.length; int resultVal=1; for (int i = 0; i < 4; i ) { resultVal*=10; } if(length<2 || length>resultVal){ return null; } int resultVal1=1; for (int i = 0; i < 9; i ) { resultVal1*=10; } resultVal1=resultVal1*(-1); int resultVal2=1; for (int i = 0; i < 9; i ) { resultVal2*=10; } if(targetNum<resultVal1 || targetNum>resultVal2){ return null; } for (int i = 0; i < intArr.length; i ) { int intElement=intArr[i]; if(intElement<resultVal1 || intElement>resultVal2){ return null; } } ArrayList<CustomerIntIndex> arrayList = new ArrayList<CustomerIntIndex>(); for (int i = 0; i < intArr.length-1; i ) { int intArrayEle = intArr[i]; CustomerIntEle customerIntEle = new CustomerIntEle(); customerIntEle.setIdIndex(i); customerIntEle.setIntArrEle(intArrayEle); for (int j = i 1; j < intArr.length; j ) { int intArrayEle2 = intArr[j]; CustomerIntEle customerIntEle2 = new CustomerIntEle(); customerIntEle2.setIdIndex(j); customerIntEle2.setIntArrEle(intArrayEle2); int tempSum=intArrayEle intArrayEle2; if(tempSum==targetNum){ CustomerIntIndex customerIntIndex = new CustomerIntIndex(); customerIntIndex.setIdIndex1(i); customerIntIndex.setIdIndex2(j); arrayList.add(customerIntIndex); } } } return arrayList; } } class CustomerIntIndex{ private Integer idIndex1; private Integer idIndex2; public Integer getIdIndex1() { return idIndex1; } public void setIdIndex1(Integer idIndex1) { this.idIndex1 = idIndex1; } public Integer getIdIndex2() { return idIndex2; } public void setIdIndex2(Integer idIndex2) { this.idIndex2 = idIndex2; } @Override public String toString() { return "[" idIndex1 "," idIndex2 "]"; } } class CustomerIntEle{ private Integer idIndex; private Integer intArrEle; public int getIdIndex() { return idIndex; } public void setIdIndex(int idIndex) { this.idIndex = idIndex; } public int getIntArrEle() { return intArrEle; } public void setIntArrEle(int intArrEle) { this.intArrEle = intArrEle; } } |
---|
2. 给定一个字符串,请你找出其中不含有重复字符的最长子串的长度(30分)
import java.util.*; public class YILIDETest2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String nextLine = scanner.nextLine(); int maxLenSubStr = getMaxLenSubStr(nextLine); System.out.println(maxLenSubStr); } /** * 给定一个字符串,请你找出其中不含有重复字符的最长子串的长度 * @param srcStr * @return */ public static int getMaxLenSubStr(String srcStr){ if (srcStr==null){ return 0; } if (srcStr.isEmpty()){ return 0; } // String trim = srcStr.trim(); // if (trim.isEmpty()){ // return 0; // } int length = srcStr.length(); Random random = new Random(); HashSet<String> stringHashSet = new HashSet<>(); int count=0; while (true){ for (int i = 0; i < length; i ) { int trimStrBegIndex = random.nextInt(length 1); int trimStrEndIndex = random.nextInt(length 1); if (trimStrEndIndex>trimStrBegIndex){ String substring = srcStr.substring(trimStrBegIndex, trimStrEndIndex); stringHashSet.add(substring); } } count ; if (count>1000000){ break; } } System.out.println(stringHashSet); ArrayList<String> stringArrayList = new ArrayList<>(); for (String s : stringHashSet) { boolean checkStrIsRepeatChar = checkStrIsRepeatChar(s); if (!checkStrIsRepeatChar){ stringArrayList.add(s); } } Collections.sort(stringArrayList, new Comparator<String>() { @Override public int compare(String o1, String o2) { int length1 = o1.length(); int length2 = o2.length(); if (length1<length2){ return 1; }else if (length1>length2){ return -1; } return 0; } }); String s = stringArrayList.get(0); System.out.println(stringArrayList); System.out.println(s); return s.length(); } /** * 判断字符串是否含有重复字符 * @param srcStr * @return */ public static boolean checkStrIsRepeatChar(String srcStr){ if (srcStr==null){ return false; } if (srcStr.isEmpty()){ return false; } int length = srcStr.length(); HashMap<Character, Integer> characterIntegerHashMap = new HashMap<>(); for (int i = 0; i < length; i ) { char charAt = srcStr.charAt(i); if (characterIntegerHashMap.containsKey(charAt)){ characterIntegerHashMap.put(charAt,characterIntegerHashMap.get(charAt) 1); }else { characterIntegerHashMap.put(charAt,1); } } System.out.println(characterIntegerHashMap); for (Map.Entry<Character,Integer> entry : characterIntegerHashMap.entrySet()){ Character key = entry.getKey(); Integer value = entry.getValue(); if (value<2){ continue; }else { return true; } } return false; } } |
---|
3. 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。考虑负数。(40分)
//先转成字符串,便于计算
import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Created by Lenovo on 2023/3/16. */ public class BYDTest { public static void main(String[] args){ System.out.println("Hello World!"); String src="abcba"; String src1="abc"; Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); boolean huiwenStr = isHuiwenStr(s); if(huiwenStr){ System.out.println("true"); }else { System.out.println("false"); } } /** * 获取10000内完数的数 * 完数:6=1 2 3; 1,2,3是6的因子 * * @param srcNum * @return */ public static List<Integer> getSpecialNumCnt(int srcNum){ if(srcNum<1){ return null; } if(srcNum>10000){ return null; } return null; } /** * 获取一个整数的乘数因子 * @param srcInt * @return */ public static List<Integer> getFactorList(int srcInt){ if(srcInt<1){ return null; } if(srcInt>10000){ return null; } ArrayList<Integer> integerArrayList = new ArrayList<>(); int srcIntI1=srcInt; for (int i = 0; i < srcInt; i ) { int intTemp=i 1; if(srcInt%intTemp==0){ integerArrayList.add(intTemp); srcIntI1 = srcInt / intTemp; } } return null; } /** * 判断一个字符串是否为回文串 * 字符串为数字和字母 * 限制条件可以自定添加 * @param srcStr * @return */ public static boolean isHuiwenStr(String srcStr){ if(srcStr==null){ return false; } if(srcStr.trim().isEmpty()){ return false; } int length = srcStr.length(); if(length<1){ return false; } if(length>1000000){ return false; } String trimStr = srcStr.trim(); int count=0; for (int i = 0; i < trimStr.length(); i ) { char charAt = trimStr.charAt(i); if(charAt>'A' && charAt<'z'){ count ; continue; } if(Character.isDigit(charAt)){ count ; continue; } } if(count!=trimStr.length()){ return false; } int length1 = trimStr.length(); int length1Temp=0; int count1=0; for (int i = 0; i < trimStr.length(); i ) { if(length1%2==0){ length1Temp=length1/2; }else{ length1Temp=(length1-1)/2; } count1 ; if(count1>length1Temp){ break; } char charAtHead = trimStr.charAt(i); char charAtTail = trimStr.charAt(trimStr.length() - i-1); if(charAtHead==charAtTail){ return true; } } return false; } } |
---|