LeetCode T1: The sum of two numbers

  • Set a flag, and you can complete 100 power deduction questions in a year

insert image description here
JavaScript version

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
    
    
    for(let i = 0,len = nums.length;i < len;i++){
    
    
        for(let j = i + 1;j < len;j++){
    
    
            if(nums[i] + nums[j] === target){
    
    
                return [i,j]
            }
        }
    }
};

Java Edition

class Solution {
    
    
    public int[] twoSum(int[] nums, int target) {
    
    
        for(int i = 0,len = nums.length;i < len;i++){
    
    
            for(int j = i + 1;j < len;j++){
    
    
                if(nums[i] + nums[j] == target){
    
    
                    return new int[]{
    
    i,j};
                }
            }
        }
        return null;
    }
}


Submit the results
insert image description here
I have to say that Java still runs faster than JavaScript

Guess you like

Origin blog.csdn.net/qq_41602125/article/details/126857647