小米2022校招笔试编程题(9月8日)

1、求最长公共子序列的长度(动态规划)

题目描述:

给定两个字符串str1和str2,输出两个字符串的最长公共子序列的长度,如果最长公共子序列为空,则返回“0”,目前给出的数据,仅仅会存一个最长公共子序列。

输入描述:

输入:

1A2C3D4B56

B1D23A456A

输出描述

输出:6

在这里插入图片描述

package xiaomi;

import java.util.Scanner;

/**
 * @program: 数据结构
 * @description:
 * @author: hjc
 * @create: 2021-09-09 09:16
 **/
public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String str1 = input.next();
        String str2 = input.next();
        int length = getMaxSub(str1,str2);
        System.out.println(length);
    }
    
    public static int getMaxSub(String str1,String str2){
        int n = str1.length();
        int m = str2.length();
        //构建辅助二维数组
        int[][] visited = new int[n + 1][m + 1];
        for (int row = 0; row <= n; row++) {
            for (int col = 0; col <= m; col++) {
                if (row == 0 || col == 0){
                    visited[row][col] = 0;
                }else if (str1.charAt(row - 1) == str2.charAt(col - 1)){
                    //原有最长公共子序列长度加一
                    visited[row][col] = visited[row - 1][col - 1] + 1;
                }else {
                    visited[row][col] = Math.max(visited[row][col - 1],visited[row - 1][col]);
                }
            }
        }

        /*for (int[] ints : visited) {
            for (int anInt : ints) {
                System.out.print(anInt+" ");
            }
            System.out.println();
        }*/
        return visited[n][m];
    }
}

2、红白蓝彩条排序(leetcode三色排序

题目描述:

给你一个仅有红、白、蓝三种颜色组成的10个条块序列,现需要你将这些条块按照红、白、蓝的顺序排好,可用1代表红色,2代表白色,3代表蓝色,要求时间复杂度为O(n),例如给定彩色条块序列为:

{蓝,白,红,白,蓝,红,白,白,红,蓝}

则要求排列的结果为:

{红,红,红,白,白,白,白,蓝,蓝,蓝}

样例:

输入:{3,2,1,2,3,1,2,2,1,3}

输出:{1,1,1,2,2,2,2,3,3,3}

package xiaomi;

import java.util.*;

/**
 * @program: 数据结构
 * @description:
 * @author: hjc
 * @create: 2021-09-09 09:46
 **/
public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
//        List<Integer> list = new ArrayList<>(10);
//        for (int i = 0; i < 10; i++) {
//            list.add(input.nextInt());
//        }
//        Integer[] colors = list.toArray(new Integer[10]);
//        sortColors(colors);
//        System.out.println(Arrays.asList(colors));
        int[] colors = new int[10];
        for (int i = 0; i < colors.length; i++) {
            colors[i] = input.nextInt();
        }
        sortColors(colors);
        System.out.println(Arrays.toString(colors));
    }

    //双指针交换
    public static void sortColors(int[] colors){
        int size = colors.length;
        int left = 0,right = size - 1;
        int i = 0;
        while (i <= right){
            if (colors[i] == 1){
                swap(colors,left,i);
                left++;
                i++;
            }else if (colors[i] == 2){
                i++;
            }else {
                swap(colors,i,right);
                right--;
            }
        }
    }

    public static void swap(int[] colors,int i,int j){
        int temp = colors[i];
        colors[i] = colors[j];
        colors[j] = temp;
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叁Mar.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值