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次幂

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值