[Daily Coding Problem] 1 (LeetCode 1). Find if two numbers in an array add up to k

This problem was recently asked by Google.

Given a list of numbers and a number k, return whether any two numbers from the list add up to k.

For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.

Bonus: Can you do this in one pass?

经典2-sum问题的变形.  最直观的做法是first pass, 用HashMap保存每个数和其index的mapping关系, 然后second pass来寻找是否有满足要求的。 需要注意的一点是,同一个位置的数字不能使用两次.

 1 public class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
 4         for(int i = 0; i < nums.length; i++)
 5         {
 6             hm.put(nums[i], i);
 7         }
 8
 9         for(int i = 0; i < nums.length; i++)
10         {
11             if(hm.containsKey(target - nums[i]) && i != hm.get(target - nums[i]))
12             {
13                 int[] r = new int[2];
14                 r[0] = i;
15                 r[1] = hm.get(target - nums[i]);
16                 return r;
17             }
18         }
19         return null;
20     }
21 }

1 pass solution 就是一边扫描数组, 一边更新Map。 如果找到满足要求的pair, 直接返回, 否则把当前的数插入到Map中.

 1 public class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         Map<Integer, Integer> map = new HashMap<Integer, Integer>();
 4         for(int i = 0; i < nums.length; i++) {
 5             if(map.containsKey(target - nums[i])) {
 6                 int[] r = new int[2];
 7                 r[0] = map.get(target - nums[i]);
 8                 r[1] = i;
 9                 return r;
10             }
11             else {
12                 map.put(nums[i], i);
13             }
14         }
15         return null;
16     }
17 }

原文地址:https://www.cnblogs.com/lz87/p/10106326.html

时间: 2024-10-08 00:40:43

[Daily Coding Problem] 1 (LeetCode 1). Find if two numbers in an array add up to k的相关文章

Daily Coding Problem: Problem #339

/** * This problem was asked by Microsoft. Given an array of numbers and a number k, determine if there are three entries in the array which add up to the specified number k. For example, given [20, 303, 3, 4, 25] and k = 49, return true as 20 + 4 +

[Daily Coding Problem 68] Count Pairs of attacking bishop pairs

This problem was asked by Google. On our special chessboard, two bishops attack each other if they share the same diagonal. This includes bishops that have another bishop located between them, i.e. bishops can attack through pieces. You are given N b

[Daily Coding Problem 70] Nth perfect number

This problem was asked by Microsoft. A number is considered perfect if its digits sum up to exactly 10. Given a positive integer n, return the n-th perfect number. For example, given 1, you should return 19. Given 2, you should return 28. This is one

Daily Coding Problem: Problem #315

/** * This problem was asked by Google. In linear algebra, a Toeplitz matrix is one in which the elements on any given diagonal from top left to bottom right are identical. Here is an example: 1 2 3 4 8 5 1 2 3 4 4 5 1 2 3 7 4 5 1 2 Write a program t

[Daily Coding Problem 223] O(1) space in order traversal of a binary tree

Typically, an implementation of in-order traversal of a binary tree has O(h) space complexity, where h is the height of the tree. Write a program to compute the in-order traversal of a binary tree using O(1) space. In-order traversal without recursio

[Daily Coding Problem 290] Quxes Transformation

On a mysterious island there are creatures known as Quxes which come in three colors: red, green, and blue. One power of the Qux is that if two of them are standing next to each other, they can transform into a single creature of the third color. Giv

[Daily Coding Problem 294] Shortest round route with rising then falling elevations

A competitive runner would like to create a route that starts and ends at his house, with the condition that the route goes entirely uphill at first, and then entirely downhill. Given a dictionary of places of the form {location: elevation}, and a di

[Daily Coding Problem] Find the total number of solutions of a linear equation of n variables

Given a linear equation of n variables, find the total number of non-negative integer solutions of it. All coefficients are positive. Example: input: x + 2 * y = 5 output: 3,  the 3 possible integer solutions are x = 1, y = 2; x = 3, y = 1; x = 5, y

[Daily Coding Problem 250] Cryptarithmetic Puzzle

A cryptarithmetic puzzle is a mathematical game where the digits of some numbers are represented by letters. Each letter represents a unique digit. For example, a puzzle of the form: SEND + MORE -------- MONEY may have the solution: {'S': 9, 'E': 5,