Why always said "Submission Result: Time Limit Exceeded"


  • 0
    J

    import java.util.Scanner;

    public class Solution {

    public int[] twoSum(int[] nums, int target) {
        for(int i=0;i<nums.length;i++){
            for(int j=0;j<nums.length;j++){
                if(nums[i]+nums[j]==target && (i<j)){                    
                System.out.println("Output:index1="+(i+1)+",index2="+(j+1));
                }                
           }          
        }
    return nums=null;    
    }
    
    public static void main(String[] args) {
    	System.out.print("Input numbers: ");
    	Scanner scanNum = new Scanner(System.in);
    	int[] nums = new int[4];
    	for (int i = 0; i < 4; i++) {
    		nums[i] = scanNum.nextInt();
    	}
    
    	System.out.print("Input target: ");
    	Scanner scanTar = new Scanner(System.in);
    	int target = scanTar.nextInt();
    
            Solution sulo=new Solution();
                sulo.twoSum(nums,target);
        
    
    }
    

    }


  • 0
    W

    Because your algorithm's running time exceeded the limit, so chose other more efficient algorithm. Besides, OJ does't need the main function.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.