zoukankan      html  css  js  c++  java
  • LeetCode 1 Two Sum(二分法)

    题目来源:https://leetcode.com/problems/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

    解题思路:

    题目要求:给出一个数组numbers 以及 目标和target. 要求找到数组中两个数相加等于target所对应的下标.(下标不为0!)

    /*第一次做LeetCode不熟悉.一直写着int main(),一直给WA.(明明本地的测试已经过了).之后才明白代码的要求.*/

    具体方法:数组进行升序或降序排序(下面采用升序排序),采用二分法进行寻找.

    分为三种情况:

    1 if(num[left].x+num[right].x==target)
    2 else if(num[left].x+num[right].x>target)
    3 else if(num[left].x+num[right].x<target)

    相对应的操作:

                if(num[left].x+num[right].x==target)
                {
                    l=num[num[left].sort_id].id;
                    r=num[num[right].sort_id].id;
                    break;
                }//下面的left和right的移动取决于排序是按照升序还是降序
                else if(num[left].x+num[right].x>target)
                {
                    right=right-1;
                }
                else if(num[left].x+num[right].x<target)
                {
                    left=left+1;
                }

    给出代码:

    #include <bits/stdc++.h>
    #define MAX 10010
    
    using namespace std;
    
    struct Node{
        int x;
        int id;
        int sort_id;
    };
    bool cmp(Node a,Node b)
    {
        return a.x<b.x;
    }
    Node num[MAX];
    
    
    int main()
    {
        int n,target,l,r;
        while(~scanf("%d",&n))
        {
            for(int i=0;i<n;i++)
            {
                scanf("%d",&num[i].x);
                num[i].id=i+1;
            }
            scanf("%d",&target);
            sort(num,num+n,cmp);
            for(int i=0;i<n;i++)
            {
                num[i].sort_id=i;
            }
            int left=0,right=n-1;
            while(left<right)
            {
                if(num[left].x+num[right].x==target)
                {
                    l=num[num[left].sort_id].id;
                    r=num[num[right].sort_id].id;
                    break;
                }
                else if(num[left].x+num[right].x>target)
                {
                    right=right-1;
                }
                else
                {
                    left=left+1;
                }
            }
            printf("%d %d
    ",l,r);
        }
    
    }

    提交代码:

     1 class Solution {
     2 public:
     3     vector<int> twoSum(vector<int>& nums, int target) {
     4         vector<int> result;
     5         int n = nums.size();
     6         if(n < 2)
     7             return result;
     8         vector<int> original = nums;
     9         sort(nums.begin(), nums.end());
    10         int left=0, right=n-1;
    11         int i, j, smaller, bigger;
    12         while(left < right)
    13         {
    14             if(nums[left]+nums[right] == target)
    15             {
    16                 for(i=0; i<n; i++)
    17                 {
    18                     if(nums[left] == original[i])
    19                         {
    20                             result.push_back(i+1);
    21                             break;
    22                         }
    23                 }
    24                 for(j=n-1; j>=0; j--)
    25                 {
    26                     if(nums[right] == original[j])
    27                     {
    28                         result.push_back(j+1);
    29                         break;
    30                     }
    31                 }
    32                 if(result[0] < result[1])
    33                 {
    34                     smaller = result[0];
    35                     bigger  = result[1];
    36                 }
    37                 else
    38                 {
    39                     smaller = result[1];
    40                     bigger  = result[0];
    41                 }
    42                 result[0] = smaller;
    43                 result[1] = bigger;
    44                 return result;
    45             }
    46             else if(nums[left]+nums[right] < target)
    47                 left = left + 1;
    48             else
    49                 right = right - 1;
    50         }
    51         return result;
    52     }
    53 };
  • 相关阅读:
    HubSpot – 网站开发必备的 jQuery 信息提示库
    Ink – 帮助你快速创建响应式邮件(Email)的框架
    Simptip – 使用 Sass 制作的 CSS Tooltip 效果
    字体大宝库:12款好看的手写艺术字体免费下载
    『摄影欣赏』20幅温馨浪漫的精美照片欣赏【组图】
    Stickup – 轻松实现元素固定效果的 jQuery 插件
    精品素材:15套免费的 Photoshop 自定义图形集
    Node.js 入门手册:那些最流行的 Web 开发框架
    潮流设计:15个创意的 3D 字体版式作品欣赏
    值得拥有!精心推荐几款超实用的 CSS 开发工具
  • 原文地址:https://www.cnblogs.com/zpfbuaa/p/5049571.html
Copyright © 2011-2022 走看看