2017.11.21 LeetCode

来源:互联网 发布:欧洲人怎么看中国知乎 编辑:程序博客网 时间:2024/05/21 17:39

728. Self Dividing Numbers

A self-dividing number is a number that is divisible by every digit it contains.

For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.

Also, a self-dividing number is not allowed to contain the digit zero.

Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.

Example 1:

Input:
left = 1, right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

Note:

The boundaries of each input argument are 1 <= left <= right <= 10000.

题意: 给你个范围,让你求范围内的满足一些数可以整除该数的所有数字,比如128,因为128%1 == 0,,128%2 == 0,128%8 == 0 所以改数字就满足,把所有满足的放进去即可

分析: 直接扫过去,然后每个数判断即可,水~

class Solution {public:    bool get(int x) {        int t = x;        while(t) {            int tt = t%10;            if(tt == 0) return false;            if(x%tt != 0) return false;            t /= 10;        }        return true;    }    vector<int> selfDividingNumbers(int left, int right) {        vector<int> res;        for(int i = left;i <= right;i++) {            if(get(i)) {                res.push_back(i);            }        }        return res;    }};

729. My Calendar I

Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.

Your class will have the method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.

A double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)

For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.

Your class will be called like this: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)
Example 1:
MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(15, 25); // returns false
MyCalendar.book(20, 30); // returns true

Explanation:

The first event can be booked. The second can’t because time 15 is already booked by another event.
The third event can be booked, as the first event takes every time less than 20, but not including 20.

Note:

The number of calls to MyCalendar.book per test case will be at most 1000.
In calls to MyCalendar.book(start, end), start and end are integers in the range [0, 10^9].

题意: 给你一个类,让你写两个方法,一个是清空方法,另一个是判断可否添加该范围,每次呢给你一个范围,要求范围不能有重复的区域,如果有的话就返回false,可以的话添加进去返回true

分析: 我是用的pair来存储可行的范围,然后暴力的判断每个是否可行,这里判断时注意,只要判断是否可行就行,不用判断不可行的

参考函数

class MyCalendar {public:    vector<pair<int,int> > s;    MyCalendar() {        s.clear();    }    bool check(int start,int end,pair<int,int> p) {        int l = p.first,r = p.second;        if(start > r) return true;        if(end < l) return true;        return false;    }    bool book(int start, int end) {        int len = s.size();        if(!len) {            s.push_back(make_pair(start,end-1));            return true;        }        bool tag = false;        for(int i = 0;i < len;i++) {            if(!check(start,end-1,s[i])) {                tag = true;                break;            }        }        if(!tag){            s.push_back(make_pair(start,end-1));            return true;        } else {            return false;        }    }};
原创粉丝点击