牛客刷题--找数字-- 字符串检测-字符串 双指针

1.找数字

java 复制代码
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int left=sc.nextInt();
        int right=sc.nextInt();
        int target=sc.nextInt();


        for(int i=left;i<=right;i++){
            if(i%target==0)
            {
                    System.out.print(i);
                  return; 
            }
        }
         System.out.print(-1);
    }
}

2.买橘子


java 复制代码
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        //最少需要购买的袋数。
        
        for (int i = n / 8; i >= 0; i--) {
            int rest = n - i * 8;
            if (rest % 6 == 0) {
                System.out.println(i + rest / 6);
                return;
            }
        }
        
        System.out.println(-1);
    }
}

3.检测合法字符串


java 复制代码
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int  n=sc.nextInt();
        String  s=sc.next();

        if (n == 0) {
            System.out.println(0);
            return;
        }
        //求最大值,不断更新
        int max=0;
        int cur=0;
        //注意这种检测要关注上一个字符
        char last=' ';
        for(int i=0;i<n;i++){
            char c=s.charAt(i);
                //笑声是字母a和hh交替的序列
                //// 不是a/h,直接中断序列
                if(c!='a'&&c!='h'){
                    cur=0;
                    last=' ';
                    continue;
                }
                //如果是a 或着 h 判断是不是第一个 而且不能重复
                else if(last==' '||c!=last){
                    cur++;
                    last=c;
                    max=Math.max(max,cur);
                }else{
                    //出现了重复
                    cur=1;
                        max=Math.max(cur,max);
                }
        }
        System.out.print(max);

    }
}

4.小红的回文字符串


常规的回文字符串

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        
        boolean isHuiwen = true;
        int left = 0;
        int right = s.length() - 1;
        
        // 暴力双指针比对
        while (left < right) {
            // 只要有一对不一样,就不是回文
            if (s.charAt(left) != s.charAt(right)) {
                isHuiwen = false;
                break;
            }
            left++;
            right--;
        }
        
        System.out.println(isHuiwen ? "YES" : "NO");
    }
}
相关推荐
鱟鲥鳚8 小时前
Spring Boot 集成 LangChain4j:从模型调用到 Tool Calling(Demo版)
java·spring boot
-银雾鸢尾-9 小时前
C#中的StringBuilder相关方法
开发语言·c#
大模型码小白10 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
腾渊信息科技公司11 小时前
Spring Boot对接MES实战:视觉检测数据自动同步方案
java·人工智能·spring boot·后端·计算机视觉·ai·软件需求
爱笑的源码基地12 小时前
高并发 Redis 缓存门诊HIS系统源码,含财务统计药房进销存
java·程序·门诊系统·诊所系统·云诊所源码
段一凡-华北理工大学12 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
Ljwuhe12 小时前
C++——多态
开发语言·c++
心平气和量大福大13 小时前
C#-WPF-Window主窗体
开发语言·c#·wpf
莫逸风14 小时前
【AgentScope 2.0】 0. 学习指南
java·llm·agent·agentscope
从零开始的代码生活_14 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法