287. Find the Duplicate Number\142. Linked List Cycle II\75. Sort Colors\295. Find Median from Data

287. Find the Duplicate Number

题目描述

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Note:

  • You must not modify the array (assume the array is read only).
  • You must use only constant, O(1) extra space.
  • Your runtime complexity should be less than O(n2).
  • There is only one duplicate number in the array, but it could be repeated more than once.

代码实现

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        int nums_len = nums.size();
        for(int i = 0; i < nums_len; i++) {
            if(nums[abs(nums[i])] < 0) return abs(nums[i]);
            else nums[abs(nums[i])] = -1*nums[abs(nums[i])];    
        }

        return 1;
    }
};

142. Linked List Cycle II

题目描述

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Note: Do not modify the linked list.

代码实现

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *double_node = (head == NULL)?NULL:head;
        ListNode *one_node = head;
        bool isCycle = false;
        while(double_node != NULL) {
            if(double_node->next) double_node = double_node->next->next;
            else break;
            one_node = one_node->next;
            if(double_node && double_node == one_node) {
                isCycle = true; break;
            }    
        }
        if(!isCycle) return NULL;
        one_node = head;
        while(one_node != double_node) {
            one_node = one_node->next;
            double_node = double_node->next;
        }
        return one_node;
    }
};

75. Sort Colors

题目描述

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note:
You are not suppose to use the library’s sort function for this problem.

代码实现

class Solution {
public:
    void sortColors(vector<int>& num) {
        int s[3] = {0}, nlen = num.size();
        for(int i = 0; i < nlen; i++) s[num[i]]++;
        int sift = 0;    
        for(int i = 0; i < 3; i++) {
            sift = i > 0?sift+s[i-1]:0;
            for(int j = 0; j < s[i]; j++) num[j+sift] = i;
        }    
    }
};

295. Find Median from Data Stream

题目描述

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.

Examples:
[2,3,4] , the median is 3

[2,3], the median is (2 + 3) / 2 = 2.5

Design a data structure that supports the following two operations:

void addNum(int num) - Add a integer number from the data stream to the data structure.
double findMedian() - Return the median of all elements so far.
For example:

addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3) 
findMedian() -> 2

代码实现

class MedianFinder {
    vector<int> res;
public:
    /** initialize your data structure here. */
    MedianFinder() {
    }

    void addNum(int num) {
        int n = res.size();
        for(vector<int>::iterator it = res.begin(); it != res.end(); it++) {
            if(*it >= num) {
                res.insert(it, num); return;
            }    
        }
        res.push_back(num);
    }

    double findMedian() {
        int n = res.size();
        int ind = n >> 1;
        return (n&1)?res[ind]:(double(res[ind]+res[ind-1])/2.0);
    }
};

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder obj = new MedianFinder();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值