20200927交通银行编程题目

交通银行编程题目

  1. 素数列表

    输入 [3,5,9,11]

    输出[3,5,11]

    package org.sjd.util;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * @Author LiPeng
     * @Email imleep@163.com
     * @Date 2020/9/28 17:31
     * @Version 1.0.0
     * @Description
     */
    public class Main2 {
        
        public static void main(String[] args) {
            Integer[] arr = {2,3,5,8,9,11};
            System.out.println(Arrays.toString(new Main2().filterArray(arr)));
        }
        
        
        public Integer[] filterArray(Integer[] nums) {
            List<Integer> list = new ArrayList<>();
            for (Integer n : nums) {
                boolean prime = isPrime(n);
                if (prime) {
                    list.add(n);
                }
            }
            Integer[] arr = new Integer[list.size()];
            for(int i = 0; i < list.size(); i++)
                arr[i] = list.get(i);
            return arr;
        }
        
        public boolean isPrime(Integer number) {
            if (number <= 1) {
                return false;
            }
            if (number == 2) return true;
            int sqrt = (int)(Math.sqrt(number) + 1);
            for (int i = 2; i <= sqrt; i++) {
                if (number % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }
    
    

    运行结果

    [2, 3, 5, 11]
    
    Process finished with exit code 0
    
  2. 判断有向图是否有环

    输入输出
    {(A->B),(B->C),(C-A)}1
    {(A->B),(B->C),(C-D)}0
    {(A->B),(B->C),()}-1
    {}-1
    ()-1
    {()}-1
    import java.util.*;
    
    /**
     * @Author LiPeng
     * @Email imleep@163.com
     * @Date 2020/9/28 10:55
     * @Version 1.0.0
     * @Description
     */
    public class Main {
        public static void main(String[] args) {
            System.out.println(new Main().find_circle("{(A->B),(B->C),(C->A)}")); // 1
            System.out.println(new Main().find_circle("{(A->B),(B->C),(C->D)}")); // 0
            System.out.println(new Main().find_circle("{(A->B),(B->C),(C-A)}")); // -1
            System.out.println(new Main().find_circle("{()}")); // -1
            System.out.println(new Main().find_circle("{}")); // -1
            System.out.println(new Main().find_circle("())")); // -1
        }
        
        private Integer hasCircle = 0;
        
        // 题目给出的函数
        public Integer find_circle(String mapStr) {
            try {
                dfs(this.parseMapStrToMap(mapStr));
            } catch (Exception e) {
                this.hasCircle = -1;
            }
            return this.hasCircle;
        }
        
        // 解析图字符串
        private Map<String, List<String>> parseMapStrToMap(String mapStr) {
            if ("()".equals(mapStr)) return new HashMap<>();
            Map<String, List<String>> graph = new HashMap<>();
            String[] split = mapStr.substring(1, mapStr.length() - 1).split(",");
            for (String str : split) {
                String substring = str.substring(1, str.length() - 1);
                String[] node_2_node = substring.split("->");
                String s = node_2_node[0];
                String e = node_2_node[1];
                if (graph.get(s) == null) {
                    graph.put(s, new ArrayList<>());
                }
                graph.get(s).add(e);
            }
            return graph;
        }
        
        // 深度优先遍历
        private void dfs(Map<String, List<String>> graph) {
            dfsVisited(graph, new LinkedHashMap<String, String>(), graph.keySet().toArray()[0].toString());
        }
        
        private void dfsVisited(Map<String, List<String>> graph, Map<String, String> visitedMap, String nodeName) {
            if (visitedMap.get(nodeName) == null) { // 节点未被访问
                visitedMap.put(nodeName, "visited");
                List<String> list = graph.get(nodeName);
                if (list != null) {
                    for (String node : list) {
                        dfsVisited(graph, visitedMap, node);
                    }
                }
            } else { // 节点被访问有环
                this.hasCircle = 1;
            }
        }
    }
    
    

    运行结果

    1
    0
    -1
    -1
    -1
    -1
    
    Process finished with exit code 0
    
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值