📌  相关文章
📜  N 个平行轴的矩形中缺少顶点

📅  最后修改于: 2021-10-23 08:25:06             🧑  作者: Mango

给定二维笛卡尔坐标系中的N 个轴平行矩形和4N-1个顶点的坐标,任务是找到缺失的单个顶点。例子:

方法:
请按照以下步骤解决问题:

  • 将频率x 坐标y 坐标存储在 Map 中。
  • 遍历 Map 以找到两个坐标具有奇数频率的元素。
  • 最后,打印奇数频率的 x 和 y 坐标。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
void MissingPoint(vector > V,
                  int N)
{
    map mp;
    for (int i = 0; i < V.size(); i++) {
        mp[V[i].first]++;
    }
 
    int x, y;
    for (auto it : mp) {
        if (it.second % 2 == 1) {
            x = it.first;
            break;
        }
    }
    mp.clear();
    for (int i = 0; i < V.size(); i++) {
        mp[V[i].second]++;
    }
 
    for (auto it : mp) {
        if (it.second % 2 == 1) {
            y = it.first;
            break;
        }
    }
 
    cout << x << " " << y;
}
 
// Driver Code
int main()
{
 
    // Number of rectangles
    int N = 2;
 
    // Stores the coordinates
    vector > V;
 
    // Insert the coordinates
    V.push_back({ 1, 1 });
    V.push_back({ 1, 2 });
    V.push_back({ 4, 6 });
    V.push_back({ 2, 1 });
    V.push_back({ 9, 6 });
    V.push_back({ 9, 3 });
    V.push_back({ 4, 3 });
 
    MissingPoint(V, N);
 
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
    static class pair
    {
        int first, second;
        public pair(int first, int second) 
        {
            this.first = first;
            this.second = second;
        }   
    }
static void MissingPoint(Vector V, int N)
{
    HashMap mp = new HashMap();
    for (int i = 0; i < V.size(); i++)
    {
        if(mp.containsKey(V.get(i).first))
            mp.put(V.get(i).first,
                   V.get(i).first + 1);
        else
            mp.put(V.get(i).first, 1);
    }
    int x = 0, y = 0;
    for (Map.Entry it : mp.entrySet())
    {
        if (it.getValue() % 2 == 1)
        {
            x = it.getKey();
            break;
        }
    }
    mp.clear();
    for (int i = 0; i < V.size(); i++)
    {
        if(mp.containsKey(V.get(i).second))
            mp.put(V.get(i).second,
                   V.get(i).second + 1);
        else
            mp.put(V.get(i).second, 1);
    }
    for (Map.Entry it : mp.entrySet())
    {
        if (it.getValue() % 2 == 1)
        {
            y = it.getKey();
            break;
        }
    }
    System.out.print(x + " " + y);
}
 
// Driver Code
public static void main(String[] args)
{
    // Number of rectangles
    int N = 2;
 
    // Stores the coordinates
    Vector V = new Vector();
 
    // Insert the coordinates
    V.add(new pair(1, 1));
    V.add(new pair(1, 2));
    V.add(new pair(4, 6));
    V.add(new pair(2, 1));
    V.add(new pair(9, 6));
    V.add(new pair(9, 3));
    V.add(new pair(4, 3));
 
    MissingPoint(V, N);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
from collections import defaultdict
 
def MissingPoint(V, N):
 
    mp = defaultdict(lambda : 0)
    for i in range(len(V)):
        mp[V[i][0]] += 1
 
    for it in mp.keys():
        if(mp[it] % 2 == 1):
            x = it
            break
 
    del mp
    mp = defaultdict(lambda : 0)
 
    for i in range(len(V)):
        mp[V[i][1]] += 1
 
    for it in mp.keys():
        if(mp[it] % 2 == 1):
            y = it
            break
 
    print(x, y)
 
# Driver code
if __name__ == '__main__':
 
    # Number of rectangles
    N = 2
 
    # Stores the coordinates
    V = []
 
    # Insert the coordinates
    V.append([1, 1])
    V.append([1, 2])
    V.append([4, 6])
    V.append([2, 1])
    V.append([9, 6])
    V.append([9, 3])
    V.append([4, 3])
 
    MissingPoint(V, N)
 
# This code is contributed by Shivam Singh


C#
// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
   
class pair
{
  public int first, second;
  public pair(int first, int second) 
  {
    this.first = first;
    this.second = second;
  }   
}
   
static void MissingPoint(List V,
                         int N)
{
  Dictionary mp = new Dictionary();
  for (int i = 0; i < V.Count; i++)
  {
    if(mp.ContainsKey(V[i].first))
      mp[V[i].first] = mp[V[i].first] + 1;
    else
      mp.Add(V[i].first, 1);
  }
   
  int x = 0, y = 0;
  foreach (KeyValuePair it in mp)
  {
    if (it.Value % 2 == 1)
    {
      x = it.Key;
      break;
    }
  }
  mp.Clear();
   
  for (int i = 0; i < V.Count; i++)
  {
    if(mp.ContainsKey(V[i].second))
      mp[V[i].second] = mp[V[i].second] + 1;
    else
      mp.Add(V[i].second, 1);
  }
   
  foreach (KeyValuePair it in mp)
  {
    if (it.Value % 2 == 1)
    {
      y = it.Key;
      break;
    }
  }
  Console.Write(x + " " + y);
}
 
// Driver Code
public static void Main(String[] args)
{
  // Number of rectangles
  int N = 2;
 
  // Stores the coordinates
  List V = new List();
 
  // Insert the coordinates
  V.Add(new pair(1, 1));
  V.Add(new pair(1, 2));
  V.Add(new pair(4, 6));
  V.Add(new pair(2, 1));
  V.Add(new pair(9, 6));
  V.Add(new pair(9, 3));
  V.Add(new pair(4, 3));
 
  MissingPoint(V, N);
}
}
 
// This code is contributed by Rajput-Ji


输出:
2 2




时间复杂度: O(N)
辅助空间: O(N)