leetcode第一题java代码报错。求原因?

问题描述

leetcode第一题java代码报错。求原因?

package com.hust.ali.test;
import java.util.*;

/**

  • 给定一个整数数组,发现两个数字,使得它们添加到一个特定的目标数。
  • 函数twoSum应返回两个数字,使得它们加起来的目标,其中索引1必须小于索引2的所有。
  • @author Cat
    *
    /
    public class TwoNumSum {
    /
    *

    • @param args
      */
      public static void main(String[] args) {
      int[] num = {0,4,3,0,6,9};
      TwoNumSum ts = new TwoNumSum();
      int[] result = ts.twoSum (num, 7);
      System.out.println(result[0]+" " + result[1]);
      }
      public int[] twoSum (int[] numbers,int target){
      IdentityHashMap hashNum = new IdentityHashMap();
      Integer[] temp = new Integer[numbers.length];

      for(int i=0;i
      temp[i] = new Integer(numbers[i]);
      hashNum.put(temp[i] , i);
      }
      Arrays.sort(numbers);
      Arrays.sort(temp);
      int i = 0 , j = numbers.length - 1;
      int[] result = new int[2];
      while( i
      if( numbers[i] + numbers[j] == target ){
      result[0] = hashNum.get(temp[i])+1;
      result[1] = hashNum.get(temp[j])+1;
      break;
      }else if (numbers[i] + numbers[j] > target ){
      j--;
      }else if(i < numbers.length-1){
      i++;
      }
      }
      Arrays.sort(result);
      return result;
      }
      }

解决方案

这是我写的Java代码,可以参考下
``
public class Solution {

public int[] twoSum(int[] nums, int target) {
    int[] result = new int[2];
    HashMap<Integer, Integer> t = new HashMap<Integer, Integer>();
    for (int i = 0; i < nums.length; i++) {
        t.put(nums[i], i);
    }
    for (int i = 0; i < nums.length; i++) {
        int tmp = target - nums[i];
        Integer index2 = t.get(tmp);
        if (index2 != null) {
            result[0]=i+1;
            result[1]=index2+1;
            if (result[0] < result[1]) {
                break;
            }
        }
    }
    return result;
}

}
``

解决方案二:

import java.util.*;

/**

  • 给定一个整数数组,发现两个数字,使得它们添加到一个特定的目标数。 函数twoSum应返回两个数字,使得它们加起来的目标,其中索引1必须小于索引2的所有。
  • @author Cat
    /
    public class TwoNumSum {
    /

    • @param args
      */
      public static void main(String[] args) {
      int[] num = { 0, 4, 3, 0, 6, 9 };
      TwoNumSum ts = new TwoNumSum();
      int[] result = ts.twoSum(num, 7);
      System.out.println(result[0] + " " + result[1]);
      }

    public int[] twoSum(int[] numbers, int target) {
    IdentityHashMap hashNum = new IdentityHashMap();
    Integer[] temp = new Integer[numbers.length];
    for (int i = 0; i < temp.length; i++) {
    temp[i] = new Integer(numbers[i]);
    hashNum.put(temp[i], i);
    }
    Arrays.sort(numbers);
    Arrays.sort(temp);
    int i = 0, j = numbers.length - 1;
    int[] result = new int[2];
    while (i != numbers.length) {
    if (numbers[i] + numbers[j] == target) {

            result[0] = (Integer) hashNum.get(temp[i]) + 1;
            result[1] = (Integer) hashNum.get(temp[j]) + 1;
            break;
        }
    
        else if (numbers[i] + numbers[j] > target) {
            j--;
        } else if (i < numbers.length - 1) {
            i++;
        }
    }
    Arrays.sort(result);
    return result;
    

    }
    }

时间: 2024-08-21 13:10:47

leetcode第一题java代码报错。求原因?的相关文章

树形 递归-java 递归报错 求大神帮忙

问题描述 java 递归报错 求大神帮忙 private List<Post> getPostLower(List<Post> PostTops){ List<Post> postAll=new ArrayList<Post>(); // 上级 for(Post post:PostTops){ //查询到下级 List<Post> posts=basService.queryPostByParentId(post.getPostId()); //

MyEclipse 编译java代码报错,但是类文件上没有红叉提示

问题描述 MyEclipse 编译java代码报错,但是类文件上没有红叉提示 MyEclipse 在做项目时,java代码中编译错误之后,在类上没有红X显示,怎么回事呢? 解决方案 建议你将代码放在另外一台电脑上试试,看看是你的电脑环境的问题,还是统一存在的问题. 如果你的电脑的问题,估计别人是帮不上忙的.除了重装系统之外,估计也没有其它办法. 如果都存在这样的问题,可能是软件的 BUG.只能反馈给软件开发者来修改. 解决方案二: 可能是你还没有保存,你保存一下看看 解决方案三: clean一下

第一行代码-酷欧天气showProgressDialog();代码报错

问题描述 酷欧天气showProgressDialog();代码报错 第一行代码的酷欧天气ChooseAreaActivity,showProgressDialog();总是显示报错,根据原版的代码修改后依然如此,想知道是什么原因. 以下是代码: package activity; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.ProgressD

java程序报错,求大神找bug

问题描述 java程序报错,求大神找bug package arraysofobjects; import java.util.Scanner; public class arraysofobjects { public static void main(String[] args) { Scanner input=new Scanner(System.in); students[] stu=new students[4]; String name; int grade; for(int i=0

入门-JAVA 简短代码报错,就是发现不了问题

问题描述 JAVA 简短代码报错,就是发现不了问题 class StaticDemo { public static void main(String[] args) { Person.show(); } } class Person { String name;//成员变量 static String country = "CN"; public static void show() { System.out.println(country+":"); } } /

代码-db2报错求大神解决,在线等到明天

问题描述 db2报错求大神解决,在线等到明天 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'systemInfoCache' defined in class path resource [config/core.xml]: Invocation of init method failed; nested e

java 执行 sql 文件报错 求大神帮忙解决 急急急!!!!

问题描述 java 执行 sql 文件报错 求大神帮忙解决 急急急!!!! sql文件里边的内容 /* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Version : 50096 Source Host : localhost:3306 Source Database : cemscenter1 Target Server Type : MYSQL Target Server Version :

java cmd报错!!求大神!

问题描述 java cmd报错!!求大神! 10C 该项目打包jar在cmd运行报错了.在idea工具下正常运行,请问什么原因啊!求大神指教!具体怎么解决啊!谢谢! 解决方案 activity -server.jar 包没导入吧,放到lib里边试试 解决方案二: 配置文件里面有东西没配置好 解决方案三: http://chenzhou123520.iteye.com/blog/1971322是不是这个原因?

ie8下 全选功能报错-ie8下 JS全选功能报错 求达人帮忙看下代码

问题描述 ie8下 JS全选功能报错 求达人帮忙看下代码 // ???? function autoCalDaysNm() { for(var j=1; j<=29; ++j) { var el = $("#writeForm input[name=atdDay" + j + "]"); var d = Date.parse("2016/02/" + el.val()); if (d) { var t = d.toString("