码迷,mamicode.com
首页 > 其他好文 > 详细

发现LeetCode大bug一个!!!!!!

时间:2017-06-27 23:25:20      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:while   pre   lis   turn   array   type   result   return   ==   

1. Two Sum 

第36行本来应该是if (nodes.get(left).key < nodes.get(right).key) { 一不小心误写成了if (nodes.get(left).key < nodes.get(right).value) { 竟然A了!!!!

 1 class Node{
 2     int key;
 3     int value;
 4     public Node(int key, int value) {
 5         this.key = key;
 6         this.value = value;
 7     }
 8 }
 9 public class Solution {
10     /*
11      * @param numbers : An array of Integer
12      * @param target : target = numbers[index1] + numbers[index2]
13      * @return : [index1 + 1, index2 + 1] (index1 < index2)
14      */
15     public int[] twoSum(int[] numbers, int target) {
16         if (numbers == null || numbers.length <= 1) {
17             return new int [2];
18         }
19         //put the key and value in list, the type of list is Node
20         List<Node> nodes = new ArrayList<>();
21         for (int i = 0; i < numbers.length; i++) {
22             nodes.add(new Node(i, numbers[i]));
23         }
24         //sort the list
25         Collections.sort(nodes, new Comparator<Node>() {
26             public int compare(Node a, Node b) {
27                 return a.value - b.value;
28             }
29         });
30         // Two points
31         int left = 0;
32         int right = numbers.length - 1;
33         int[] result = new int[2];
34         while (left < right) {
35             if (nodes.get(left).value + nodes.get(right).value == target) {
36                 if (nodes.get(left).key < nodes.get(right).value) { //第二个明显应该是key,但是第一次不小心写成了value,居然A了,但是lintcode却不能A
37                     result[0] = nodes.get(left).key;
38                     result[1] = nodes.get(right).key;
39                 } else {
40                     result[0] = nodes.get(right).key;
41                     result[1] = nodes.get(left).key;
42                 }
43                 return result;
44             } else if (nodes.get(left).value + nodes.get(right).value < target) {
45                 left++;
46             } else {
47                 right--;
48             }
49         }
50         return result;
51     }
52 }

 

发现LeetCode大bug一个!!!!!!

标签:while   pre   lis   turn   array   type   result   return   ==   

原文地址:http://www.cnblogs.com/muziyueyueniao/p/7087342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!