Java 比较给出三个单独列表的产品
您将收到n种产品的列表,每种产品都有名称、价格和重量 找出列表中有多少产品的副本。复制品是具有所有参数的产品,与其他产品相同 输入是3个列表Java 比较给出三个单独列表的产品,java,arrays,list,Java,Arrays,List,您将收到n种产品的列表,每种产品都有名称、价格和重量 找出列表中有多少产品的副本。复制品是具有所有参数的产品,与其他产品相同 输入是3个列表 Name Price Weight 1. ball 2 1 2. box 2 2 3. ball 2 1 4. ball 2 1 5. box 2 3 输出: 2 说明: (1.) is same as (3.) and is also same as (4.) so there
Name Price Weight
1. ball 2 1
2. box 2 2
3. ball 2 1
4. ball 2 1
5. box 2 3
输出:
2
说明:
(1.) is same as (3.) and is also same as (4.) so there is 2... 1->3 and 1->4
功能描述
numDuplicates具有以下参数:
您需要填写的函数:
public static int numDuplicates(List<String> name, List<Integer> price, List<Integer> weight) {
// Write your code here
//return an int
}
public static int numDuplicates(列表名称、列表价格、列表权重){
//在这里编写代码
//返回一个整数
}
这是我为我的代码写的,是O(n^2),但是一些测试用例已经没有时间了,所以我认为它需要一个O(n)解决方案,但我想不起来
class Result {
/*
* Complete the 'numDuplicates' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. STRING_ARRAY name
* 2. INTEGER_ARRAY price
* 3. INTEGER_ARRAY weight
*/
public static int numDuplicates(List<String> name, List<Integer> price, List<Integer> weight) {
int count = 0;
List<obj> classList = new ArrayList<obj>();
for(int i=0; i<name.size(); i++){
obj holder = new obj(name.get(i),price.get(i),weight.get(i));
classList.add(holder);
}
for(int i=0; i<classList.size(); i++){
for(int j=i+1; j<classList.size();j++){
if(classList.get(i).isDuplicate(classList.get(j))){
count++;
classList.remove(classList.get(j));
j--;
}
}
}
return count;
}
}
class obj{
String name;
int price;
int weight;
public obj(String name, int price, int weight){
this.name = name;
this.price = price;
this.weight = weight;
}
public boolean isDuplicate(obj x){
if(this.name.equals(x.name) && this.price == x.price && this.weight == x.weight){
return true;
}
return false;
}
}
类结果{
/*
*完成下面的“numDuplicates”功能。
*
*该函数应返回一个整数。
*该函数接受以下参数:
*1.字符串\数组名称
*2.整数数组价格
*3.整数_数组权重
*/
公共静态整数(列表名称、列表价格、列表权重){
整数计数=0;
List classList=new ArrayList();
对于(int i=0;ipublic static int numDuplicates)(列表名称、列表价格、,
列表重量){
整数计数=0;
字符串乘积=”;
Map dupMap=新的HashMap();
对于(int i=0;i
公共静态整数(列表名称、列表价格、,
列表重量){
整数计数=0;
字符串乘积=”;
Map dupMap=新的HashMap();
对于(int i=0;i
列表最终版=新列表();
int countTotal=name.Count;
对于(int i=0;i
列表最终版=新列表();
int countTotal=name.Count;
对于(int i=0;i
用于:
public static int numDuplicates(List<String> name, List<Integer> price, List<Integer> weight) {
// Write your code here
//return an int
}
public static int numDuplicates(List<String> name, List<Integer> price, List<Integer> weight) {
List<String> f1 = new ArrayList<String>();
int countTotal = name.size();
for (int i = 0; i < countTotal; i++) {
f1.add(name.get(i) + price.get(i) + weight.get(i));
}
//Unique List item
Set<String> uniqueItemSet = new HashSet<String>(f1);
Integer uniqueItems = uniqueItemSet.size();
int countAll = f1.size();
return(countAll-uniqueItems);
}
public static int numDuplicates(列表名称、列表价格、列表权重){
//在这里编写代码
//返回一个整数
}
如下所示使用:
public static int numDuplicates(List<String> name, List<Integer> price, List<Integer> weight) {
// Write your code here
//return an int
}
public static int numDuplicates(List<String> name, List<Integer> price, List<Integer> weight) {
List<String> f1 = new ArrayList<String>();
int countTotal = name.size();
for (int i = 0; i < countTotal; i++) {
f1.add(name.get(i) + price.get(i) + weight.get(i));
}
//Unique List item
Set<String> uniqueItemSet = new HashSet<String>(f1);
Integer uniqueItems = uniqueItemSet.size();
int countAll = f1.size();
return(countAll-uniqueItems);
}
public static int numDuplicates(列表名称、列表价格、列表权重){
列表f1=新的ArrayList();
int countTotal=name.size();
对于(int i=0;i
用于:
public static int numDuplicates(List<String> name, List<Integer> price, List<Integer> weight) {
// Write your code here
//return an int
}
public static int numDuplicates(List<String> name, List<Integer> price, List<Integer> weight) {
List<String> f1 = new ArrayList<String>();
int countTotal = name.size();
for (int i = 0; i < countTotal; i++) {
f1.add(name.get(i) + price.get(i) + weight.get(i));
}
//Unique List item
Set<String> uniqueItemSet = new HashSet<String>(f1);
Integer uniqueItems = uniqueItemSet.size();
int countAll = f1.size();
return(countAll-uniqueItems);
}
public static int numDuplicates(列表名称、列表价格、列表权重){
//在这里编写代码
//返回一个整数
}
如下所示使用:
public static int numDuplicates(List<String> name, List<Integer> price, List<Integer> weight) {
// Write your code here
//return an int
}
public static int numDuplicates(List<String> name, List<Integer> price, List<Integer> weight) {
List<String> f1 = new ArrayList<String>();
int countTotal = name.size();
for (int i = 0; i < countTotal; i++) {
f1.add(name.get(i) + price.get(i) + weight.get(i));
}
//Unique List item
Set<String> uniqueItemSet = new HashSet<String>(f1);
Integer uniqueItems = uniqueItemSet.size();
int countAll = f1.size();
return(countAll-uniqueItems);
}
public static int numDuplicates(列表名称、列表价格、列表权重){
列表f1=新的ArrayList();
int countTotal=name.size();
对于(int i=0;i
公共静态整数(列表名称、列表价格、列表权重){
Set uniqueProducts=new HashSet();
对于(int i=0;i
公共静态整数(列表名称、列表价格、列表权重){
Set uniqueProducts=new HashSet();
对于(int i=0;i
公共静态整数(列表名称、列表价格、列表权重){
int res=0;
HashSet=newhashset();
对于(int i=0;i公共静态int numDuplicates(列表名称、列表价格、列表权重){
int res=0;
HashSet=newhashset();
对于(int i=0;iMore语言标记吸引了更多的目光关注您的问题,但请记住,这些额外的目光中有不少人对解决Java问题不感兴趣,或者没有资格解决Java问题。有些人会感到恼火。无论如何,没有必要认为某件事情效率低下。您可以分析代码,证明它效率低下,并准确地了解内容是最低效的,这样你就知道该把精力集中在哪里了。@user4581301你可以用任何语言回答这个问题,我只是用java给出了我的答案,所以即使它是用python完成的,我也不介意……Hackerrank说它低效,我知道是O(n^2)使它低效,所以它想要一个O(n)回答,我一直在努力,但我想不出一个O(n)回答。试着这样做:在添加到列表中之前,试着将该项放入集合中。如果它已经在集合中,那么它是重复的。数一数,不要添加到列表中。你必须满足集合中的平等要求,而且自从我写了大量Java以来,它已经超过十年了。我记不起它了m、 但是Java确实有,光是这一点就应该比O更糟糕