📜  计算在数字行上访问的不同点

📅  最后修改于: 2021-06-28 20:56:24             🧑  作者: Mango

给定一个人位于current_pos位置,并且二进制字符串路径表示该人所执行的移动,如果path [i] =’0′则该人向左移动了一步,如果path [i] =’1′则该人向左移动了向右走一步。任务是找到该人拜访的不同职位的数量。

例子:

方法:

  • 声明一个数组points []以存储该人经历的所有点。
  • 将此数组的第一个位置初始化为当前位置current_pos
  • 遍历字符串路径并执行以下操作:
    • 如果当前字符为‘0’ ,那么该人向左走。因此,将当前位置减1,然后将其存储在points []中
    • 如果当前字符为‘1’ ,则表示该人向右走。因此,将当前位置加1并将其存储在points []中
  • 计算中不同元素的总数[] 。有关对数组中的不同元素进行计数的不同方法,请参阅对数组中的不同元素进行计数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Utility function to return the number
// of distinct elements in an array
int countDistinct(int arr[], int len)
{
  
    set hs;
  
    for (int i = 0; i < len; i++) {
        // add all the elements to the HashSet
        hs.insert(arr[i]);
    }
  
    // Return the size of hashset as
    // it consists of all unique elements
    return hs.size();
}
  
// Function to return the count of
// positions the person went to
int getDistinctPoints(int current_pos, string path)
{
  
    // Length of path
    int len = path.length();
  
    // Array to store all the points traveled
    int points[len + 1];
  
    // The first point is the current_pos
    points[0] = current_pos;
  
    // For all the directions in path
    for (int i = 0; i < len; i++) {
  
        // Get whether the direction was left or right
        char ch = path[i];
  
        // If the direction is left
        if (ch == '0') {
  
            // Decrement the current position by 1
            current_pos--;
  
            // Store the current position in array
            points[i + 1] = current_pos;
        }
  
        // If the direction is right
        else {
  
            // Increment the current position by 1
            current_pos++;
  
            // Store the current position in array
            points[i + 1] = current_pos;
        }
    }
  
    return countDistinct(points, len + 1);
}
  
// Driver code
int main()
{
    int current_pos = 5;
    string path = "011101";
  
    cout << (getDistinctPoints(current_pos, path));
  
    return 0;
}
// contributed by Arnab Kundu


Java
// Java implementation of the approach
import java.util.*;
class GFG {
  
    // Function to return the count of
    // positions the person went to
    public static int getDistinctPoints(int current_pos, String path)
    {
  
        // Length of path
        int len = path.length();
  
        // Array to store all the points traveled
        int points[] = new int[len + 1];
  
        // The first point is the current_pos
        points[0] = current_pos;
  
        // For all the directions in path
        for (int i = 0; i < len; i++) {
  
            // Get whether the direction was left or right
            char ch = path.charAt(i);
  
            // If the direction is left
            if (ch == '0') {
  
                // Decrement the current position by 1
                current_pos--;
  
                // Store the current position in array
                points[i + 1] = current_pos;
            }
  
            // If the direction is right
            else {
  
                // Increment the current position by 1
                current_pos++;
  
                // Store the current position in array
                points[i + 1] = current_pos;
            }
        }
  
        return countDistinct(points, len + 1);
    }
  
    // Utility function to return the number
    // of distinct elements in an array
    public static int countDistinct(int arr[], int len)
    {
  
        HashSet hs = new HashSet();
  
        for (int i = 0; i < len; i++) {
            // add all the elements to the HashSet
            hs.add(arr[i]);
        }
  
        // Return the size of hashset as
        // it consists of all unique elements
        return hs.size();
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int current_pos = 5;
        String path = "011101";
  
        System.out.print(getDistinctPoints(current_pos, path));
    }
}


Python3
# Utility function to return the number
# of distinct elements in an array
def countDistinct(arr, Len):
  
    hs = dict()
  
    for i in range(Len):
          
        # add all the elements to the HashSet
        hs[arr[i]] = 1
  
    # Return the size of hashset as
    # it consists of all unique elements
    return len(hs)
  
# Function to return the count of
# positions the person went to
def getDistinctPoints(current_pos, path):
  
    # Length of path
    Len = len(path)
  
    # Array to store all the points traveled
    points = [0 for i in range(Len + 1)]
  
    # The first pois the current_pos
    points[0] = current_pos
  
    # For all the directions in path
    for i in range(Len):
  
        # Get whether the direction 
        # was left or right
        ch = path[i]
  
        # If the direction is left
        if (ch == '0'):
  
            # Decrement the current position by 1
            current_pos -= 1
  
            # Store the current position in array
            points[i + 1] = current_pos
  
        # If the direction is right
        else:
  
            # Increment the current position by 1
            current_pos += 1
  
            # Store the current position in array
            points[i + 1] = current_pos
          
    return countDistinct(points, Len + 1)
  
# Driver code
current_pos = 5
path = "011101"
  
print(getDistinctPoints(current_pos, path))
  
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Function to return the count of
    // positions the person went to
    public static int getDistinctPoints(int current_pos,
                                        string path)
    {
  
        // Length of path
        int len = path.Length;
  
        // Array to store all the points traveled
        int[] points = new int[len + 1];
  
        // The first point is the current_pos
        points[0] = current_pos;
  
        // For all the directions in path
        for (int i = 0; i < len; i++) {
  
            // Get whether the direction was left or right
            char ch = path[i];
  
            // If the direction is left
            if (ch == '0') {
  
                // Decrement the current position by 1
                current_pos--;
  
                // Store the current position in array
                points[i + 1] = current_pos;
            }
  
            // If the direction is right
            else {
  
                // Increment the current position by 1
                current_pos++;
  
                // Store the current position in array
                points[i + 1] = current_pos;
            }
        }
  
        return countDistinct(points, len + 1);
    }
  
    // Utility function to return the number
    // of distinct elements in an array
    public static int countDistinct(int[] arr, int len)
    {
  
        HashSet hs = new HashSet();
  
        for (int i = 0; i < len; i++) {
            // add all the elements to the HashSet
            hs.Add(arr[i]);
        }
  
        // Return the size of hashset as
        // it consists of all unique elements
        return hs.Count;
    }
  
    // Driver code
    public static void Main(string[] args)
    {
        int current_pos = 5;
        string path = "011101";
  
        Console.Write(getDistinctPoints(current_pos, path));
    }
}
  
// This code is contributed by shrikanth13


PHP


输出:
4