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

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");
    }
}
相关推荐
xyq20248 小时前
jEasyUI 创建简单的菜单
开发语言
张小洛8 小时前
Spring 常用类深度剖析(工具篇 04):CollectionUtils 与 Stream API 的对比与融合
java·后端·spring·spring工具类·spring utils·spring 类解析
l1t8 小时前
利用python statsmodels包分析数据
开发语言·python
小同志008 小时前
请求两个注解 @PathVariable + @RequestPart
开发语言·lua·请求注解
nnsix9 小时前
C# ProcessStartInfo对象笔记
开发语言·笔记·c#
Hello--_--World9 小时前
ES15:Object.groupBy() 和 Map.groupBy()、Promise.withResolvers() 相关知识点
开发语言·前端·javascript
一 乐9 小时前
房产租赁管理|基于springboot + vue房产租赁管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·房产租赁管理系统
黑牛儿9 小时前
PHP 8.3性能暴涨实测|对比8.2,接口响应提速30%,配置无需大幅修改
android·开发语言·后端·php
xieliyu.9 小时前
Java顺序表实现扑克牌Fisher-Yates 洗牌算法
java·数据结构·算法·javase
YanDDDeat9 小时前
【Spring】事务注解失效与传播机制
java·后端·spring