京东笔试

public class 前K个调大写后面的调成小写 {

    public static void main(String[] args) {
        // write your code here

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();

        String s = sc.next();

        for (int i = 0; i < n; i++) {
            char t = s.charAt(i);
            if (i < k && t <= 'z' && t >= 'a') {
                t = (char) (t - 32);
            } else if (i >= k && t >= 'A' && t <= 'Z') {
                t = (char) (t + 32);
            }
            System.out.print(t);
        }

    }
}
public class 长城 {
    /**
     * 数组a调成长城数组最小操作次数,原数组的每个元素都可以调成另一个元素
     * 原数组:  a0 a1 a2 a3 a4 a5 ...
     * 长城数组:x  y  x  y  x  y  ...
     * x != y
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        Map<Integer, Integer> map1 = new HashMap<>(1 << 15);
        Map<Integer, Integer> map2 = new HashMap<>(1 << 15);

        for (int i = 0; i < n; i += 2) {
            int x = map1.getOrDefault(a[i], 0);
            map1.put(a[i], x + 1);
        }

        for (int i = 1; i < n; i += 2) {
            int x = map2.getOrDefault(a[i], 0);
            map2.put(a[i], x + 1);
        }
        int res;


        int[] max1 = map1.keySet().stream().sorted((k1, k2) -> map1.get(k2) - map1.get(k1)).limit(2).mapToInt(k -> k).toArray();
        int[] max2 = map2.keySet().stream().sorted((k1, k2) -> map2.get(k2) - map2.get(k1)).limit(2).mapToInt(k -> k).toArray();
        if (max1[0] != max2[0]) {
            res = n - map1.get(max1[0]) - map2.get(max2[0]);
        } else {
            res = Math.min(n - map1.get(max1[0]) - map2.get(max2[1]), n - map1.get(max1[1]) - map2.get(max2[0]));
        }
        System.out.println(res);
    }
}
public class 漂亮串 {


    /**
     * 字符串里出现至少2个“red”子串就是漂亮串
     * 给一个数字n
     * 长n的漂亮串有多少种形式 答案很大 模 1e9+7
     * 1<=n<=1e6
     * <p>
     * 例如: redred,redavfred是漂亮串
     * xsdredrexs 不是
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(beauty(n));
    }


    static final int N = (int) 1e6 + 10;
    static final long mod = (int) (1e9 + 7);
    // 长n的字符串 不包含red的数量
    static final long[] dp0 = new long[N + 1];
    static final long[] f = new long[N + 1];

    // 预处理 dp的过程有难度 不好想
    static {
        f[0] = 1;
        //初始化 26^n
        for (int i = 1; i <= N; i++) {
            f[i] = f[i - 1] * 26 % mod;
        }
        dp0[0] = 1;
        dp0[1] = f[1];
        dp0[2] = f[2];
        dp0[3] = f[3] - 1;
        for (int i = 4; i <= N; i++) {
            // r _ _ _ _ _
            // r e d _ _ _
            // 第一位随便取 后面 i-i位不出现red
            // 但是一旦相邻的出现ed 也会出现red
            // 再扣除掉 高三位是red的情况 ===> 就是一共i位不出现red的情况
            dp0[i] = (f[1] * dp0[i - 1] - dp0[i - 3] + mod) % mod;
        }
    }

    // 长n的字符串 只存在一个red
    // 枚举每一处red出现的位置 red之前和之后的位置都没有red
    static long f1(int n) {
        long res = 0;
        for (int i = 0; i < n - 2; i++) {
            res = (res + dp0[i] * dp0[n - i - 3]) % mod;
        }
        return res;
    }


    // 方法1:
    // 用所有的情况 扣除掉没有red 和只存在1个red的情况
    // 分析复杂度  O(n~N)
    // 预处理只进行一次 占用第一个用例的时间,但是第一个用例数据肯定不大,后面每个用例可以全局共享dp,用到了java语言本身的技巧
    static long beauty(int n) {
        return (f[n] - dp0[n] - f1(n) + mod * 2) % mod;
    }

    // 方法2:
    // 长n且 至少存在2个red
    // 枚举2个red的位置 ,第一个red之前没得red,2个red直接也没得red,第二个red之后随便取
    // 复杂度 N2
    private static long f2(int n) {
        long res = 0;
        for (int i = 0; i < n; i++) {
            long v = dp0[i];
            for (int j = i + 3; j < n - 2; j++) {
                res = (res + getMod(v, dp0[j - i - 3], f[n - j - 3])) % mod;
            }
        }
        return res;
    }

    static long getMod(long... nums) {
        long res = 1;
        for (long x : nums) {
            res = res * x % mod;
        }
        return res;
    }
}
#京东笔试#
全部评论
帮助京东应聘的你们
点赞
送花
回复
分享
发布于 2022-09-28 17:29 河南
这是哪个岗的笔试啊
点赞
送花
回复
分享
发布于 2022-10-06 19:51 湖北
滴滴
校招火热招聘中
官网直投

相关推荐

投递腾讯等公司8个岗位
点赞 评论 收藏
转发
1 11 评论
分享
牛客网
牛客企业服务