Skip to content

Instantly share code, notes, and snippets.

@samandar-boymurodov
Last active June 6, 2021 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samandar-boymurodov/b7da20428c99c3d664af590db8d30871 to your computer and use it in GitHub Desktop.
Save samandar-boymurodov/b7da20428c99c3d664af590db8d30871 to your computer and use it in GitHub Desktop.
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
for (let [i1, v1] of nums.entries()) {
for (let [i2, v2] of nums.entries()) {
if (i1 !== i2) {
if (v1+v2 === target) return [i1, i2]
}
}
}
};
@samandar-boymurodov
Copy link
Author

public int[] twoSum(int[] nums, int target) {
    for (int i = 0; i < nums.length; i++) {
        for (int j = i + 1; j < nums.length; j++) {
            if (nums[j] == target - nums[i]) {
                return new int[] { i, j };
            }
        }
    }
    throw new IllegalArgumentException("No two sum solution");
}
  • I liked this solution written in java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment