示例#1
0
void run4() {
    int nums1Size = arc4random()%20;
    int nums2Size = arc4random()%20;
    int * nums1 = sortedArray(nums1Size);
    int * nums2 = sortedArray(nums2Size);
    double result = findMedianSortedArrays(nums1, nums1Size, nums2, nums2Size);
    printf("median_of_two_sorted_arrays_test %lf", result);
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* sortedArray(int* nums, int start, int end) {
    if (end < start) return NULL;
    struct TreeNode* root = malloc(sizeof(struct TreeNode));
    int cur = (start+end) / 2;
    root->val = nums[cur];
    root->left = sortedArray(nums, start, cur-1);
    root->right = sortedArray(nums, cur+1, end);
    return root;
}
示例#3
0
ConvexHull1<Real>::ConvexHull1 (int numVertices, Real* vertices,
    Real epsilon, bool owner, Query::Type queryType)
    :
    ConvexHull<Real>(numVertices, epsilon, owner, queryType),
    mVertices(vertices)
{
    assertion(mVertices != 0, "Must provide vertices\n");

    std::vector<SortedVertex> sortedArray(mNumVertices);
    int i;
    for (i = 0; i < mNumVertices; ++i)
    {
        sortedArray[i].Value = mVertices[i];
        sortedArray[i].Index = i;
    }
    std::sort(sortedArray.begin(), sortedArray.end());

    Real range = sortedArray[mNumVertices-1].Value - sortedArray[0].Value;
    if (range >= mEpsilon)
    {
        mDimension = 1;
        mNumSimplices = 2;
        mIndices = new1<int>(2);
        mIndices[0] = sortedArray[0].Index;
        mIndices[1] = sortedArray[mNumVertices-1].Index;
    }
}
struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
    return sortedArray(nums, 0, numsSize-1);
}