zoukankan      html  css  js  c++  java
  • Java实现LeetCode_0001_Two Sum

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class TwoSum_1 {
    	public static void main(String[] args) {
    			@SuppressWarnings({ "resource"})
    			Scanner input = new Scanner(System.in);
    			System.out.println("Please input the array's length:");			
    			int length=input.nextInt();
    			int []nums=new int[length];
    			System.out.println("Please input the array elements:");
    			for(int i=0;i<nums.length;i++) {
    					nums[i]=input.nextInt();
    			}//end for
    			System.out.println("Please input the target:");
    			int target=input.nextInt();
    			System.out.println(Arrays.toString(twoSum(nums,target)));
    	}// end main()
    
    /**
     *
     *the function of judge 
     *@param
     *	
     * */
    	public static int[] twoSum(int[] nums, int target) {
    		int i, j; // index
    		for (i = 0; i < nums.length; i++) {
    			for (j = i+1; j < nums.length; j++) {
    				if (target - nums[j] == nums[i]) {
    					return new int[] {i,j};
    				} // end if
    			} // end for
    		} // end for
    		 throw new IllegalArgumentException("No two sum solution");
    	}// end twoSum()
    
    }// end TwoSum_1
    
    
    
  • 相关阅读:
    我说
    时间管理
    职场自我管理
    html元素不可见的三种方式
    windows查看端口占用情况
    windows下vbs脚本隐藏控制台
    找钥匙问题
    CSS中的偏僻知识点
    竖式谜题
    node库的选择
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13076591.html
Copyright © 2011-2022 走看看