米饭吃饱饱 level
获赞
0
粉丝
1
关注
5
看过 TA
11
北京联合大学
2020
安卓
IP属地:北京
暂未填写个人简介
私信
关注
    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        int[] nums = Arrays.stream(in.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();        String[] c = in.nextLine().split(" ");        List cards = new ArrayList();        for (int i = 0; i < nums.length; i++) {            cards.add(new Card(nums[i], c[i].charAt(0)));        }        System.out.println(getResult(cards));    }    public static int getResult(List<Card> cards) {        Queue<Integer> path = new PriorityQueue<>(new Comparator<Integer>() {            @Override            public int compare(Integer o1, Integer o2) {                return o2 - o1;            }        });        for (int i = 0; i < cards.size(); i++) {            List<Card> temp = cards.stream().collect(Collectors.toList());            temp.remove(i);            DFS(temp, path,cards.get(i), 1);        }        if (path.size() > 0)            return path.poll();        return 0;    }    public static void DFS(List<Card> totol, Queue<Integer> path, Card cur, int depth) {        boolean has = false;        for (int i = 0; i < totol.size(); i++) {            Card temp = totol.get(i);            if (temp.num == cur.num || temp.color == cur.color) {                List<Card> copy = totol.stream().collect(Collectors.toList());                has = true;                copy.remove(i);                DFS(copy, path, totol.get(i), depth + 1);            }        }        if (!has) {            path.add(depth);        }    }    static class Card {        public int num;        public char color;        public Card(int num, char color) {            this.num = num;            this.color = color;        }        @Override        public boolean equals(Object obj) {            if (obj instanceof Card)                return false;            Card c = (Card) obj;            if (this.num == c.num && this.color == c.color)                return true;            return false;        }    }
查看2道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        int[] boll = Arrays.stream(in.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();        int[] order = Arrays.stream(in.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();        System.out.println(getResult(boll, order));    }    public static String getResult(int[] boll, int[] order) {        LinkedList<Integer> bucket = new LinkedList<>();        StringBuilder ans = new StringBuilder();        int j = 0;        for (int i = 0; i < boll.length; i++) {            bucket.add(boll[i]);            while (!bucket.isEmpty()) {                int left = bucket.peekFirst();                int right = bucket.peekLast();                if (left == order[j]) {                    bucket.pollFirst();                    ans.append("L");                    j++;                } else  if (right == order[j]) {                    bucket.pollLast();                    ans.append("R");                    j++;                } else                    break;            }        }        return ans.toString();    }
查看2道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        String[] namea = sc.nextLine().split(",");        String abbr = sc.nextLine();        System.out.println(getNames(namea, abbr));    }    public static List getNames(String[] nameall, String word) {        List<String> ans = new ArrayList<>();        for (int i = 0; i < nameall.length; i++) {            if (isSaftty(nameall[i], word))                ans.add(nameall[i]);        }        return ans;    }    public static boolean isSaftty(String name, String word) {        String[] names = name.split( " ");        int index = 0, right = 0, i = 1;        while (right + i < word.length()) {            if (dfs(names, word, index, right, right + i))                return true;            i++;        }        return false;    }    public static boolean dfs(String[] names, String word, int index,int left,int right) {        if (index > names.length - 1 && left > word.length())            return true;        if (index > names.length - 1 || right > word.length())            return false;        String s = word.substring(left, right);        if (names[index].startsWith(s)) {            if (index == names.length - 1 && right == word.length())                return true;            int i = 1;            while (right + i <= word.length()) {                if (dfs(names, word, index + 1, right, right + i))                    return true;                i++;            }            return false;        } else            return false;    }
查看2道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        List times = new ArrayList();        String[] s = in.nextLine().split("[,\\[\\]]");        String[] s1 = Arrays.copyOfRange(s, 1, s.length);        int[] t = Arrays.stream(s1).mapToInt(Integer::parseInt).toArray();        for (int i = 0; i < t.length; i++) {            times.add(t[i]);        }        System.out.println(getMaxTime(times));    }    public static String getMaxTime(List<Integer> times) {        StringBuilder stringBuilder = new StringBuilder();        int[][] maxTimeTable = {{2,0},{4,9},{5,0},{9,9},{5,0},{9,9}};        int pro = 0;        for (int i = 0; i < maxTimeTable.length; i++) {            int max = 0;            if (i == 1 && pro == 2) {                max = maxTimeTable[i][0];            } else if (i == 1) {                max = maxTimeTable[i][1];            } else                max = maxTimeTable[i][0];            while (!times.contains(max)) {                max--;            }            stringBuilder.append(max);            if (i == 1 || i == 3)                stringBuilder.append(":");            times.remove((Integer) max);            pro = max;        }        return stringBuilder.toString();    }
查看2道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
暴力做法    static int[][] DISTANCE = {{1,0},{-1,0},{0,1},{0,-1}};    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int H = scanner.nextInt();        int W = scanner.nextInt();        int[][] map = new int[H][W];        for (int i = 0; i < H; i++) {            for (int j = 0; j < W; j++) {                map[i][j] = scanner.nextInt();            }        }        System.out.println(getMaxFlow(map));    }    public static int getMaxFlow(int[][] map) {        if (map == null)            return 0;        boolean[][] visited = new boolean[map.length][map[0].length];        Path path = new Path("", Integer.MAX_VALUE);        List<Path> ans = new ArrayList<>();        DFS(map, visited, path,0,0, ans);        if (ans != null)            return ans.get(0).maxFlow;        else            return 0;    }    public static void DFS(int[][] map, boolean[][] visited, Path path, int i, int j, List<Path> ans) {        if (i < 0 || i >= map.length || j < 0 || j >= map[0].length)            return;        if (visited[i][j] == true)            return;        if (path.path.endsWith(map.length - 1 + "" + (map.length - 1) + "")) {            ans.add(new Path(path.path, path.maxFlow));            return;        }        visited[i][j] = true;        path = new Path(path.path + i + j, Math.min(path.maxFlow, map[i][j]));        for (int[] ints : DISTANCE) {            DFS(map, visited, path, i + ints[0], j + ints[1], ans);        }        visited[i][j] = false;    }    static class Path {        String path = null;        int maxFlow = 0;        public Path(String path, int maxFlow) {            this.path = path;            this.maxFlow = maxFlow;        }    }
查看2道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
    static int[][] DISTANCE = {{1,0}, {0,1}};    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        int[] mn = Arrays.stream(in.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();        int[][] map = new int[mn[0]][mn[1]];        for (int i = 0; i < mn[0]; i++) {            for (int j = 0; j < mn[1]; j++) {                map[i][j] = in.nextInt();            }        }        List pathList = new ArrayList<>();        String path = "";        dfs(map, path, pathList, 0, 0, mn);        System.out.println(pathList.size());    }    public static void dfs(int[][] map, String path, List pathList, int i, int j, int[] mn) {        if (path.endsWith("" + (mn[0] - 1) + "" + (mn[1] - 1)) && !pathList.contains(path)) {            pathList.add(path);            return;        }        if (i > map.length - 1 || i < 0 || j > map.length - 1 || j < 0)            return;        if (map[i][j] == 1)            return;        for (int k = 0; k < DISTANCE.length; k++) {            dfs(map, path + i + j, pathList, i + DISTANCE[k][0], j + DISTANCE[k][1], mn);        }    }
查看2道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int totalCities = scanner.nextInt();        // 读取城市之间的关系,假设是无向图,且n个城市形成n-1条边        int[][] cityRelations = new int[totalCities - 1][2];        for (int i = 0; i < totalCities - 1; i++) {            cityRelations[i][0] = scanner.nextInt();            cityRelations[i][1] = scanner.nextInt();        }        // 输出最小连通分量中包含的城市编号列表        System.out.println(findCitiesWithMinimumComponentSize(totalCities, cityRelations));    }        public static String findCitiesWithMinimumComponentSize(int totalCities, int[][] cityRelations) {        UnionFindSet[] unionFindSet = new UnionFindSet[cityRelations.length];        for (int i = 0; i < unionFindSet.length; i++) {            unionFindSet[i] = new UnionFindSet(totalCities);        }        for (int i = 0; i < cityRelations.length; i++) {            for (int j = 0; j < cityRelations.length; j++) {                if (cityRelations[j][0] - 1 != i && cityRelations[j][1] - 1 != i)                    unionFindSet[i].union(cityRelations[j][0] - 1, cityRelations[j][1] - 1);            }        }        List<Integer> ansList = new ArrayList<Integer>();        for (int i = 0; i < unionFindSet.length; i++) {            int[] parent = unionFindSet[i].parent;            int[] diff = new int[totalCities];            for (int i1 = 0; i1 < parent.length; i1++) {                diff[unionFindSet[i].find(parent[i1])] = 1;            }            int count = 0;            for (int j = 0; j < diff.length; j++) {                if (diff[j] == 1)                    count++;            }            ansList.add(count);        }        Collections.sort(ansList);        String ans = "";        for (int i = ansList.size() - 1; i >= 0; i--) {            if (ansList.get(i) == ansList.get(ansList.size() - 1))                ans += i + " ";            else break;        }        return ans.substring(0, ans.length() - 1);    }    // 并查集类保持不变,因为它已经足够清晰和高效    static class UnionFindSet {        int[] parent;        public UnionFindSet(int n) {            parent = new int[n];            for (int i = 0; i < n; i++) {                parent[i] = i;            }        }        public int find(int x) {            if (parent[x] != x) {                parent[x] = find(parent[x]); // 路径压缩            }            return parent[x];        }        public void union(int x, int y) {            int xRoot = find(x);            int yRoot = find(y);            if (xRoot != yRoot) {                parent[yRoot] = xRoot;            }        }    }
查看1道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
    static int[][] DISTANCE = {{1,0},{-1,0},{0,1},{0,-1}};    static String ans = null;    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        int num = Integer.parseInt(in.nextLine());        String[][] ch = new String[num][num];        for (int i = 0; i < num; i++) {            ch[i] = in.nextLine().split(",");        }        String word = in.nextLine();        System.out.println(word);        getResult(word, ch);    }    public static void getResult(String word, String[][] map) {        boolean[][] visited = new boolean[map.length][map.length];        StringBuilder out = null;        String path = "";        for (int i = 0; i < map.length; i++) {            for (int j = 0; j < map[i].length; j++) {                out = new StringBuilder();                dfs(word, visited, map, i, j, out, 0, path);            }        }        System.out.println(ans);    }    public static boolean dfs(String word, boolean[][] visited, String[][] map, int i, int j, StringBuilder out, int depth, String path) {        if (word.equals(path)) {            ans = out.delete(out.length() - 1, out.length()).toString();            return true;        }        if (i >= map.length || j >= map.length || i < 0 || j < 0)            return false;        if (visited[i][j] == true)            return false;        if (word.charAt(depth) != map[i][j].charAt(0))            return false;        visited[i][j] = true;        out.append(i + ",");        out.append(j + ",");        for (int k = 0; k < DISTANCE.length; k++) {            if(dfs(word, visited, map, i + DISTANCE[k][0], j + DISTANCE[k][1], out, depth + 1, path + map[i][j]))                return true;        }        out.delete(out.length() - 4, out.length());        visited[i][j] = false;        return false;    }
查看3道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        int num = in.nextInt();        System.out.println(getM(num));    }    public static int getM(int num) {        int ans = num;        LinkedList<Integer> one = new LinkedList<>();        LinkedList<Integer> zero = new LinkedList<>();        for (int i = 1; i <= num << 1; i = i << 1) {            if ((i & num) == 0) {                zero.add(i);            }            if ((i & num) == i) {                ans -= i;                one.add(i);            }            if (one.size() > 0 && (i & num) == 0) {                ans += i;                break;            }        }        for (int i = 1; i < 1 << one.size() - 1; i = i << 1) {            ans += i;        }        return ans;    }
查看3道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        String inputString = scanner.nextLine();        System.out.println(findLongestSubstringWithoutRepetition(inputString));    }    /**     * 计算输入字符串中不包含重复字符的最长子串的长度。     * 使用位运算和链表数组来跟踪每个状态(字符组合)下的最早出现位置。     *     * @param input 输入字符串     * @return 不包含重复字符的最长子串的长度     */    public static int findLongestSubstringWithoutRepetition(String input) {        int status = 0; // 使用位运算表示当前字符组合的状态        LinkedList<Integer>[] positionMap = new LinkedList[8]; // 映射每个状态到最早出现位置的链表        for (int i = 0; i < 8; i++) {            positionMap[i] = new LinkedList<>();        }        positionMap[0].add(-1); // 初始化空状态的位置为-1        int maxLength = 0; // 记录最长子串的长度        for (int i = 0; i < input.length() * 2; i++) {            char c = input.charAt(i % input.length()); // 循环输入字符串以处理重叠子串            switch (c) {                case 'l':                    status ^= 4; // 对应0b100                    break;                case 'o':                    status ^= 2; // 对应0b010                    break;                case 'x':                    status ^= 1; // 对应0b001                    break;            }            if (i < input.length()) {                positionMap[status].add(i); // 记录当前字符在当前状态下的位置            }            // 清理不再可能构成最长子串的状态位置            while (!positionMap[status].isEmpty()) {                int earliestPosition = positionMap[status].getFirst();                if (i - earliestPosition > input.length()) {                    positionMap[status].removeFirst();                } else {                    maxLength = Math.max(maxLength, i - earliestPosition);                    break;                }            }        }        return maxLength;    }
查看3道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        int i = 0;        String[] strs = new String[3];        while (in.hasNextLine()) {            strs[i] = in.nextLine();            i++;        }        int target = Integer.parseInt(strs[2]);        System.out.println(getResult(strs[0], strs[1], target));    }    public static int getResult(String str1, String str2, int target) {        int max = 0;        for (int i = 0; i < str1.length();) {            int temp = 0;            int sum = 0;            for (int j = i; j < str1.length(); j++) {                int a1 = str1.charAt(j);                int a2 = str2.charAt(j);                sum += Math.abs(a2 - a1);                if (sum <= target)                    temp++;                else {                    break;                }            }            max = Math.max(max, temp);            i += temp + 1;        }        return max;    }华为OD机试真题 - 字符串比较
查看3道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        String[] str = new String[2];        int i = 0;        while (in.hasNextLine()) {            str[i] = in.nextLine();            i++;        }        String[] litter = str[1].split(",");        String[] words = str[0].split(",");        System.out.println(getResult(litter, words));    }    public static String getResult(String[] litter, String[] words) {        LinkedHashMap<Character, ArrayList> hashMap = new LinkedHashMap<Character, ArrayList>();        // 如果里面放入map 可以模拟输入法        // LinkedHashMap<Character, map> hashMap = new LinkedHashMap<Character, map>();        StringBuilder stringBuilder = new StringBuilder();        for (int i = 0; i < litter.length; i++) {            if (hashMap.get(litter[i].charAt(0)) != null) {                hashMap.get(litter[i].charAt(0)).add(litter[i]);            } else {                ArrayList arrayList = new ArrayList();                arrayList.add(litter[i]);                hashMap.put(litter[i].charAt(0),arrayList);            }        }        for (int i = 0; i < words.length; i++) {            String temp = null;            ArrayList path = new ArrayList<>();            for (int j = 0; j < words[i].length();) {                List list = hashMap.get(words[i].charAt(j));                if (list == null) {                    if (temp != null) {                        System.out.println(stringBuilder.length() - temp.length() + "   " + stringBuilder.length());                        stringBuilder.delete(stringBuilder.length() - temp.length() - 1, stringBuilder.length());                        j -= temp.length();                        continue;                    }                    stringBuilder.append(words[i].charAt(j));                }                else {                    boolean has = false;                    for (int k = 0; k < list.size(); k++) {                        String str = (String) list.get(k);                        if (words[i].length() - j >= str.length()) {                            temp = words[i].substring(j, str.length() + j);                            if (temp.contains(str) && !path.contains(str)) {                                j += str.length();                                path.add(str);                                stringBuilder.append(str + ",");                                has = true;                                break;                            }                        }                    }                    if (!has) {                        if (temp != null) {                            System.out.println(stringBuilder.length() - temp.length() + "   " + stringBuilder.length());                            stringBuilder.delete(stringBuilder.length() - temp.length() - 1, stringBuilder.length());                            j -= temp.length();                        }                    }                }            }        }        stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());        return stringBuilder.toString();    }
查看3道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        int m = 0, n = 0;        String line = in.nextLine();        int[] mn = Arrays.stream(line.split(",")).mapToInt(Integer::parseInt).toArray();        m = mn[0];        n = mn[1];        int[][] map = new int[m][n];        int i = 0;        while (in.hasNextLine()) {            String line1 = in.nextLine();            map[i] = Arrays.stream(line1.split(",")).mapToInt(Integer::parseInt).toArray();            i++;        }        int space =  100;        int banance = space;        boolean[][] path = new boolean[m][n];        for (int j = 0; j < path.length; j++) {            Arrays.fill(path[j], false);        }        banance = dfs(map, banance, path, 0, 0);        System.out.println(space - banance);    }    public static int dfs(int[][] map, int banance, boolean[][] path, int i, int j) {        path[i][j] = true;        if (map[i][j] == -1)            banance = 100;        else {            banance = banance - map[i][j];        }        if (banance < 0)            return banance;        if (i == map.length - 1 && j == map[i].length - 1)            return banance;        int down = Integer.MIN_VALUE;        int left = Integer.MIN_VALUE;        int up = Integer.MIN_VALUE;        int right = Integer.MIN_VALUE;        if (banance < 0)            return banance;        if (i + 1 < map.length && path[i + 1][j] == false && map[i + 1][j] != 0) {            down = dfs(map, banance, path, i + 1, j);            path[i + 1][j] = false;            if (down < 0)                down = Integer.MIN_VALUE;        }        if (i - 1 >= 0 && path[i - 1][j] == false && map[i - 1][j] != 0) {            up = dfs(map, banance, path, i - 1, j);            path[i - 1][j] = false;            if (up < 0)                up = Integer.MIN_VALUE;        }        if (j + 1 < map[i].length && path[i][j + 1] == false && map[i][j + 1] != 0) {            right = dfs(map, banance, path, i, j + 1);            path[i][j + 1] = false;            if (right < 0)                right = Integer.MIN_VALUE;        }        if (j - 1 >= 0 && path[i][j - 1] == false && map[i][j - 1] != 0) {            left = dfs(map, banance, path, i, j - 1);            path[i][j - 1] = false;            if (left < 0)                left = Integer.MIN_VALUE;        }        banance = Math.max(Math.max(down,up), Math.max(left,right));        return banance;    }
查看3道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
/*** * 攀登者喜欢寻找各种地图,并且尝试攀登到最高的山峰。 * * 地图表示为一维数组,数组的索引代表水平位置,数组的元素代表相对海拔高度。其中数组元素0代表地面。 * * 例如:[0,1,2,4,3,1,0,0,1,2,3,1,2,1,0],代表如下图所示的地图,地图中有两个山脉位置分别为 1,2,3,4,5 和 8,9,10,11,12,13,最高峰高度分别为 4,3。最高峰位置分别为3,10。 * * 一个山脉可能有多座山峰(高度大于相邻位置的高度,或在地图边界且高度大于相邻的高度)。 * * 作者:华为OD机试题库 * 链接:https://www.nowcoder.com/discuss/637024593848320000?sourceSSR=users * 来源:牛客网 */public class Num001 {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int[] heights = Arrays.stream(scanner.nextLine().split(","))                .mapToInt(Integer::parseInt)                .toArray();        int strength = Integer.parseInt(scanner.nextLine());        int num = getNum(heights, strength);        System.out.println(num);    }    public static int getNum(int[] nums, int strength) {        int ans = 0;        for (int i = 0; i < nums.length; i++) {            if (isPeak(nums, i) && strength > nums[i] * 3) {                ans++;            }        }        return ans;    }    public static Boolean isPeak(int[] nums, int i) {        if (i + 1 < nums.length && i - 1 >= 0 && nums[i] > nums[i - 1] && nums[i] > nums[i + 1])            return true;        else return false;    }}
查看2道真题和解析 投递华为等公司10个岗位
0 点赞 评论 收藏
分享
https://www.nowcoder.com/discuss/635066067198509056?sourceSSR=users public static int dynamic(int[] days, int[] costs) {        int[] dp = new int[days[days.length - 1] + 1];        int index = 0;        for (int i = 1; i < dp.length && index < days.length; i++) {            if (days[index] == i) {                int min = Integer.MAX_VALUE;                int costT = Integer.MAX_VALUE;                int costW = Integer.MAX_VALUE;                int costM = Integer.MAX_VALUE;                int costD = dp[i - 1] + costs[0];                min = Math.min(min,costD);                if (i - 3 > 0)                    costT = dp[i - 3] + costs[1];                min = Math.min(min,costT);                if (i - 7 > 0)                    costW = dp[i - 7] + costs[2];                min = Math.min(min,costW);                if (i - 30 > 0)                    costM = dp[i - 30] + costs[3];                min = Math.min(min,costM);                dp[i] = min;                index++;            } else {                dp[i] = dp[i - 1];            }        }        return dp[days[days.length - 1]];    }
0 点赞 评论 收藏
分享
关注他的用户也关注了:
牛客网
牛客企业服务