Leetcode 933 Number of Recent Calls
Write a class RecentCounter
to count recent requests.
It has only one method: ping(int t)
, where t represents some time in milliseconds.
Return the number of ping
s that have been made from 3000 milliseconds ago until now.
Any ping with time in [t - 3000, t]
will count, including the current ping.
It is guaranteed that every call to ping
uses a strictly larger value of t
than before.
Example 1:
Input: inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]] Output: [null,1,2,3,3]
這個題的意思是找到在給定數字中符合條件的部分。
1)
class RecentCounter {
PriorityQueue<Integer> queue = new PriorityQueue<>();
public RecentCounter() {
}
public int ping(int t) {
queue.add(t);
while(queue.peek() < t - 3000){
queue.poll();
}
return queue.size();
}
}
/**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter obj = new RecentCounter();
* int param_1 = obj.ping(t);
*/
2)
class RecentCounter {
set<int> history;
public:
int ping(int t) {
history.insert( t );
return distance( history.lower_bound( t - 3000 ), history.end() );
}
};
相關文章
- LeetCode之Number of Recent Calls(Kotlin)LeetCodeKotlin
- Leetcode Number of islandsLeetCode
- LeetCode:Largest NumberLeetCode
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- LeetCode-Strobogrammatic NumberLeetCode
- LeetCode-Largest NumberLeetCode
- LeetCode-Number of IslandsLeetCode
- Leetcode Valid NumberLeetCode
- leetcode Palindrome NumberLeetCode
- Leetcode Single NumberLeetCode
- Leetcode 447 Number of BoomerangsLeetCodeOOM
- Leetcode 9 Palindrome NumberLeetCode
- LeetCode-Additive NumberLeetCode
- LeetCode-Strobogrammatic Number IILeetCode
- LeetCode-Super Ugly NumberLeetCode
- LeetCode-Find the Duplicate NumberLeetCode
- LeetCode-Create Maximum NumberLeetCode
- LeetCode-Number of Islands IILeetCode
- [LeetCode] Excel Sheet Column NumberLeetCodeExcel
- Leetcode-Single NumberLeetCode
- Leetcode-Valid NumberLeetCode
- Palindrome Number leetcode javaLeetCodeJava
- Valid Number leetcode javaLeetCodeJava
- Single Number leetcode javaLeetCodeJava
- [LeetCode] 248. Strobogrammatic Number IIILeetCode
- [LeetCode] 9. Palindrome NumberLeetCode
- 【Leetcode】1395. Count Number of TeamsLeetCode
- LeetCode-Number of Digit OneLeetCodeGit
- LeetCode136:Single NumberLeetCode
- Leetcode 263. Ugly NumberLeetCode
- LeetCode 200. Number of IslandsLeetCode
- Leetcode- Single Number IILeetCode
- leetcode刷題--Happy NumberLeetCodeAPP
- leetcode刷題--Number of 1 BitsLeetCode
- LeetCode - 9. Palindrome NumberLeetCode
- [LeetCode] Ugly Number 醜陋數LeetCode
- Single Number II leetcode javaLeetCodeJava