java笔试-众安保险秋招笔试9.4

一个半小时一共5道编程题

2.最长重复字串

package zhongan;

import java.util.Scanner;

/**
 * @author liu
 * @Description
 */
public class main2 {
    public static void main(String[] args) {

        Scanner ss = new Scanner(System.in);
        String s = ss.nextLine();
        System.out.println(longestRepeatingSubstring(s));
    }
    public static int longestRepeatingSubstring (String s) {
        // write code here
        int l = s.length();
        String sample,left;
        for(int i = l/2;i>=0;--i){
            for(int j = 0;j<l-i;j+=i){
                sample = s.substring(j,i);
                left = s.substring(i);
                if(left.indexOf(sample)!=-1) return sample.length();
            }
        }
        return 0;
    }
}

3.数组元素位异或

    public int xorOperation (int n, int start) {
        // write code here
        int[] nums = new int[n];
        int res = 0;
        for(int i = 0;i<n;i++){
            nums[i] = start + 2*i;
            res ^=nums[i];
        }
        return res;
    }
}

4.翻转字符串
裂开…
我寻思着,你输入就输入,你在例子里面带引号干嘛。。。。。。。。。。。。。
输入:“hello world!”
输出:“world! hello”
需要处理多余得空格

class Solution {
    public String reverseWords(String s) {
        if (s.equals("")) return s;
        //去除首尾空格
        String str = s.trim();
        if (str == null || str.length()==0) {
           return "";
        }
        String[] arr = str.split(" ");
        int length = arr.length;
        StringBuilder builder = new StringBuilder(s.length());
        for(int i=length-1; i>=0; i--) {
            if (!arr[i].equals("") ) {
                builder.append(arr[i]).append(" ");
            }
        }
        return builder.deleteCharAt(builder.length()-1).toString();
    }
}


5.矩阵得k次幂

猜你喜欢

转载自blog.csdn.net/weixin_45773603/article/details/108413429
今日推荐