感情是培养出来滴!# pets - 心有所宠
o*i
1 楼
我写的是
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
public int maxPoints(Point[] points) {
Point p1 = new Point();
Point p2 = new Point();
int maxCount = 0;
if(points.length == 1){
return 1;
}
for(int i=0;i boolean isSame = true;
p1 = points[i];
for(int j=i+1;j int count = 2;
p2 = points[j];
if(p1.x==p2.x && p1.y==p2.y){//skip if at same point
if(isSame){
for(int l=0; l points are same
if(l!=i && l!=j){
Point checkP = points[l];
if(checkP.x == p1.x && checkP.y==p1.y){
count++;
}
}
if(count==points.length) return count;
isSame = false;
}
}
if(count>maxCount) maxCount = count;
continue;
}
else if(p1.x==p2.x){//x=p1.x
for(int l=0; l if(l!=i && l!=j){
Point checkP = points[l];
if(checkP.x == p1.x){
count++;
}
}
}
if(count>maxCount) maxCount = count;
}
else if(p1.y==p2.y){//y=p1.y
for(int l=0; l if(l!=i && l!=j){
Point checkP = points[l];
if(checkP.y == p1.y){
count++;
}
}
}
if(count>maxCount) maxCount = count;
}
else{
double k = (double)(p2.y-p1.y)/(p2.x-p1.x);//slope rate
double b = (double)p1.y-(double)k*p1.x;
for(int l=0; l if(l!=i && l!=j){
Point checkP = points[l];
if(checkP.y == (k*checkP.x+b)){
count++;
}
}
}
if(count>maxCount) maxCount = count;
}
}
}
return maxCount;
}
}
出现错误
Input: [(-54,-297),(-36,-222),(3,-2),(30,53),(-5,1),(-36,-222),(0,2),(1,
3),(6,-47),(0,4),(2,3),(5,0),(48,128),(24,28),(0,-5),(48,128),(-12,-122),(-
54,-297),(-42,-247),(-5,0),(2,4),(0,0),(54,153),(-30,-197),(4,5),(4,3),(-42,
-247),(6,-47),(-60,-322),(-4,-2),(-18,-147),(6,-47),(60,178),(30,53),(-5,3),
(-42,-247),(2,-2),(12,-22),(24,28),(0,-72),(3,-4),(-60,-322),(48,128),(0,-72
),(-5,3),(5,5),(-24,-172),(-48,-272),(36,78),(-3,3)]
Output: 27
Expected: 30
有人可以帮我看看问题出在哪吗?
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
public int maxPoints(Point[] points) {
Point p1 = new Point();
Point p2 = new Point();
int maxCount = 0;
if(points.length == 1){
return 1;
}
for(int i=0;i
p1 = points[i];
for(int j=i+1;j
p2 = points[j];
if(p1.x==p2.x && p1.y==p2.y){//skip if at same point
if(isSame){
for(int l=0; l
if(l!=i && l!=j){
Point checkP = points[l];
if(checkP.x == p1.x && checkP.y==p1.y){
count++;
}
}
if(count==points.length) return count;
isSame = false;
}
}
if(count>maxCount) maxCount = count;
continue;
}
else if(p1.x==p2.x){//x=p1.x
for(int l=0; l
Point checkP = points[l];
if(checkP.x == p1.x){
count++;
}
}
}
if(count>maxCount) maxCount = count;
}
else if(p1.y==p2.y){//y=p1.y
for(int l=0; l
Point checkP = points[l];
if(checkP.y == p1.y){
count++;
}
}
}
if(count>maxCount) maxCount = count;
}
else{
double k = (double)(p2.y-p1.y)/(p2.x-p1.x);//slope rate
double b = (double)p1.y-(double)k*p1.x;
for(int l=0; l
Point checkP = points[l];
if(checkP.y == (k*checkP.x+b)){
count++;
}
}
}
if(count>maxCount) maxCount = count;
}
}
}
return maxCount;
}
}
出现错误
Input: [(-54,-297),(-36,-222),(3,-2),(30,53),(-5,1),(-36,-222),(0,2),(1,
3),(6,-47),(0,4),(2,3),(5,0),(48,128),(24,28),(0,-5),(48,128),(-12,-122),(-
54,-297),(-42,-247),(-5,0),(2,4),(0,0),(54,153),(-30,-197),(4,5),(4,3),(-42,
-247),(6,-47),(-60,-322),(-4,-2),(-18,-147),(6,-47),(60,178),(30,53),(-5,3),
(-42,-247),(2,-2),(12,-22),(24,28),(0,-72),(3,-4),(-60,-322),(48,128),(0,-72
),(-5,3),(5,5),(-24,-172),(-48,-272),(36,78),(-3,3)]
Output: 27
Expected: 30
有人可以帮我看看问题出在哪吗?