【LeetCode-面試演算法經典-Java實現】【001-Two Sum(求兩個數的和)】,leetcode--java

來源:互聯網
上載者:User

【LeetCode-面試演算法經典-Java實現】【001-Two Sum(求兩個數的和)】,leetcode--java
【001-Two Sum(求兩個數的和)】原題

  Given an array of integers, find two numbers such that they add up to a specific target number.
  The function twoSum should 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 would have exactly one solution.
  Input: numbers={2, 7, 11, 15}, target=9
  Output: index1=1, index2=2

題目大意

  給定一個整數數組,找出其中兩個數滿足相加等於你指定的目標數字。
  要求:這個函數twoSum必須要返回能夠相加等於目標數位兩個數的索引,且index1必須要小於index2。請注意一點,你返回的結果(包括index1和index2)都不是基於0開始的。你可以假設每一個輸入肯定只有一個結果。

解題思路

  建立一個輔助類數組,對輔助類進行排序,使用兩個指標,開始時分別指向數組的兩端,看這兩個下標對應的值是否等於目標值,如果等於就從輔助類中找出記錄的下標,構造好返回結果,返回。如果大於就讓右邊的下標向左移,進入下一次匹配,如果小於就讓左邊的下標向右移動,進入下一次匹配,直到所有的資料都處理完

代碼實現
import java.util.Arrays;public class Solution {    /**     * 輔助類     */    private static class Node implements Comparable<Node> {        int val; // 值        int idx; // 值對應的數組下標        public Node() {        }        public Node(int val, int idx) {            this.val = val;            this.idx = idx;        }        // 比較方法        @Override        public int compareTo(Node o) {            if (o == null) {                return -1;            }            return this.val - o.val;        }    }    /**     * 001-Two Sum(求兩個數的和)      *      * @param nums   輸入數組     * @param target 兩個數相加的和     * @return 兩個數對應的下標    public int[] twoSum(int[] nums, int target) {        // 用於儲存返回結果        int[] result = {0, 0};        // 建立輔助數組        Node[] tmp = new Node[nums.length];        for (int i = 0; i < nums.length; i++) {            tmp[i] = new Node(nums[i], i);        }        // 對輔助數組進行排序        Arrays.sort(tmp);        // 記錄輔助數組中左邊一個值的下標        int lo = 0;        // 記錄輔助數組中右邊一個值的下標        int hi = nums.length - 1;        // 從兩邊向中間靠隴進行求解        while (lo < hi) {            // 如果找到結果就設定返回結果,並且退出迴圈            if (tmp[lo].val + tmp[hi].val == target) {                if (tmp[lo].idx > tmp[hi].idx) {                    result[0] = tmp[hi].idx + 1;                    result[1] = tmp[lo].idx + 1;                } else {                    result[0] = tmp[lo].idx + 1;                    result[1] = tmp[hi].idx + 1;                }                break;            }            // 如果大於目標值,右邊的下標向左移動            else if (tmp[lo].val + tmp[hi].val > target) {                hi--;            }            // 如果小於目標值,左邊的下標向右移動            else {                lo++;            }        }        return result;    }}
評測結果

  點擊圖片,滑鼠不釋放,拖動一段位置,釋放後在新的視窗中查看完整圖片。

特別說明歡迎轉載,轉載請註明出處【http://blog.csdn.net/DERRANTCM/article/details/46905233】

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.