上海易立德公司笔试题目中级水平Java
JAVA卷二
1.给定一个整数数组nums和一个目标值target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标(30分)
package controller;
import java.util.ArrayList;
import java.util.List;
/**
* Created by wanzhongLiao on 2023/5/27.
*/
public class YILIDETestMain {
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 = trim.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.Scanner;
/**
* Created by wanzhongLiao on 2023/5/27.
*/
public class YILIDETest {
public static void main(String[] args) {
System.out.println("Hello World!");
Scanner scanner = new Scanner(System.in);
// String s = scanner.nextLine();
int nextInt = scanner.nextInt();
boolean huiwenStr = isHuiwenStr(nextInt);
if (huiwenStr){
System.out.println("srcInt is huiwenStr.");
}else{
System.out.println("srcInt is not a huiwenStr.");
}
}
/**
* 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。考虑负数。
* @param srcInt
* @return
*/
public static boolean isHuiwenStr(int srcInt){
String s = String.valueOf(srcInt);
String srcStr=s;
if (s.contains("-")){
String substring = srcStr.substring(s.indexOf("-")+1);
srcStr=substring;
}
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;
}
}
#视觉/交互/设计工作体验#Java 开发经验技术详解 文章被收录于专栏
7年Java软件开发技术。 内容概要 Java企业开发编程经验 ,2016年毕业至今 适用人群 Java初级 中级 高级 开发 应届毕业生 大学生或者是喜欢程序开发的从业人员 使用场景及目标 入职开发前置