java 从最后一个数组中删除三重数组

bvhaajcl  于 2023-02-07  发布在  Java
关注(0)|答案(5)|浏览(187)

给定一个nums的整数数组,从last中删除三元组。
元素的相对顺序应保持相同。
实施例1
输入:数值=[2,4,2,2,7,5,6,7,8,6,6,2,6,7,6]
输出:数值=[2,4,5,6,8,6]
示例2输入:数值=[2,2,3,2,3,2]
输出:数值=[2,3,3]
我在java里有这个

int[] nums = {2,4,2,2,7,5,6,7,8,6,6,2,6,7,6};
int[] ans = new int[6];
int count=1; 
for(int i=0;i<nums.length;i++){
   for(int j=0;j<nums.length;j++){
     if(arr[i] == arr[j]){
        if(count < 3){
         count++;
         ans[i] = nums[i];
        }
     }
   }
}
zengzsys

zengzsys1#

我不能放弃这个,所以我用c#编写了一个例子,这是一种方法,可能有很多方法可以做得更好:

private static int[] RemoveLastTriplet(int[] input)
{
    var toRemove = new bool[input.Length];
    var counts = new Dictionary<int, int>();
    
    //Count how many times each input value is found.
    foreach(var value in input)
    {
        int count;
        
        if (counts.TryGetValue(value, out count))
        {
            counts[value] = count + 1;
        }
        else
        {
            counts[value] = 1;
        }
    }
    
    foreach(var kvp in counts)
    {
        //Determine how many triplets we have for this value
        var tripletCount = kvp.Value / 3;
        
        //Keep track of where we're starting
        var currentIndex = input.Length - 1;
        
        //Remove each triplet
        for(var tripletIndex = 0; tripletIndex < tripletCount; tripletIndex++)
        {
            //counts the number of elements in this triplet
            var thisTripletCount = 0;
            
            //Mark each member of the triplet for deletion.
            for(var inputIndex = currentIndex; thisTripletCount < 3; inputIndex--)
            {
                if (input[inputIndex] == kvp.Key)
                {
                    //Mark this index for removal
                    toRemove[inputIndex] = true;
                    thisTripletCount++;
                }

                //Keep track of where we are in the overall input array
                currentIndex--;
            }
        }
    }
    
    //We could be more clever here and keep track of how many
    // items we'll have in the output list and just create an array.
    var output = new List<int>();
    
    for(int index = 0; index < input.Length; index++)
    {
        if (!toRemove[index])
        {
            output.Add(input[index]);
        }
    }
    
    return output.ToArray();
}
lokaqttq

lokaqttq2#

这是我对这个问题的n^2解法,希望对你有帮助,先求出元素的个数,然后求出要删除的元素的总数,假设后面是t,把从末尾算起的t个元素替换成-1。

#include <iostream>
#include <unordered_set>

using namespace std;

int main() {
  int n;
  cout << "Enter the length of array : ";
  cin >> n;
  int arr[n];
  cout << "Enter items : ";
  for (int i = 0; i < n; ++i)
    cin >> arr[i];

  //   set for fast lookups
  unordered_set<int> uset;

  for (int i = n - 1; i > -1; --i) {
    int e = arr[i];
    if (uset.find(e) != uset.end())
      continue;

    // count the element frequiency
    int c = 0;

    for (int j = 0; j < n; ++j) {
      if (arr[j] == e)
        c++;
    }

    // number of elements would we remove from end
    c = c - c % 3;

    for (int j = n - 1; j > -1; --j) {
      if (c == 0)
        break;
      if (arr[j] == e) {
        arr[j] = -1; // put -1 at removed element location
        c--;
      }
    }
  }

  //   now just print only elemt those value is not equals to zero
  for (int i = 0; i < n; ++i) {
    if (arr[i] != (-1))
      cout << arr[i] << " ";
  }
  cout << endl;
  return 0;
}
bwitn5fc

bwitn5fc3#

arr = input().split()[::-1]
# print(arr)
i = 0

def count_same(num, ind):
    count = 0
    indexes = []
    for index in range(ind, len(arr)):
        if arr[index] == num:
            count += 1
            indexes.append(index)
            if count == 3:
                break
    if count == 3:
        for index in indexes[::-1]:
            arr.pop(index)
        return True
    return False

while i < len(arr):
    if not count_same(arr[i], i):
        i += 1
    # print(arr)
print(list(map(int, arr[::-1])))
rggaifut

rggaifut4#

#include<bits/stdc++.h>
using namespace std;
int main(){
    unordered_map<int, int> mp;
    vector<int> nums = {2,2,3,2,3,2};
    for(auto it:nums){
        mp[it]++;
    }
    for (int i = 0; i < nums.size();i++){
        if(mp[nums[i]]%3==0){
            continue;
        }
        else{
            cout << nums[i] << " ";
            mp[nums[i]] -= mp[nums[i]] / 3;
        }
    }
}
gijlo24d

gijlo24d5#

这些python代码正在工作,您可以使用以下示例
例一:
输入:数值=[2,4,2,2,7,5,6,7,8,6,6,2,6,7,6]输出:数值=[2、4、5、6、8、6]
例二:
输入:数字[2,2,3,2,3,2]
输出:数值[2,3,3]

from collections import defaultdict
nums = list(map(int, input("Enter the elements of the array separated by commas: ").split(',')))
mp = defaultdict(int)
for i in nums:
    mp[i] += 1
result = []
for i in nums:
    if mp[i] % 3 != 0:
        result.append(i)
        mp[i] -= mp[i] // 3
print(result)

相关问题