寻找比目字母大的最小字母

寻找比目字母大的最小字母:

给你一个排序后的字符列表 letters ,列表中只包含小写英文字母。另给出一个目标字母 target,请你寻找在这一有序列表里比目标字母大的最小字母。

在比较时,字母是依序循环出现的。举个例子:

如果目标字母 target = ‘z’ 并且字符列表为 letters = [‘a’, ‘b’],则答案返回 ‘a’

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-smallest-letter-greater-than-target

示例:

输入: letters = [“c”, “f”, “j”],target = “a”
输出: “c”

//直接利用差值进行判断
public class NextGreatestLetter {
    public static char nextGreatestLetter(char[] letters, char target) {
        int n = letters.length;
        int tar = 26;
        int j = 0;
        char res = letters[0];
        for (int i = 0; i < n; i++) {
            if (target>letters[i]){
                continue;
            }
            int c = Math.abs( target - letters[i]);
            if (c != 0  && tar > c){
                if (j == 0 || letters[i]<=j){
                    tar = c;
                    j = letters[i];
                    res= letters[i];
                }

            }
        }
        return res;

    }

    public static void main(String[] args) {
        char[] letters = {'c','f','j'};
        char target = 'd';
        System.out.println(nextGreatestLetter(letters,target));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是8086汇编语言的程序,用于随机生成一些字母寻找最大和最小值: ``` DATA SEGMENT LETTERS DB 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' NUM_LETTERS DB 10 LETTER_ARR DB 10 DUP(?) MAX DB ? MIN DB ? DATA ENDS CODE SEGMENT ASSUME CS:CODE, DS:DATA START: MOV AX, DATA MOV DS, AX ; generate random letters MOV CX, NUM_LETTERS MOV SI, 0 GENERATE_LETTERS: ; generate a random index into the letters array CALL RANDOM_NUM MOV DL, AL AND DL, 1FH ; mask out all but the lower 5 bits MOV BH, 0 MOV BL, LETTERS[DL] ; load the letter from the array MOV LETTER_ARR[SI], BL ; store the letter in the array INC SI LOOP GENERATE_LETTERS ; find the max and min letters MOV CX, NUM_LETTERS MOV SI, 0 MOV MAX, LETTER_ARR[SI] ; initialize max to first letter MOV MIN, LETTER_ARR[SI] ; initialize min to first letter FIND_MAX_MIN: CMP LETTER_ARR[SI], MAX ; check if current letter is greater than max JG UPDATE_MAX CMP LETTER_ARR[SI], MIN ; check if current letter is less than min JL UPDATE_MIN INC SI LOOP FIND_MAX_MIN ; output results MOV AH, 02H ; set up to print characters MOV DL, MAX INT 21H ; print max letter MOV DL, MIN INT 21H ; print min letter MOV AH, 4CH ; exit to DOS INT 21H ; generates a random number between 0 and 31 RANDOM_NUM: PUSH AX PUSH BX PUSH CX PUSH DX MOV CX, 1000H ; set up delay loop DELAY_LOOP: DEC CX JNZ DELAY_LOOP MOV AH, 2CH ; get system time INT 21H MOV BX, DX ; BX = DX:AX MOV CX, 31 ; set divisor for divide instruction DIV CX ; divide BX by CX, result in AX POP DX POP CX POP BX POP AX RET CODE ENDS END START ``` 该程序使用一个名为 LETTERS 的字节字符串来存储所有可能的字母。它使用 RANDOM_NUM 子程序来生成一个介于 0 和 31 之间的随机数,该数字用作 LETTERS 数组的索引以选择一个字母。它存储每个生成的字母在名为 LETTER_ARR 的字节数组中。然后,它迭代 LETTER_ARR 数组以找到最大和最小值,并将它们存储在 MAX 和 MIN 变量中。最后,它使用 DOS 中断 21H 功能 02H 来打印最大和最小字母
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值