[LeetCode-interview algorithm classic-Java implementation] [001-Two Sum (Sum of Two numbers)], leetcode -- java
[001-Two Sum (calculate the Sum of Two numbers )]Original question
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum shocould return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input wowould have exactly one solution.
Input: numbers = {2, 7, 11, 15}, target = 9
Output: index1 = 1, index2 = 2
Theme
Given an integer array, find the two numbers that meet the sum and are equal to the target number you specified.
Requirement: The twoSum function must return an index that can add two numbers equal to the target number, and index1 must be smaller than index2. Note that the returned results (including index1 and index2) are not based on 0. You can assume that each input has only one result.
Solutions
Create an auxiliary class array to sort the auxiliary classes. Use two pointers to point to both ends of the array at the beginning to see if the values of the two subscripts are equal to the target value, if the value is equal, the subscript of the record is found from the auxiliary class, and the returned result is constructed and returned. If the value is greater than, move the lower mark on the right to the left to enter the next match. If the value is smaller than, move the subscript on the left to the right to enter the next match until all the data is processed.
Code Implementation
Import java. util. arrays; public class Solution {/*** auxiliary class */private static class Node implements Comparable <Node> {int val; // value: int idx; // array subscript public Node () {} public Node (int val, int idx) {this. val = val; this. idx = idx;} // comparison method @ Override public int compareTo (Node o) {if (o = null) {return-1;} return this. val-o. val ;}}/*** 001-Two Sum (calculate the Sum of Two numbers) ** @ param nums input array * @ param target the subscript public int [] twoSum (int [] nums, int target) corresponding to the sum of the two numbers and * @ return) {// used to save the returned result int [] result = {0, 0}; // create an auxiliary array Node [] tmp = new Node [nums. length]; for (int I = 0; I <nums. length; I ++) {tmp [I] = new Node (nums [I], I);} // sorts the secondary array Arrays. sort (tmp); // record the subscript int lo = 0 for the value on the left of the secondary array; // record the subscript int hi = nums for the value on the right of the secondary array. length-1; // solve the problem from the two sides to the middle by the cursor while (lo
Evaluation Result Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/46905233] Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.