【问题标题】:how to print last element after deleting odd indices continuously连续删除奇数索引后如何打印最后一个元素
【发布时间】:2021-05-07 12:54:55
【问题描述】:

我正在尝试从 1 开始连续删除奇数位置并获取最后一个剩余元素;

例如:
n=6;

1 2 3 4 5 6

首先:删除奇数索引将得到 (2 4 6);

第二个:删除奇数索引将得到 (4),这就是答案...

这是我的代码:

import java.util.HashMap;

public class Odd_Deletions {

    public static void oddDeletions(HashMap<Integer, Integer> hm) {
        int j = 1;
        for (int i = 1; i < hm.size(); i++) {

            if (hm.get(i) % 2 != 0) {
                continue;
            } else {
                hm.put(j, i);
                j++;

            }

        }
        //System.out.println(hm);
        while (true) {
            if (hm.size() == 1) {
                System.out.println(hm);
                break;
            } else
                oddDeletions(hm);
        }
    }

    public static void main(String args[]) {
        int n = 6;
        HashMap<Integer, Integer> hm = new HashMap<>();

        for (int i = 1; i <= n; i++) {
            hm.put(i, i);
        }
        //System.out.println(hm);
        oddDeletions(hm);
    }
}

为什么我收到 StackOverflow 错误,这个逻辑有什么问题?

谁能解决?

感谢和问候;

【问题讨论】:

  • 试运行oddDeletions 方法。你有一个递归,但它什么时候停止?
  • 你永远不会从hm中删除任何东西。
  • 也许HashMap 不是在这里使用的正确类
  • 对于某个正整数 n,答案将始终是 2^n 形式的最大索引,因此如果您知道输入的大小,则可以在恒定时间内计算答案。

标签: java data-structures hashmap


【解决方案1】:

也许HashMap 不是在这里使用的正确类。无论如何,正如@Welbog 指出的那样,你永远不会从你的桌子上删除任何东西。还有,为什么要使用递归?

试试这样的:

    while (hm.size() > 1) {
        int j = 1;
        int last = hm.size();
        for (int i = 1; i <= last; i++) {
            int value = hm.remove(i);
            if (i % 2 == 0) {
                hm.put(j, value);
                j++;
            }
        }
    }

【讨论】:

    【解决方案2】:

    有 3 种可能的解决方案。

    (1) 我不知道您为什么需要使用该逻辑在HashMap 上应用该“删除过程”,无论如何可能的解决方案可能是以下解决方案。但仅当您需要申请 Map 时才使用它,由于某些原因,您需要通过在 Map 键上应用该逻辑来删除其条目。

    public class RemovingOddIndexes {
    
        public static void main(String[] args) {
    
            // initialize
            int n = 6;
            HashMap<Integer, Integer> hm = new HashMap<>();
            for (int i = 1; i <= n; i++) {
                hm.put(i, i);
            }
    
            //
            oddDeletions(hm);
    
            // print result
            hm.forEach((k, v) -> System.out.println(String.format("%s:%s, ", k, v)));
        }
    
        public static void oddDeletions(HashMap<Integer, Integer> hm) {
    
            while (hm.size() > 1) {
                hm.keySet()
                  .stream()
                  .sorted()
                  .forEach(new Consumer<Integer>() {
                      int i = 1;
    
                      @Override
                      public void accept(Integer n) {
    
                          if (i % 2 == 1)
                              hm.remove(n);
                          ++i;
                      }
                  });
            }
        }
    }
    

    (2) 否则使用简单的LinkedList,您可以递归浏览。我更喜欢使用LinkedList 而不是ArrayList,因为alg 需要在每次迭代中删除元素,直到最后一个。并且LinkedList 上的删除操作效果更好。

    public class RemovingOddIndexes {
    
        public static void main(String[] args) {
    
            // initialize
            int n = 6;
            List<Integer> list = new LinkedList<>();
            for (int i = 1; i <= n; i++) {
                list.add(i);
            }
    
            //
            oddDeletions(list);
    
            // print result
            list.forEach(i -> System.out.println(String.format("%s, ", i)));
        }
    
        public static void oddDeletions(List<Integer> list) {
    
            while (list.size() > 1) {
                int i = 1;
                Iterator<Integer> it = list.iterator();
                while (it.hasNext()) {
                    it.next();
                    if (i++ % 2 == 1) {
                        it.remove();
                    }
                }
            }
        }
    }
    

    (3) 最后一个选项,最快的方式

    int lastOdd = 1 << (int)(Math.log(n) / Math.log(2))
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2021-11-24
      • 2017-04-08
      • 1970-01-01
      • 2021-10-18
      • 2022-01-03
      • 2023-02-07
      • 1970-01-01
      • 2021-12-28
      相关资源
      最近更新 更多