LeetCode 2016 400,398,397,396,393,352,284

400 Nth Digit

class Solution {
public:
    vector<long long> f;
    vector<long long> g;
    vector<long long> tens;
    int dUpper = 10;
    int th = 9;
    void countNumbers()
    {
        long long ten = 1;
        long long nines = 9;
        g.clear();g.resize(11);g[0]=0;
        f.clear();f.resize(11);f[0]=0;
        tens.clear();tens.resize(11);tens[0]=0;
        for(long long i=1;i<=dUpper;i++)
        {
            f[i]=0;
            f[i]=(nines - ten + 1)*i;
            g[i]=g[i-1]+f[i];
            tens[i]=ten;
            ten*=10;
            nines = nines * 10 + 9;
        }
        tens[1]=0;
    }
    int findGreatIndex(int n)
    {
        int i;
        for(i=1;i<=dUpper;i++)
        {
            if (g[i]>=n) break;
        }
        return i;
    }
    int findDigit(int st, int n)
    {
        int i=st-1,cnt=0,len;
        string s;
        for(;;)
        {
            i++;
            stringstream strStream;
            strStream << i;
            s = strStream.str();
            len = s.size();
            cnt+=len;
            if (cnt>=n)
            {
                cnt-=len;
                break;
            }
        }
        cnt=n-cnt;
        return s[cnt-1]-'0';
    }
    int findNthDigit(int n)
    {
        if (n<=9) return findDigit(1,n);
        countNumbers();
        int index = findGreatIndex(n);
        n -=g[index-1];
        int d = n/index;
        n-= d*index;
        int st = tens[index]+d;
        if (n==0)
        {
            return (st-1) % 10;
        }
        else
        return findDigit(st,n);
    }
};

398 Random Pick Index

class Solution {
public:
    vector<int> numbers;
    int len;
    Solution(vector<int> nums)
    {
        this->numbers = nums;
        this->len = nums.size();
    }

    int pick(int target)
    {
        int ans = 0;
        while (numbers[ans]!=target) ans++;
        int node = ans + 1;
        int k=1;
        int n=k+1;
        while (node < len)
        {
            if (numbers[node]!=target)
            {
                node++;
                continue;
            }
            int index=rand()%n;
            if (index==0) ans=node;
            n++;
            node++;
        }
        return ans;

    }
};

397 Integer Replacement

class Solution {
public:
    int integerReplacement(int n)
    {
        int cnt = 0;
        long long left = n;
        while(left!=1)
        {
            //cout<<left<<endl;
            while(left % 2 ==0 && left > 1)
            {
                cnt++;
                left /=2;
            }
            if (left ==1) break;
            long long minusTwo = 0,plusTwo = 0;
            long long minusOne = left - 1, plusOne = left + 1;
            while(minusOne%2==0) 
            {
                minusOne/=2;
                minusTwo++;
            }
            while(plusOne%2==0) 
            {
                plusOne/=2;
                plusTwo++;
            }
            if (minusOne ==1 && plusOne ==1 ) 
            {
                cnt += min(plusTwo,minusTwo)+1;
                break;
            }
            if (minusTwo > plusTwo) left--; else left++;
            cnt++;
        }
        return cnt;
    }
};

396 Rotate Function

class Solution {
public:
    int maxRotateFunction(vector<int>& A)
    {
        int ans = 0, tmpans = 0;
        int len = A.size();
        if (len ==0) return ans;
        int sum = 0;
        vector<int> nums;
        nums.clear();
        for(int i=0;i<2;i++)
        {
            for(int j=0;j<len;j++)
                nums.push_back(A[j]);
        }
        for(int i=0;i<len;i++)
        {
            sum += A[i];
            ans += A[i]*i;
        }
        int iUpper = len -1;
        tmpans = ans;
        for(int i=0;i<iUpper;i++)
        {
            tmpans -=sum;
            tmpans += A[i]*len;
            if (tmpans>ans) ans = tmpans;
        }
        return ans;
    }
};

393 UTF-8 Validation

class Solution {
public:
    int len;
    vector< pair<int,int> > head;
    vector< pair<int,int> > interval;
    vector<int> lower;
    vector<int> upper;
    int upNums = 5;
    void initial()
    {
        lower.clear();upper.clear();

        // [0] ans the 10xx xxxx
        // [1000 0000 , 1011 1111]
        lower.push_back(128);upper.push_back(191);

        // [0000 0000 , 0111 1111]
        lower.push_back(0);upper.push_back(127);

        // [1100 0000 , 1101 1111]
        lower.push_back(192);upper.push_back(223);

        // [1110 0000 , 1110 1111]
        lower.push_back(224);upper.push_back(239);

        // [1111 0000 , 1111 0111]
        lower.push_back(240);upper.push_back(247);

        head.clear();interval.clear();
        return ;
    }

    bool validUtf8(vector<int>& data)
    {
        this->len = data.size();
        initial();
        int index = 0;
        while(index<len)
        {
            if (lower[1]<=data[index] && data[index]<=upper[1])
            {
                index++;
                continue;
            }
            bool headFlag = false;
            int i;
            for(i=2;i<=4;i++)
            {
                if (lower[i]<=data[index] && data[index]<=upper[i])
                {
                    headFlag = true;
                    break;
                }
            }
            //cout<<i<<endl;
            if (!headFlag) return false;
            int j;
            for(j=index+1;j<index+i;j++)
            {
                //cout<<"j = "<<j<<endl;
                if (data[j]>upper[0] || data[j]<lower[0]) return false;
            }
            index = j;
        }
        return true;
    }
};

352 Data Stream as Disjoint Intervals

class SummaryRanges {
public:
    /** Initialize your data structure here. */
    set<int> s;
    SummaryRanges()
    {
        s.clear();
    }
    void addNum(int val)
    {
        if (s.find(val)==s.end()) s.insert(val);
    }
    vector<Interval> getIntervals()
    {
        vector<int> nums;
        vector<Interval> ans;
        ans.clear();
        nums.clear();
        for(set<int>::iterator it = s.begin();it!=s.end();it++)
            nums.push_back((*it));
        if (nums.size()==0) return ans;
        int left=0,right=0;
        int len=nums.size();
        nums.push_back(nums[len-1]-1);
        len++;
        for(int i=1;i<len;i++)
        {
            if (nums[i]==nums[i-1]+1)
            {
                right=i;
                continue;
            }
            Interval tmp = Interval(nums[left],nums[right]);
            ans.push_back(tmp);
            left=i;right=i;
        }
        return ans;
    }
};

284 Peeking Iterator

// Below is the interface for Iterator, which is already defined for you.
// **DO NOT** modify the interface for Iterator.
class Iterator {
    struct Data;
	Data* data;
public:
	Iterator(const vector<int>& nums);
	Iterator(const Iterator& iter);
	virtual ~Iterator();
	// Returns the next element in the iteration.
	int next();
	// Returns true if the iteration has more elements.
	bool hasNext() const;
};


class PeekingIterator : public Iterator {
public:
	PeekingIterator(const vector<int>& nums) : Iterator(nums) {
	    // Initialize any member here.
	    // **DO NOT** save a copy of nums and manipulate it directly.
	    // You should only use the Iterator interface methods.
	    
	}

    // Returns the next element in the iteration without advancing the iterator.
	int peek() {
	    return Iterator(*this).next();
	}

	// hasNext() and next() should behave the same as in the Iterator interface.
	// Override them if needed.
	int next() {
	    return Iterator::next();
	}

	bool hasNext() const {
	    return Iterator::hasNext();
	}
};









评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值