【问题标题】:Returning true if you can split a list into smaller list with minimum two values in the parted list else return false如果您可以将一个列表拆分为较小的列表,则返回 true,在分区列表中至少包含两个值,否则返回 false
【发布时间】:2021-03-28 07:09:41
【问题描述】:

在一副纸牌中,每张纸牌上都写有一个整数。

当且仅当可以将整个牌组分成 1 组或多组卡片时才返回 true,其中:

每组至少有 2 张卡片。 每组中的所有卡片都具有相同的整数。

示例:

输入:deck = [1,2,3,4,4,3,2,1]

输出:真

说明:可能的分区[1,1],[2,2],[3,3],[4,4]

我尝试使用堆栈解决此问题,但它没有按预期工作,并且在应该打印为 true 的地方打印 false。为了清楚我的代码,我添加了注释。

打包练习pkg;

import java.util.Stack;

public class DeckInteger {

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<Integer>();
        //declaring individual variable for each integer and setting all of them to zero
        int p_1=0,p_2=0,p_3=0,p_4=0,p_5=0,p_6=0,p_7=0,p_8=0,p_9=0;
        
        //adding element which can form pair and should return true
        stack.add(1);
        stack.add(2);
        stack.add(1);
        stack.add(2);
        stack.add(1);
        stack.add(2);
        
        
        //checking till stack is empty and comparing the top of stack to all the individual integer and incrementing them and popping the top element 
        while(stack.empty()==false) {
            if(stack.peek()==1) {
                p_1+=1;
                stack.pop();                        
                break;
            } else if(stack.peek()==2) {
                p_2+=1;
                stack.pop();                        
                break;
            } else if(stack.peek()==3) {
                stack.pop();
                p_3+=1;
                break;
            } else if(stack.peek()==4) {
                stack.pop();
                p_4+=1;
                break;
            } else if(stack.peek()==5) {
                stack.pop();
                p_5+=1;
                break;
            } else if(stack.peek()==6) {
                stack.pop();
                p_6+=1;
                break;
            } else if(stack.peek()==7) {
                stack.pop();
                p_7+=1;
                break;
            } else if(stack.peek()==8) {
                stack.pop();
                p_8+=1;
                break;
            } else {
                stack.pop();
                p_9+=1;
                break;
            }
        }
        //checking if any of the integer variable have the value 1 then it cannot form pair as x>=2 so it should print false
        if(p_1==1||p_2==1||p_3==1||p_4==1||p_5==1||p_6==1||p_7==1||p_8==1||p_9==1) {
            System.out.println("False");
        }
        //print true as if any of the variable have a value other than one
        else {
            System.out.println("True");
        }
    }    
}

【问题讨论】:

    标签: java algorithm debugging stack logic


    【解决方案1】:

    如果你有a_i的卡片,整数为i,那么所有可能的分区都是a_i的除数,否则,这张卡片不能被拆分。因此,您只需要计算所有唯一数字并计算它们的最大公约数。这个最大公约数将是最大可能的 X。

    【讨论】:

    • 嘿,你能告诉我为什么我的代码不起作用吗?
    • 您的代码不起作用,因为您的代码中有一个中断,它中断了 while 循环,并且您只计算输入中的最后一个数字,而不是全部。假设它在从开关盒转换后离开。无论如何,这并不重要,因为修复后它仍然是不正确的。例如,如果您有数字 [2, 2, 3, 3, 3] 作为输入,您的代码(修复后)将返回 true,而此类输入没有分区。
    • 如果我删除 break ,我们可以创建 [2,2] 和 [3,3,3] 的分区,顺便说一句,我只需要检查变量的值不是 1。
    • 看在上帝的份上,非常感谢你。它解决了我的问题。你是最棒的。 :)
    【解决方案2】:

    在代码中,您在每个 if 条件中使用 break。所以它会在第一次迭代本身中脱离while 循环。所以只有p_1 等于1,而所有其他都等于0。 有一个更好的方法来解决这个问题,使用Maps 将键作为整数,将值作为计数

    假设输入为数组:int[] input

    private boolean isValidGroup(Stack<Integer> stack){
        Map<Integer, Integer> countMap = new HashMap<>();   
        while(!stack.empty()){
            countMap.merge(stack.pop(),1,Integer::sum);
        }
        // The above map would contain the count for each integer.
        // For input : {1,2,4,2,4,3,1,3} , the map would be {1:2,2:2,3:2,4:2}
        
        List<Integer> distinctCounts = countMap.values().stream().distinct().collect(Collectors.toList());
        // The above list contains the distinct group counts.
    
        // Ideally the number of distinct count value should be 1 and the value should be >=2.
        //In such case, we return true.
        return (distinctCounts.size()==1 && distinctCounts.get(0)>=2);
    }
    

    【讨论】:

    • 嘿,谢谢您的解决方案。尽管我被要求仅使用堆栈来解决它,但它看起来更加优雅。
    • 即使在这种情况下,Map 也可以用来代替 10 个不同的整数变量。同样使用地图,您可以在 while 循环内使用单个 if 条件
    • @BiggestNoob,我已经使用Stack更新了答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2020-03-28
    相关资源
    最近更新 更多