java - Google Foo.Bar Challenge Help Minion Salutes

标签 java

我正在应对 foo.bar 挑战并陷入困境。这是问题的描述:

> 途中敬礼

Lambda 指挥官热爱效率,讨厌任何浪费时间的事情。毕竟,她是一只忙碌的羔羊!她慷慨地奖励发现效率低下根源并想出办法消除这些根源的追随者。您已经发现了一个这样的来源,并且您认为解决它会帮助您建立晋升所需的声誉。

每次指挥官的员工在大厅里经过彼此时,他们每个人都必须停下来互相敬礼 - 一次一个 - 然后再继续他们的路径。一次敬礼是五秒钟,所以每次交换敬礼需要整整十秒钟( lambda 指挥官的敬礼有点,呃,复杂)。你认为通过取消敬礼要求,你每天可以节省几个小时的员工时间。但首先,您需要向她展示问题到底有多严重。

编写一个程序,计算在典型的走廊行走过程中交换了多少次敬礼。大厅用字符串表示。例如: “--->-><-><-->-”

每个走廊字符串将包含三种不同类型的字符:'>',一个向右走的员工; '<',一名员工向左走;和'-',一个空格。每个员工都按照自己的方向以相同的速度向右或向左行走。每当两名员工过马路时,他们都互相敬礼。然后他们继续走到尽头,最后离开走廊。在上面的例子中,他们敬礼 10 次。

编写一个函数 answer(s),它接受一个表示员工沿着走廊行走的字符串,并返回员工敬礼的次数。 s 将包含至少 1 个至多 100 个字符,每个字符都是 -、> 或 <。

示例输出:

> 测试用例

输入: (字符串)s = ">----<" 输出: (整数)2

输入: (字符串)s = "<<>>><" 输出: (整数)4

到目前为止我的代码:

public class Minion_SalutesV1 {
public static void main(String[] args){
    //Test input
    System.out.println(answer("<-->-<--<>-<<-->--<-->"));
    System.out.println(answer("<<><>><>><<><>>>"));
    System.out.println(answer(">----<"));
    System.out.println(answer("<<>><"));
}
public static int answer(String s) {
    //Return a string starting at the first occurrence of '>' and end at the last occurrence of '<+1' 
    String empBound = s.substring(s.indexOf('>'), s.lastIndexOf('<')+1).replaceAll("-", "");
    //Isolate the number of employees walking right
    String rightSaluters = empBound.replaceAll("<", "");
    //Isolate the number of employees walking left
    String leftSaluters = empBound.replaceAll(">", "");
    if(empBound.length() == 2){
        return 2;
    }
    else if (empBound.length() == 3){
        return 4;
    }
    else
    return rightSaluters.length() * leftSaluters.length();
    }
}

我一直在尝试很多不同的事情,但还没有取得正确的结果。

我认为返回 rightSaluters.length() * leftSaluters.length() 会给我想要的结果。我觉得通过隔离左右敬礼者我的想法是正确的,但超出这一点我就迷路了。

如何正确计算敬礼者之间的敬礼次数?

我是否应该为将要发生的相遇次数创建一个变量,然后将该数字乘以 2?我意识到我在这里吐口水,但我已经为此绞尽脑汁好几天了,一直等到最后一刻才寻求帮助。

更新:尝试实现 Jason 的回答

public class Minion_SalutesV1 {
    public static void main(String[] args){
        //Test input
        System.out.println(answer("<-->-<--<>-<<-->--<-->"));
        System.out.println(answer("<<><>><>><<><>>>"));
        System.out.println(answer(">----<"));
        System.out.println(answer("<<>><"));
    }
    public static int answer(String s) {
        //Return a string starting at the first occurrence of '>' and end at the last occurrence of '<+1'
        String empBound = s.substring(s.indexOf('>'), s.lastIndexOf('<')+1).replaceAll("-", "");
        //Isolate the number of employees walking right
        String rightSaluters = empBound.replaceAll("<", "");
        //Isolate the number of employees walking left
        String leftSaluters = empBound.replaceAll(">", "");
        int saluters = rightSaluters.length()+leftSaluters.length();
        System.out.println(saluters);

        for(char c = '>'; c < rightSaluters.length(); c++){
            int count = leftSaluters.length();
            saluters = count*2;
        }
        return saluters * 2;
    }
}

输出:

8  //remove this
16
11 //remove this
22
2  //remove this
4
3  //remove this
6

看来为每个字符串返回的最终整数是我需要的整数如何消除其他返回值?

最佳答案

关键是要认识到,小兵只会向在他们面前并走向他们的其他小兵敬礼。

计算敬礼次数的算法是:

for each '>'
    count the '<' to its right
    add count to the running total
end for

output the running total * 2 (because 2 salutes per meeting)

编辑:这是实现我的算法的一种方法(不是最高效的代码,而是快速而粗略的第一次尝试):

public static void main(String[] args) {
    //Test input
    System.out.println(answer("<-->-<--<>-<<-->--<-->"));
    System.out.println(answer("<<><>><>><<><>>>"));
    System.out.println(answer(">----<"));
    System.out.println(answer("<<>><"));
}

public static int answer(String s) {

    int meetings = 0;

    for (int index = 0; index < s.length() - 1; index++) {    // stop one short of the end, since if the last minion is heading right, there won't be anyone in from of him
        if (s.charAt(index) == '>') {
            meetings += countApproachingMinions(s.substring(index + 1));
        }
    }

    return meetings * 2;
}

private static int countApproachingMinions(String hallwayAhead) {
    return hallwayAhead.replaceAll(">", "")    // ignore minions heading the same way
            .replaceAll("-", "")               // ignore spaces
            .length();                         // count the approaching minions
}

关于java - Google Foo.Bar Challenge Help Minion Salutes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42378334/

相关文章:

java - 如果 Swing View 是由 Controller 中的新线程设置的,那么它是否需要同步方法

java.lang.ClassNotFoundException : org. glassfish.jersey.client.JerseyClientBuilder

java - .contains 的对面(不包含)

java - 在 Mac OS X Snow Leopard 上为 Glassfish 3 构建 Java EE 应用程序

java - 与 mySQL RDS 实例的 JDBC 连接

java - 单个 Socket 的多个 InputReader

java - 从 Java 管理动态 JavaScript 的最佳实践是什么?

java - 将接缝预订示例与 jboss 链接时出错

java - SQL 根据条件将变量字符串附加到 WHERE 子句

java - JNI加载类问题