牛客刷题记录1

题1:坐标求和

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

class Solution {
    public long sumSelected(long[][] matrix, List<int[]> coords) {
        long total = 0;
        for (int[] opt : coords) {
            total += matrix[opt[0]][opt[1]];
        }
        return total;
    }
}


public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        if(!sc.hasNextInt())
            break;
        int m = sc.nextInt();
        if(!sc.hasNextInt())
            break;
        int n = sc.nextInt();

        long[][] matrix = new long[m][n];

        //注意这里不能用while  因为后面还有输入  直接进入死循环
        // while(sc.hasNextInt()) {
        //     for (int i = 0; i < m; i++) {
        //         for (int j = 0; j < n; j++) {
        //             matrix[i][j] = sc.nextLong();
        //         }
        //     }
        // }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = sc.nextLong();
            }
        }

        if(!sc.hasNextInt())
            break;
        int q = sc.nextInt();

        List<int[]> list = new ArrayList<>(q);
        while(sc.hasNextInt()) {
            for (int i = 0; i < q; i++) {
                int r = sc.nextInt();
                int c = sc.nextInt();

                list.add(new int[] {r, c});
            }
        }

        System.out.println(solution.sumSelected(matrix, list));
        
    }
    sc.close();
}

题2:坐标移动

正则表达式好用!

  • [abc] - 匹配 a、b 或 c 中的一个字母 测验
  • [a-z] - 匹配 a 到 z 中的一个字母 测验
  • [^abc] - 匹配除了 a、b 或 c 中的其他字母测验
  • {n} - 匹配 n测验
  • {n,} - 匹配 n 次以上 测验
  • {m ,n} - 最少 m 次,最多 n 次匹配 测验
java 复制代码
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        //输入处理,并对字符进行分割
        Scanner sc = new Scanner(System.in);
        String[] in = sc.nextLine().split(";");


        //设置坐标原始值
        int x = 0;
        int y = 0;

        //对分割之后的每个字符进行处理
        for (String s : in) {
            //对字符进行匹配,因为最多两位数,所以就最少匹配1次,最多匹配2次
            if (!s.matches("[WASD][0-9]{1,2}")) {
                continue;
            }
            //截取数字部分并转换为整数
            int step = Integer.valueOf(s.substring(1));
            //对第一位字符进行方向匹配,并做对应的移动
            switch (s.charAt(0)) {
                case 'W':
                    y += step;
                    break;
                case 'S':
                    y -= step;
                    break;
                case 'A':
                    x -= step;
                    break;
                case 'D':
                    x += step;
                    break;
            }
        }
        //输出结果
        System.out.println(x + "," + y);
        //关闭scanner
        sc.close();

        
    }
}
相关推荐
008爬虫实战录13 分钟前
【数据结构】 树、二叉树、完全二叉树,先序遍历、中序遍历、后序遍历
数据结构·算法
小O的算法实验室15 分钟前
2024年AST,基于费马点分组粒子群算法的复合型无人机统一路径规划
算法·无人机
AllData公司负责人30 分钟前
大模型赋能AllData数据中台,系列升级|通过联合智谱大模型与BiSheng开源项目,建设企业大模型应用开发平台,支持知识库向量检索!
大数据·数据结构·数据库·算法·大模型·向量数据库·智谱ai
一个王同学36 分钟前
从零到一 | CV转多模态大模型 | week12 | 整理 MiniLLaVA 工程与文档
人工智能·深度学习·算法·机器学习·计算机视觉
这料鬼有毒43 分钟前
二刷hot100-78.子集
算法·leetcode·职场和发展
ZHW_AI课题组1 小时前
使用DBSCAN算法对纽约市 Airbnb 房源数据集进行聚类分析
算法
蓦然回首却已人去楼空2 小时前
【分词:中文分词】BPE字节级分词算法实现汉字表达!
java·算法·中文分词
3DVisionary2 小时前
aero-engine-blade-thermal-fatigue-dic-inspection
人工智能·算法·机器学习·航空发动机·高温dic·涡轮叶片·热疲劳
Kurisu5752 小时前
深度拆解:从二进制切片到并发控制,大文件断点续传的底层工程设计
算法