LeetCode-1 Two Sum

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30986521/article/details/80695199

问题链接

C++

class Solution 
{
    public:
        vector<int> twoSum(vector<int>& nums, int target) 
        {
            int ok = 0;
            vector<int> res;
            for (int i = 0; !ok && i < nums.size(); ++i)
            {
                for (int j = i + 1; !ok && j < nums.size(); ++j)
                {
                    if (nums[i] + nums[j] == target)
                    {
                        ok = 1;
                        res.push_back(i);
                        res.push_back(j);
                    }
                }
            }
            return res;
        }
};

Python

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        res = []
        for i in range(0, len(nums)):
            for j in range(i + 1, len(nums)):
                if nums[i] + nums[j] == target:
                    res.append(i)
                    res.append(j)
                    return res
        

leetcode里two sum结果超时的问题

03-31

由于不太会hash表,所以想法是用快排后二分查找的方法来解决,输出结果是未排序前数字的序号。本机上测试通过所以逻辑应该没问题,但提交leetcode结果是超时:[url=https://leetcode.com/problems/two-sum/][/url]。rn但我在discuss里看见一哥们也是相同的逻辑用c写的,我用他的提交就通过了:[url=https://leetcode.com/discuss/28164/my-c-6ms-solution-using-quicksort-%26-binarysearch][/url]。我看了下,逻辑确实都是快排再二分,有点想不明白了,只能上来求助。rn以下是本人代码:rn[code=c]void swapOrdler(int *x, int *y)rnrn int tmp = *x;rn *x = *y;rn *y = tmp;rnrnvoid quickSort(int number[], int order[], int l, int h)rnrn int low = l, high = h;rn int target = number[low];rn rn if (low >= high)rn return;rn while (low < high) rn while (number[high] >= target && low < high )rn high--;rn number[low] = number[high];rn swapOrdler(order + low, order + high);rn rn rn while (number[low] <= target && low < high)rn low++;rn number[high] = number[low];rn swapOrdler(order + high, order + low);rn rn number[low] = target;rn rn quickSort(number, order, l, low - 1);rn quickSort(number, order, high + 1, h);rnrnint binarySearch(int number[], int target, int h)rnrn int low = 0, high = h, mid = (low + high) / 2;rn rn while (low <= high) rn if (number[mid] == target) rn return mid;rn rn else if (number[mid] > target) rn high = mid - 1;rn mid = (low + high) / 2;rn rn else if (number[mid] < target) rn low = mid + 1;rn mid = (low + high) / 2;rn rn rn rn return 0;rnrnrnint *twoSum(int number[], int n, int target)rnrn int *result = (int *)malloc(sizeof(int) * 2);rn int intemp, index1 = 0, index2 = 0;rn int *orders = (int *)malloc(sizeof(int) * n);rn for (int i = 0; i < n; i++)rn *(orders + i) = i + 1;rnrn quickSort(number, orders, 0, n - 1);rn for (int i = 0; i < n; i++) rn intemp = target - number[i];rn if ((index1 = binarySearch(number, intemp, n - 1))) rn index2 = i;rn break;rn rn rn rn if (orders[index1] < orders[index2])rn result[0] = orders[index1], result[1] = orders[index2];rn elsern result[0] = orders[index2], result[1] = orders[index1];rn rn return result;rn[/code]rn--------------------------------------------------------------rn以下是那哥们通过的代码:rn[code=c]void swap(int *x, int *y)rnrn int temp;rn temp = *x;rn *x = *y;rn *y = temp;rn return;rnrnrnint search(int numbers[], int target, int low, int high)rnrn if (low > high)rn return -1;rn int mid = low + (high - low) / 2;rn if (target < numbers[mid])rn return search(numbers, target, low, mid - 1);rn else if (target > numbers[mid])rn return search(numbers, target, mid + 1, high);rn elsern return mid;rnrnvoid sort(int numbers[], int orders[], int low, int high)rnrn int key, keyOrder, i, j, ran;rn if (low < high)rn rn ran = (low + high) / 2;rn swap(&numbers[ran], &numbers[low]);rn swap(&orders[ran], &orders[low]);rn key = numbers[low];rn keyOrder = orders[low];rn i = low + 1;rn j = high;rn while (i <= j)rn rn while ((i <= high) && (numbers[i] <= key))rn i++;rn while ((j >= low) && (numbers[j] > key))rn j--;rn if (i < j)rn rn swap(&numbers[i], &numbers[j]);rn swap(&orders[i], &orders[j]);rn rnrn rn swap(&numbers[low], &numbers[j]);rn swap(&orders[low], &orders[j]);rn sort(numbers, orders, low, j - 1);rn sort(numbers, orders, j + 1, high);rn rnrnrnint *twoSum(int numbers[], int n, int target) rn int i = 0, j = -1;rn int *index = (int *)malloc(2 * sizeof(int));rn int len = n;rn int *orders = (int *)malloc(n * sizeof(int));rn for (i = 0; i < len; i++)rn rn orders[i] = i + 1;rn rn sort(numbers, orders, 0, len - 1);rn for (i = 0; i < len; i++)rn rn j = search(numbers, target - numbers[i], i + 1, len - 1);rn if (j > -1)rn break;rn rn if (j > -1)rn rn if (orders[i] > orders[j])rn rn index[0] = orders[j]; index[1] = orders[i];rn rn elsern rn index[0] = orders[i]; index[1] = orders[j];rn rn return index;rn rn elsern return NULL;rn[/code]

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试