在向量之间踢出重复的条目

时间:2015-06-19 19:56:17

标签: c++ unique

我有向量,我想检索一个包含所有输入向量中任何地方都没有重复的条目的向量。

#include <vector>
int main() {

  std::vector<int> a = {2, 1, 3};
  std::vector<int> b = {99, 1, 3, 5, 4};
  std::vector<int> c = {5, 6, 7, 1};

  // magic to retrieve {2, 99, 4, 6, 7} (order doesn't matter)

}

是否有可以帮助有效执行此任务的库函数?

我不喜欢使用矢量。解决方案可以包括列表,集合或任何最适合该任务的内容。

1 个答案:

答案 0 :(得分:1)

使用unordered_map,O(N)空间复杂度和O(N)时间复杂度:

public class TestAlgo{
private String move;

public static void main(String[] args){
    TestAlgo testAlgo = new TestAlgo();
    testAlgo.runGame();
}

public void runGame(){
    Cram game = new Cram();
    boolean start = game.startGame();


    while (start){
        ExecutorService executor = Executors.newSingleThreadExecutor();/////
        Future<String> future = executor.submit(new Task());/////
        move = "";
        try {
            System.out.println("Started..");
            move = future.get(10, TimeUnit.SECONDS);
            System.out.println("Finished!");
        } catch (TimeoutException e) {
            future.cancel(true);
            move = "Timeout";
            System.out.println("Terminated!");
        } catch (InterruptedException ie){
            System.out.println("Error: InterruptedException");
        } catch (ExecutionException ee){
            System.out.println("Error: ExecutionException");
        }
        System.out.println("Move: " + move);
        executor.shutdownNow();



        if (game.sendMove(move)) break;
        game.printBoard();
        if (game.getMove()) break;
        game.printBoard();
    }
}

// public static String algorithm(){
//     while (true){ //infinite loop
//         System.out.println("Algo is running...");
//     }
//     return "test";
// }

public static String algorithm(){
    Scanner userInputScanner = new Scanner(System.in);
    System.out.print("Please enter your move: ");
    String input = userInputScanner.nextLine();
    return input;
}}

class Task implements Callable<String> {
@Override
public String call() throws Exception {
    String move = TestAlgo.algorithm();
    return move;
}}