In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to write the algorithm code of the sum of two numbers". In the daily operation, I believe that many people have doubts about how to write the algorithm code of the sum of two numbers. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to write the algorithm code of the sum of two numbers"! Next, please follow the editor to study!
Given an array of integers nums and an integer target value target, please find the two integers in the array that are the target values and return their array subscript.
Example:
Input: nums = [2, 7, 7, 11, 15], target = 9
Output: [0BI 1]
Explanation: because nums [0] + nums [1] = = 9, return [0,1].
Package com.lau.javabase;import org.junit.Test;import java.time.Duration;import java.time.Instant;import java.util.Arrays;import java.util.HashMap;import java.util.Map;import java.util.Objects / * * time complexity sort: * O (1)-- constant time * O (logN)-- logarithmic time * O (N)-- Linear time * O (Numbn)-- 2 exponential time * O (N*N*N)-- 3 exponential time * * given an integer array nums and an integer target value target, please find the two integers in the array that are the target value. And return their array subscript. * * you can assume that each input will only correspond to one answer. However, the same element in the array cannot be repeated in the answer. * * you can return answers in any order. * * Source: LeetCode (LeetCode) * Link: https://leetcode-cn.com/problems/two-sum * * Test case: * input: nums = [2Jing 7 nums 11jue 15], target = 9 * output: [0J 1] * explanation: because nums [0] + nums [1] = = 9, return [0J 1]. * * Source: LeetCode * Link: https://leetcode-cn.com/problems/two-sum * copyright belongs to the collar buckle network. For commercial reprint, please contact official authorization. For non-commercial reprint, please indicate the source. * * / public class TwoSumTest {@ Test public void test () {int [] array = {1pje 5je 10je 9je 12}; Instant start = Instant.now (); System.out.println ("- 1 -"); int [] resArray = findTwoIntBySum (array, 17); Instant end = Instant.now (); long timeElapsed = Duration.between (start, end). ToNanos () / / in millisecond System.out.println ("Program 1: + timeElapsed); Arrays.stream (resArray) .forEach (System.out:: println); start = Instant.now (); System.out.println ("-2-"); int [] resArray2 = findTwoIntBySum2 (array, 17); end = Instant.now () TimeElapsed = Duration.between (start, end). ToNanos (); / / millisecond System.out.println ("Program 2 time:" + timeElapsed); Arrays.stream (resArray2) .forEach (System.out:: println); start = Instant.now (); System.out.println ("- 3 -"); int [] resArray3 = findTwoIntBySum3 (array, 17) End = Instant.now (); timeElapsed = Duration.between (start, end). ToNanos (); / / millisecond System.out.println ("Program 3 time:" + timeElapsed); Arrays.stream (resArray3) .forEach (System.out:: println); start = Instant.now (); System.out.println ("- 4 -") Int [] resArray4 = findTwoIntBySum4 (array, 17); end = Instant.now (); timeElapsed = Duration.between (start, end). ToNanos (); / / millisecond System.out.println ("Program 4 time:" + timeElapsed); Arrays.stream (resArray4) .forEach (System.out:: println) } / * * @ Description: solution one, traversing * @ param array * @ param target * @ return * / private int [] findTwoIntBySum (int [] array, int target) {int [] resArray = null; for (int I = 0; I)
< array.length - 1; i++){ for(int j = i + 1; j < array.length; j++){ if(target == array[i] + array[j]){ resArray = new int[]{i, j}; return resArray; } } } return resArray; } /** * @Description:解法二,依托HashMap,将数组值作为K,索引作为V存入Map * @param array * @param target * @return */ private int[] findTwoIntBySum2(int[] array, int target){ int[] resArray = null; //建立HashMap,存储 K存值,V存索引 Map midMap = new HashMap();// Arrays.stream(array).forEach(s ->); for (int I = 0; I
< array.length; i++){ midMap.put(array[i], i); } for(int i = 0; i < array.length; i++){ if(Objects.nonNull(midMap.get(target - array[i]))){ resArray = new int[]{i, midMap.get(target - array[i])}; return resArray; } } return resArray; } /** * @Description:解法三,两层遍历(解法一的变种) * @param array * @param target * @return */ private int[] findTwoIntBySum3(int[] array, int target){ int[] resArray = null; for(int i = 0; i < array.length - 1; i++){ for(int j = i + 1; j < array.length; j++){ if(target - array[i] == array[j]){ resArray = new int[]{i, j}; return resArray; } } } return resArray; } /** * @Description:解法四,依托HashMap,一次遍历即可完成 * @param array * @param target * @return */ private int[] findTwoIntBySum4(int[] array, int target){ int[] resArray = null; //建立HashMap,存储 K存值,V存索引 Map midMap = new HashMap();// Arrays.stream(array).forEach(s ->); for (int I = 0; I < array.length; iArray +) {if (midMap.containsKey (target-array [I])) {resArray = new int [] {midMap.get (target-array [I]), I}; return resArray;} midMap.put (array [I], I);} return resArray At this point, the study on "how to write the algorithm code for the sum of two numbers" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 216
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.