1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實(shí)戰(zhàn)167:兩數(shù)之和 II - 輸入有序數(shù)組

        共 2189字,需瀏覽 5分鐘

         ·

        2021-01-28 14:15

        算法的重要性,我就不多說(shuō)了吧,想去大廠,就必須要經(jīng)過(guò)基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

        今天和大家聊的問(wèn)題叫做?兩數(shù)之和 II - 輸入有序數(shù)組??,我們先來(lái)看題面:
        https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/

        Given an array of integers that is already sorted in ascending order, 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.


        Note:


        Your returned answers (both index1 and index2) are not zero-based.

        You may assume that each input would have exactly one solution and you may not use the same element twice.

        題意


        給定一個(gè)已按照升序排列 的有序數(shù)組,找到兩個(gè)數(shù)使得它們相加之和等于目標(biāo)數(shù)。

        函數(shù)應(yīng)該返回這兩個(gè)下標(biāo)值 index1 和 index2,其中 index1 必須小于 index2。

        說(shuō)明:

        返回的下標(biāo)值(index1 和 index2)不是從零開(kāi)始的。
        你可以假設(shè)每個(gè)輸入只對(duì)應(yīng)唯一的答案,而且你不可以重復(fù)使用相同的元素。

        樣例

        輸入: numbers = [2, 7, 11, 15], target = 9
        輸出: [1,2]
        解釋: 2 與 7 之和等于目標(biāo)數(shù) 9 。因此 index1 = 1, index2 = 2 。


        解題

        https://blog.csdn.net/weixin_41943637/article/details/103343131

        思路1:雙層循環(huán)


        class?Solution?{
        ????public?int[] twoSum(int[] numbers, int?target) {
        ???????for?(int?i = 0; i < numbers.length; i++) {
        ????????????int?Num = target - numbers[i];
        ????????????for?(int?j = i + 1; j < numbers.length; j++) {
        ????????????????if?(Num == numbers[j]){
        ????????????????????int?nums[] = {i+1,j+1};
        ????????????????????return?nums;
        ????????????????}
        ????????????}
        ????????}
        ????????return?null;
        ????}
        }


        思路2:雙指針

        前后雙指針?lè)謩e指向數(shù)組的首尾,如果首尾元素相加大于目標(biāo),則尾指針向前一位;反之,如果首尾元素相加小于目標(biāo),則首指針向后一位。最差情況下,整個(gè)數(shù)組也只會(huì)遍歷一遍,所以時(shí)間復(fù)雜度為O(N)。


        class?Solution?{
        ???public?int[] twoSum(int[] numbers, int?target) {
        ??????int?left=0;
        ??????int?right=numbers.length-1;
        ??????while(left?????????int?sum=numbers[left]+numbers[right];
        ?????????if(sum==target)return?new?int[]{left+1,right+1};
        ?????????else?if(sum?????????else?if(sum>target)right--;
        ??????}
        ????return?null;
        ????}
        }


        思路3:二分查找


        class?Solution?{
        ????public?int[] twoSum(int[] numbers, int?target) {
        ????????for(int?i = 0; i < numbers.length; i++) {
        ????????????int?index = binarySearchIndex(numbers, i + 1, target - numbers[i]);
        ????????????if(index != -1) {
        ???????????????return?new?int[]{i + 1, index + 1};
        ????????????}
        ????????}
        ????????return?new?int[]{};
        ????}
        ????public?int?binarySearchIndex(int[] numbers, int?startIndex, int?target) {
        ????????int?low = startIndex;
        ????????int?high = numbers.length - 1;
        ????????while(low <= high) {
        ????????????int?mid = (low + high) >>> 1;
        ????????????if(target > numbers[mid]) {
        ????????????????low = mid + 1;
        ????????????} else?if(target < numbers[mid]) {
        ????????????????high = mid - 1;
        ????????????} else?{
        ????????????????return?mid;
        ????????????}
        ????????}
        ????????return?-1;
        ????}
        }



        好了,今天的文章就到這里,如果覺(jué)得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

        上期推文:

        LeetCode1-160題匯總,希望對(duì)你有點(diǎn)幫助!
        LeetCode刷題實(shí)戰(zhàn)161:相隔為1的編輯距離
        LeetCode刷題實(shí)戰(zhàn)162:尋找峰值
        LeetCode刷題實(shí)戰(zhàn)163:缺失的區(qū)間
        LeetCode刷題實(shí)戰(zhàn)164:最大間距
        LeetCode刷題實(shí)戰(zhàn)165:比較版本號(hào)
        LeetCode刷題實(shí)戰(zhàn)166:分?jǐn)?shù)到小數(shù)


        瀏覽 24
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            欧美四区 | 中国艹逼视频 | 日日夜夜噜 | 人人妻人人澡人人爽人人欧美一区 | 欧美午夜精品久久久久 | 泷泽萝拉无删减avhd | 女尊高h乱l高辣h文全文阅读 | 无码任你躁久久久久久小说 | 含着她花蒂啃到高潮视频 | 中国老太做爰免费视频 |