Java Solution - LeetCode Problem 1 Two Sum

Published: (June 13, 2026 at 01:22 PM EDT)
1 min read
Source: Dev.to

Source: Dev.to

Problem Description:

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to the target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Link to the problem class Solution { public int[] twoSum(int[] nums, int target) { //map to store visited numbers along with their index HashMap map = new HashMap<>(); for (int i=0; i<nums.length; i++) { int requiredNum = target - nums[I]; if(map.containsKey(requiredNum)) { //return the current index and the index from the map return new int[] {map.get(requiredNum), I}; } else { map.put(nums[i], i); } } return null; } }

0 views
Back to Blog

Related posts

Read more »

The spec is in the wrong place

My day job is at a large tech company. Hundreds of engineering teams, and every one of them is somewhere different on AI adoption. Some are still treating codin...

The Heuristics Say Don't

A culture that only records its disasters ends up with a biased archive. Wars documented, plagues chronicled, collapses catalogued. The quiet decades go unwritt...