您的位置:首页 > 其它

LeetCode 1 Two Sum

2015-07-21 10:45 495 查看
1<p align="left"><span style="color: rgb(51, 51, 51);">Given an arrayof integers, find two numbers such that they add up to a specific targetnumber.</span></p><p align="left"><span style="color: rgb(51, 51, 51);">The functiontwoSum should return indices of the two numbers such that they add up to thetarget, where index1 must be less than index2. Please note that your returnedanswers (both index1 and index2) are not zero-based.</span></p><p align="left"><span style="color: rgb(51, 51, 51);">You may assumethat each input would have exactly one solution.</span></p><p align="left"><strong><span style="color: rgb(51, 51, 51);">Input:</span></strong><span style="color: rgb(51, 51, 51);"> numbers={2, 7, 11, 15}, target=9</span></p><p align="left"><strong><span style="color: rgb(54, 46, 43);">Output:</span></strong><span style="color: rgb(54, 46, 43);"> index1=1,index2=2</span></p><p><span style="color: rgb(54, 46, 43);">思路:最简单的方法就是暴力破解法,两层</span><span style="color: rgb(54, 46, 43);">for</span><span style="color: rgb(54, 46, 43);">循环,不过时间复杂度是最高的(</span><span style="color: rgb(54, 46, 43);">N</span><span style="color: rgb(54, 46, 43);">平方)。首先利用库函数</span><span style="color: rgb(54, 46, 43);">sort</span><span style="color: rgb(54, 46, 43);">进行排序,(默认的为升序),然后头尾两个变量相加,然后和</span><span style="color: rgb(54, 46, 43);">target</span><span style="color: rgb(54, 46, 43);">做比较,相等,则找到,若大于则尾变量减一,若小于则头变量加一。知道两个变量相等为止。此题还是比较简单的,因为所给的限制条件为:每一个输入都恰好有一个解。</span></p><div><span style="color: rgb(54, 46, 43);">
</span></div>
.	class Solution {2.	public:3.	    vector<int> twoSum(vector<int>& nums, int target) {4.	        vector<int> ivec(nums);5.	        std::sort(ivec.begin(),ivec.end());6.	        int i = 0;7.	        int j = ivec.size() - 1;8.	        int val1,val2;9.	        int pos1,pos2;10.	        while(i < j)11.	        {12.	            if((ivec[i] + ivec[j]) == target)13.	            {14.	                val1 = ivec[i];15.	                val2 = ivec[j];16.	                break;17.	            }18.	            else if((ivec[i] + ivec[j]) < target)19.	                ++i;20.	            else21.	                --j;22.	        }23.24.	        vector<int> ret(2,0);25.	        if(i == j)26.	            return ret;27.	        bool flag = true;28.	        for(int i = 0;i < nums.size();++i)29.	        {30.	            if(flag)31.	            {32.	                if((nums[i] == val1) || (nums[i] == val2))33.	                {34.	                    pos1 = i + 1;35.	                    flag = false;36.	                }37.	            }38.	            else39.	            {40.	              if((nums[i] == val1) || (nums[i] == val2))41.	                {42.	                    pos2 = i + 1;43.	                    break;44.	                }45.	            }46.	        }47.	        if(pos2 < pos1)48.	        {49.	            int tmp = pos2;50.	            pos2 = pos1;51.	            pos1 = tmp;52.	        }53.	        ret[0] = pos1;54.	        ret[1] = pos2;55.	        return ret;56.57.	    }58.	};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: