703.数据流中第k大的元素
/*
*优先队列的用法,priority_queue,greater> q,容器类型是vector,大的元素放前面,即小顶堆
*/
class KthLargest {
public:
KthLargest(int k, vector<int>& nums) {
for(auto num : nums){
q.push(num);
}
while(q.size()>k)
q.pop();
this->k = k;
}
int add(int val) {
q.push(val);
if(q.size() > k) q.pop();
return q.top();
}
private:
int k;
priority_queue<int,vector<int>,greater<int>> q;
};
/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest* obj = new KthLargest(k, nums);
* int param_1 = obj->add(val);
*/
704.二分查找
class Solution {
public:
int search(vector<int>& nums, int target) {
int i = 0, j = nums.size()-1,mid;
while(i<=j){
mid = i + (j-i)/2;
if(nums[mid] == target) return mid;
else if(nums[mid] > target){
j = mid - 1;
}else{
i = mid + 1;
}
}
return -1;
}
};
705.设计哈希集合
class MyHashSet {
public:
/** Initialize your data structure here. */
MyHashSet() {
}
void add(int key) {
if(arr[key]==0)
arr[key]++;
}
void remove(int key) {
if(arr[key]==1)
arr[key]--;
}
/** Returns true if this set contains the specified element */
bool contains(int key) {
if(arr[key]>0)
return true;
return false;
}
private:
int arr[1000001] = {
0};
};
/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet* obj = new MyHashSet();
* obj->add(key);
* obj->remove(key);
* bool param_3 = obj->contains(key);
*/
706.设计哈希映射
class MyHashMap {
public:
/** Initialize your data structure here. */
MyHashMap() {
for(int i = 0; i <= 1000000; ++i)
arr[i]=-1;
}
/** value will always be non-negative. */
void put(int key, int value) {
arr[key]=value;
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
int get(int key) {
return arr[key];
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
void remove(int key) {
arr[key]=-1;
}
private:
int arr[1000001];
};
/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap* obj = new MyHashMap();
* obj->put(key,value);
* int param_2 = obj->get(key);
* obj->remove(key);
*/
709.转换成小写字母
class Solution {
public:
string toLowerCase(string str) {
for(auto& c : str){
c = (c>='A'&&c<='Z') ? c+32 : c;
}
return str;
}
};
717.1比特字符和2比特字符
class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
int n = bits.size();
for(int i = 0; i < n;){
if(i==n-1) return true;
i = i + 1 + bits[i];
}
return false;
}
};
720.词典中最长的单词
class Solution {
public:
string longestWord(vector<string>& words) {
sort(words.begin(), words.end());
set<string> tmp;
tmp.insert("");
string res = "";
for(auto w : words){
auto cstr = w.substr(0, w.size()-1);
if(tmp.find(cstr)!=tmp.end()){
tmp.insert(w);
if(w.length()>res.length())
res = w;
}
}
return res;
}
};
724.寻找数组的中心索引
class Solution {
public:
int pivotIndex(vector<int>& nums) {
if(nums.empty()) return -1;
// int sum = accumulate(nums.begin(), nums.end(),0);
int sum = 0;
for(int n : nums){
sum += n;
}
int sum_left = 0;
for(int i = 0; i < nums.size(); ++i){
if(i>0) sum_left += nums[i-1];
if(sum_left == sum - nums[i] - sum_left)
return i;
}
return -1;
}
};
728.自除数
class Solution {
public:
vector<int> selfDividingNumbers(int left, int right) {
vector<int> res;
for(;left<=right;left++){
int num = left;
while(num){
int tmp = num%10;
if(tmp == 0 || left%tmp!=0) break;
num /= 10;
}
if(num==0) res.push_back(left);
}
return res;
}
};
733.图像渲染
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
fillRecur(image,sr,sc,newColor,image[sr][sc]);
return image;
}
void fillRecur(vector<vector<int>>& image, int sr, int sc, int newColor, int oldColor){
if(image[sr][sc]==newColor) return;
image[sr][sc] = newColor;
if(sr-1>=0 && image[sr-1][sc]==oldColor) fillRecur(image, sr-1, sc, newColor, oldColor);
if(sr+1<image.size() && image[sr+1][sc]==oldColor) fillRecur(image, sr+1, sc, newColor, oldColor);
if(sc-1>=0 && image[sr][sc-1]==oldColor) fillRecur(image, sr, sc-1, newColor, oldColor);
if(sc+1<image[0].size() && image[sr][sc+1]==oldColor) fillRecur(image, sr, sc+1, newColor, oldColor);
}
};
744.寻找比目标字母大的最小字母
/*
*可能测试数据量太小,二分法的效率还不如遍历
*/
class Solution {
public:
char nextGreatestLetter(vector<char>& letters, char target) {
for(auto c : letters){
if(c > target) return c;
}
return letters[0];
}
};
746.使用最小花费爬楼梯
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int f[1000] = {
0};
int n = cost.size();
f[0] = cost[0];
f[1] = cost[1];
for(int i =2; i < n; ++i){
f[i] = cost[i] + min(f[i-1],f[i-2]);
}
return f[n-1] < f[n-2] ? f[n-1] : f[n-2];
}
};
747.至少是其他数字两倍大的最大数
class Solution {
public:
int dominantIndex(vector<int>& nums) {
int n = nums.size();
int max = nums[0];
int index = 0;
for(int i=0;i<n;i++){
if(nums[i]>max)
{
max = nums[i];
index = i;
}
}
for(int i=0;i<n;i++){
if(i!=index && nums[index]<2*nums[i]){
return -1;
}
}
return index;
}
};
748.最短完整词
class Solution {
public:
string shortestCompletingWord(string licensePlate, vector<string>& words) {
map<char,int> letters;
int total = 0;
for(auto c : licensePlate){
if(c>='a'&&c<='z'){
letters[c]++;
total++;
}else if(c>='A'&&c<='Z'){
letters[c+32]++;
total++;
}
}
int slen = INT_MAX;
int n = words.size();
string res;
for(int i = 0; i < n; ++i){
map<char,int> t = letters;
int cnt = total;
for(auto c : words[i]){
if(--t[c]>=0)
cnt--;
}
if(cnt==0 && slen > words[i].size()){
slen = words[i].size();
res = words[i];
}
}
return res;
}
};
762.二进制表示中质数个计算置位
class Solution {
public:
int countPrimeSetBits(int L, int R) {
int res = 0;
for(int i = L; i <= R; ++i){
int n = 0,tmp=i;
while(tmp){
tmp &= tmp - 1;
n++;
}
if(isPrime(n)) res++;
}
return res;
}
bool isPrime(int n){
if(n==0 || n==1) return false;
for(int i = 2; i*i <= n; ++i){
if(n%i==0) return false;
}
return true;
}
};
766.托普利茨矩阵
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
if(matrix.size()==1 || matrix[0].size()==1) return true;
int row = matrix.size(), col = matrix[0].size();
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j){
if(i>0&&j>0 && matrix[i][j]!=matrix[i-1][j-1])
return false;
}
return true;
}
};
771.宝石与石头
class Solution {
public:
int numJewelsInStones(string J, string S) {
char bucket[128] = {
0};
int res = 0;
for(auto c : J){
bucket[c] = 1;
}
for(auto c : S){
if(bucket[c]==1)
res++;
}
return res;
}
};
783.二叉搜索树节点最小距离
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDiffInBST(TreeNode* root) {
vector<int> res;
func(res, root);
int minDiff = INT_MAX;
for(int i = 0; i < res.size()-1; ++i){
int diff = res[i+1] - res[i];
if(minDiff > diff) minDiff = diff;
}
return minDiff;
}
void func(vector<int>& res, TreeNode* root){
if(root){
func(res, root->left);
res.push_back(root->val);
func(res, root->right);
}
}
};
788.旋转数字
class Solution {
public:
int rotatedDigits(int N) {
int count = 0, posCount, mode, temp;
for(int i = 2; i <= N; ++i){
temp = i;
posCount = 0;
while(temp){
mode = temp % 10;
if(mode == 3 || mode == 4 || mode == 7){
break;
}else if(mode == 2 || mode == 5 || mode == 6 || mode == 9){
posCount++;
}
temp /= 10;
}
if(temp == 0 && posCount > 0) count++;
}
return count;
}
};
796.旋转字符串
class Solution {
public:
bool rotateString(string A, string B) {
if(A.length() != B.length()) return false;
A = A + A;
if(A.find(B)!= string::npos) return true;
return false;
}
};