首页 > 代码库 > 发现LeetCode大bug一个!!!!!!
发现LeetCode大bug一个!!!!!!
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 =http://www.mamicode.com/ 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 =http://www.mamicode.com/= 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一个!!!!!!
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。