RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-458864

had0uken's questions

Martin Hope
had0uken
Asked: 2023-01-06 04:10:26 +0000 UTC

在 Spring 中正确组织 MVC 模式

  • 4

下面是关于 MVC 模式的问题。我正在写一个教育应用程序。创建的实体(问题)。之后我添加了 DAO 和服务层。螺丝弹簧安全。一切都很好,一切正常。决定进一步扩大应用范围。我添加了一个新实体(用户),然后并不完全清楚如何根据此模式正确构建所有内容。每个实体应该有一个单独的 DAO 接口吗?每个实体应该有一个单独的服务接口吗?我是否需要在控制器中为每个服务创建一个单独的 Bean?或者应该只有一个服务并与所有 DAO 一起工作?项目结构如下: 在此处输入图像描述

更新。我决定补充一点,从逻辑上讲,实体用户和问题没有任何联系。它们之间没有 FK,在应用程序中它们实际上不应相交。

java
  • 1 个回答
  • 37 Views
Martin Hope
had0uken
Asked: 2022-07-31 22:41:17 +0000 UTC

关于算法速度的问题

  • 1

有一个任务:用指定的方法创建一个类:

class NumArray {
  
    public NumArray(int[] nums) {
    //инициализация объекта с массивом интов
    }

    public void update(int index, int val) {
    // изменить значение под индексом index на значение val
    }

    public int sumRange(int left, int right) {
    //посчитать сумму между левым и правым индексом включительно
    }
}

这似乎没有什么困难,但我以最明显的方式实现了它:

class NumArray {
    int[] nums;
    public NumArray(int[] nums) {
        this.nums=nums;
    }

    public void update(int index, int val) {
        nums[index]=val;
    }

    public int sumRange(int left, int right) {
        int sum=0;
        for(int i=left;i<=right;i++)
            sum+=nums[i];
        return sum;
    }
}

该课程通过了 13/15 次检查,并且在 14 日当我收到大数组作为输入并且左右之间存在很大差异时,我没有通过检查 - 我不符合时间限制。我窥视了答案-人们组织了二叉树和其他“困难”。我不明白为什么?事实上,我的表格中的方法是尽可能快的吗?(更新是 O(1) - 不能更快地完成)和(sumRange 是 O(n) - 也不能更快地完成)。我哪里错了?提前致谢

java массивы
  • 1 个回答
  • 81 Views
Martin Hope
had0uken
Asked: 2022-07-21 17:42:22 +0000 UTC

图广度遍历

  • 0

解决任务时遇到问题: https ://leetcode.com/problems/find-if-path-exists-in-graph/

有一个具有 n 个顶点的双向图,其中每个顶点标记为从 0 到 n - 1(包括)。图中的边表示为一个二维整数数组边,其中每个边[i] = [ui, vi] 表示顶点 ui 和顶点 vi 之间的双向边。每个顶点对最多由一条边连接,并且没有顶点与自身有边。

您想确定是否存在从顶点源到顶点目的地的有效路径。

给定边和整数 n、源和目标,如果存在从源到目标的有效路径,则返回 true,否则返回 false。

以防万一翻译:

有一个具有 n 个顶点的双向图,其中每个顶点的编号从 0 到 n - 1(含)。图中的边由二维整数数组的边表示,其中每条边 [i] = [ui, vi] 表示顶点 ui 和顶点 vi 之间的双向边。每对顶点最多由一条边连接,没有一个顶点与自己有边。

您想确定是否存在从顶点源到顶点目的地的有效路径。

给定边和整数 n、源和目标,如果存在从源到目标的有效路径,则返回 true,否则返回 false。

例子

在此处输入图像描述

输入:n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2 输出:true 解释:从顶点 0 到顶点 2 有两条路径:

  • 0 → 1 → 2
  • 0 → 2

编写通过 19/26 检查的代码。20号初查数据,收到2000个节点,没有做决定,原因是:Time Limit Exceeded

请告诉我如何改进代码以适应限制。谢谢你。实际上代码本身:

public class Find_if_Path_Exists_in_Graph_1971 {
    public static void main(String[] args) {
        int n=50;
        int edges[][]={{18,46},{8,48},{13,30},{28,29},{2,16},{7,36},{12,19},{31,16},{11,46},{6,46},{19,27},{4,24},{10,37},{14,37},{39,31},{10,22},{23,2},{47,11},{40,7},{21,17},{9,3},{34,10},{48,1},{21,35},{43,48},{27,5},{36,11},{43,36},{31,48},{25,33},{46,19},{31,30},{16,45},{30,10},{35,47},{35,13},{37,48},{49,3},{7,26},{2,30},{0,27},{25,9},{28,27},{39,18},{32,6},{14,43},{9,27},{27,4},{6,0},{21,43}};
        int source =48;
        int destination =2;
        System.out.println(validPath(n,edges,source,destination));

    }
    public static boolean validPath(int n, int[][] edges, int source, int destination){
        boolean result =false;
        if(source==destination)return true;
        //Создаем матрицу смежности:

        int[][] matrix = new int[n][n];

        for(int i=0;i< edges.length;i++)
        {
            matrix[edges[i][0]][edges[i][1]]=1;
            matrix[edges[i][1]][edges[i][0]]=1;
        }

        //print(matrix);
        //создаем очередь и для обхода в ширину и лист для хранения уже проверенных значений.
        Queue<Integer>queue=new LinkedList<>();
        List<Integer> checked = new ArrayList<>();
        queue.add(source);

        while (!queue.isEmpty()){
            int num = queue.remove();
            if(num==destination)return true;
            else checked.add(num);
            for(int i=0;i<matrix.length;i++)
                if((matrix[num][i]==1)&(!checked.contains(i))) {
          //          System.out.println(i);
                    queue.add(i);
                }
        }

        return result;
    }

    public static void print(int [][] a) {
        for(int i=0;i< a.length;i++) {
            for (int j = 0; j < a[i].length; j++)
                System.out.print(a[i][j] + " ");
            System.out.println();
        }
    }
}

输出(带有邻接矩阵)

0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
true
алгоритм java
  • 1 个回答
  • 94 Views
Martin Hope
had0uken
Asked: 2022-07-21 03:22:42 +0000 UTC

请告诉我 AccessDeniedException 的原因

  • 0

你好!我正在尝试使用该方法Files.readAllLines,但出现异常AccessDeniedException

public class Test {
    public static void main(String[] args) throws IOException {

        Path directory = Paths.get("D:\\it\\A");

        boolean isDirectory = Files.isDirectory(directory);
        boolean isWritable = Files.isWritable(directory);
        System.out.println(isDirectory);
        System.out.println(isWritable);
        List<String>list = new ArrayList<>();
        list=Files.readAllLines(directory);

    }
}

安慰:

true
true
Exception in thread "main" java.nio.file.AccessDeniedException: D:\it\A
    at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:89)
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
    at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:236)
    at java.base/java.nio.file.Files.newByteChannel(Files.java:380)
    at java.base/java.nio.file.Files.newByteChannel(Files.java:432)
    at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:422)
    at java.base/java.nio.file.Files.newInputStream(Files.java:160)
    at java.base/java.nio.file.Files.newBufferedReader(Files.java:2923)
    at java.base/java.nio.file.Files.readAllLines(Files.java:3413)
    at java.base/java.nio.file.Files.readAllLines(Files.java:3454)
    at Test.main(Test.java:20)

Process finished with exit code 1

我试图在不同的本地驱动器上创建一个文件夹,该文件夹确实存在于该地址,该文件夹和所有子文件夹的“只读”模式均已关闭。防病毒被禁用,另一个应用程序不使用该文件夹。我搜索了互联网,但没有找到答案。操作系统 Windows 10 PRO,开发环境:

IntelliJ IDEA 2022.1(社区版)Build #IC-221.5080.210,2022年4月12日构建运行时版本:11.0.14.1+1-b2043.25 amd64

我知道这可能是一个愚蠢的错误,但我找不到答案。在此先感谢您的帮助!

InputStream:

public class Test {
    public static void main(String[] args) throws IOException {
        String path = "D:\\it\\A\\A2\\TEXT1.txt";

        InputStream is = new FileInputStream(path);
        int a=0;
        while ((a=is.read())!=-1) {
           System.out.println((char)a);
        }
        is.close();
 }
}

也有效Files.readAllBytes:

 public class Test {
        public static void main(String[] args) throws IOException {
            String path = "D:\\it\\A\\A2\\TEXT1.txt";
    
            String content = new String(Files.readAllBytes(Paths.get(path)));
            System.out.println(content);
     }

}

UPD。检查通过cmd 此文件夹设置的读写权限。

java windows
  • 1 个回答
  • 132 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5