admin管理员组文章数量:1130349
Algorithm
1. 题目描述
Given an integer array, find if an integer p exists in the array such that the number of integers greater than p in the array equals to p
If such an integer is found return 1 else return -1.
在一个整形数组中,如果存在一个元素p使得数组中大于p的元素个数刚好为p,则返回1, 否则返回-1.
2. 解法
package com.fqyuan.arrays;import java.util.ArrayList;
import java.util.Collections;/** Given an integer array, find if an integer p exists in the array,* such that the number of integers greater than p in the array equals to p* If such an integer is found return 1 else return -1.*/
public class NobleInteger {public int solve(ArrayList<Integer> A) {Collections.sort(A);for (int i = 0; i < A.size(); i++) {if (A.get(i) == A.size() - i - 1) {if (i == A.size() - 1)
return 1;else if (A.get(i) != A.get(i + 1))
return 1;}}
return -1;}}
Algorithm
1. 题目描述
Given an integer array, find if an integer p exists in the array such that the number of integers greater than p in the array equals to p
If such an integer is found return 1 else return -1.
在一个整形数组中,如果存在一个元素p使得数组中大于p的元素个数刚好为p,则返回1, 否则返回-1.
2. 解法
package com.fqyuan.arrays;import java.util.ArrayList;
import java.util.Collections;/** Given an integer array, find if an integer p exists in the array,* such that the number of integers greater than p in the array equals to p* If such an integer is found return 1 else return -1.*/
public class NobleInteger {public int solve(ArrayList<Integer> A) {Collections.sort(A);for (int i = 0; i < A.size(); i++) {if (A.get(i) == A.size() - i - 1) {if (i == A.size() - 1)
return 1;else if (A.get(i) != A.get(i + 1))
return 1;}}
return -1;}}
本文标签: Algorithm
版权声明:本文标题:Algorithm 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/IT/1693505729a226052.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论