2021-01-13

451. 根据字符出现频率排序

给定一个字符串,请将字符串里的字符按照出现的频率降序排列。

示例 1:

输入:
“tree”

输出:
“eert”

解释:
'e’出现两次,'r’和’t’都只出现一次。
因此’e’必须出现在’r’和’t’之前。此外,"eetr"也是一个有效的答案。
示例 2:

输入:
“cccaaa”

输出:
“cccaaa”

解释:
'c’和’a’都出现三次。此外,"aaaccc"也是有效的答案。
注意"cacaca"是不正确的,因为相同的字母必须放在一起。
示例 3:

输入:
“Aabb”

输出:
“bbAa”

解释:
此外,"bbaA"也是一个有效的答案,但"Aabb"是不正确的。
注意’A’和’a’被认为是两种不同的字符。

//时间复杂度
class Solution {
public:
    string frequencySort(string s) {
    unordered_map<char,int> mp;
    for(int i=0;i<s.size();i++){
        mp[s[i]]++;
    }
    auto cmp=[&](auto &a,auto &b){
        return mp[a]>mp[b]||mp[a]==mp[b]&&a<b;
    };
    sort(s.begin(),s.end(),cmp);
    return s;
    }
};

时间复杂度优化

//当n越来越大的时候,时间复杂度为o(n),小于127的时候时间复杂度o(nlg(n))
class Solution {
public:
    string frequencySort(string s) {
    unordered_map<char,int> mp;
    for(int i=0;i<s.size();i++){
        mp[s[i]]++;
    }
    priority_queue<pair<int,char> >que;
    for(auto it:mp){
        que.push({it.second,it.first});
    }
    string s1;
    while(!que.empty()){
        auto it=que.top();
        que.pop();
        for(int i=0;i<it.first;i++){
            s1+=it.second;
        }
    }
    return s1;
    }
};

28. 实现 strStr()

实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。

示例 1:

输入: haystack = “hello”, needle = “ll”
输出: 2
示例 2:

输入: haystack = “aaaaa”, needle = “bba”
输出: -1
说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

class Solution {
public:
    int strStr(string haystack, string needle) {
          if(needle.size()==0) return 0;
          int i=0;
          int j=0;
          while(i<haystack.size()){
             if(i<haystack.size()&&haystack[i]==needle[j]){
                 i++;
                 j++;
                 if(j==needle.size()){
                     return i-j;
                 }
             }
             else{
                  i=i-j+1;
                 j=0;
             }
          }
          return -1;
    }
};

1018. 可被 5 整除的二进制前缀

给定由若干 0 和 1 组成的数组 A。我们定义 N_i:从 A[0] 到 A[i] 的第 i 个子数组被解释为一个二进制数(从最高有效位到最低有效位)。

返回布尔值列表 answer,只有当 N_i 可以被 5 整除时,答案 answer[i] 为 true,否则为 false。

示例 1:

输入:[0,1,1]
输出:[true,false,false]
解释:
输入数字为 0, 01, 011;也就是十进制中的 0, 1, 3 。只有第一个数可以被 5 整除,因此 answer[0] 为真。
示例 2:

输入:[1,1,1]
输出:[false,false,false]
示例 3:

输入:[0,1,1,1,1,1]
输出:[true,false,false,false,true,false]

思路:一次遍历,但需要注意的事项是如果数组太长,我们定义的类型在移动的过程中会越界,所以要在每一次移动的过程中,对结果进行求模,每次都余一下5,根据数学原理,结果是不会变的。

class Solution {
public:
    vector<bool> prefixesDivBy5(vector<int>& A) {
     vector<bool> vec;
     int temp=0;
     for(int i=0;i<A.size();i++){
       temp<<=1;
       temp+=A[i];
       if(temp%5==0){
           vec.push_back(true);
       }
       else{
           vec.push_back(false);
       }
       temp%=5;
     }
     return vec;
    }
};

88. 合并两个有序数组

给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组。

初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。你可以假设 nums1 有足够的空间(空间大小等于 m + n)来保存 nums2 中的元素。

示例 1:

输入:nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
输出:[1,2,2,3,5,6]
示例 2:

输入:nums1 = [1], m = 1, nums2 = [], n = 0
输出:[1]

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
     int r=m+n-1;

    m--;
    n--;
     while(m>=0&&n>=0){
         if(nums1[m]>nums2[n]){
             nums1[r]=nums1[m];
             r--;
             m--;
         }
         else if(nums1[m]<=nums2[n]){
             nums1[r]=nums2[n];
             n--;
             r--;
         }
     }
     while(n>=0){
         nums1[n]=nums2[n];
         n--;
     }
    }
};

#503. 独木舟

题目描述
​ 一群人去旅行,要租用独木舟,每艘独木舟最多乘两人,且所有独木舟有一个统一的载重限度。给出独木舟的载重限度和每个人的体重,现求最少需要租用多少独木舟。

输入
​ 第一行一个整数 w,表示独木舟的载重量。(80≤w≤200)
​ 第二行一个整数 n,表示旅游人数。 (1≤n≤30000)
​ 接下来 n 行,每行一个数表示 ai,即每个人的重量 (5≤ai≤w)
输出
​ 输出一个数表示最少需要的独木舟数量。

样例输入
100
9
90
20
20
30
50
60
70
80
90
样例输出
6
数据规模与约定
​ 时间限制:1 s

​ 内存限制:256 M

​ 100% 的数据保证 1≤n≤30000

#include<iostream>
using namespace std;
#include<algorithm>
int main() {
	int nums[30005];
	int w, n;
	cin >> w >> n;
	for (int i = 0; i < n; i++) {
		cin >> nums[i];
	}
	sort(nums, nums + n);
	int res = 0;
	int l = 0;
	int r = n - 1;
	while (l <=r) {
		int sum = nums[l] + nums[r];
		if (sum <=w) {
			res++;
			r--;
			l++;
		}
		else if (sum > w) {
			res++;
			r--;
		}
		
    }
	cout << res << endl;
 }

//若是不规定每个船的人数

#include<iostream>
using namespace std;
#include<algorithm>
int main() {
	int nums[30005];
	int w, n;
	cin >> w >> n;
	for (int i = 0; i < n; i++) {
		cin >> nums[i];
	}
	sort(nums, nums + n);
	int res = 0;
	int l = 0;
	int r = n - 1;
	while (l <=r) {
		int sum = nums[l] + nums[r];
		if (sum > w) {
			res++;
			r--;
		}
		else if (sum < w) {
			l++;
		}
		else {
			res++;
			r--;
			l++;
		}
    }
	cout << res << endl;
 }

#504. 删数

题目描述
​ 输入一个高精度的正整数 n(长度不大于 240 位),去掉其中任意 s 个数字后剩下的数字按原左右次序将组成一个新的正整数,现求一种方案,使得新的正整数数值最小。

输入
​ 第一行一个整数 n。

​ 第二行一个正整数 s。

输出
​ 输出一个数表示最小值,输出时忽略数字的前导零。

样例输入1
179566
4
样例输出1
15
样例输入2
903071
3
样例输出2
1

#include<iostream>
using namespace std;
#include<stack>
int main() {
	string res;
	cin >> res;
	int n;
	cin >> n;
	stack<char> st;
	for (int i = 0; i < res.size(); i++) {
		while (!st.empty() && st.top() > res[i]&&n) {
			st.pop();
			n--;
		}
		if (st.empty() && res[i] == '0') {
			continue;
		}
		st.push(res[i]);
	}
	while (!st.empty() && n) {
		st.pop();
		n--;
	}
	string item;
	while (!st.empty()) {
		item = st.top() + item;
		st.pop();
	}
	cout << item << endl;

}

1673. 找出最具竞争力的子序列

给你一个整数数组 nums 和一个正整数 k ,返回长度为 k 且最具 竞争力 的 nums 子序列。

数组的子序列是从数组中删除一些元素(可能不删除元素)得到的序列。

在子序列 a 和子序列 b 第一个不相同的位置上,如果 a 中的数字小于 b 中对应的数字,那么我们称子序列 a 比子序列 b(相同长度下)更具 竞争力 。 例如,[1,3,4] 比 [1,3,5] 更具竞争力,在第一个不相同的位置,也就是最后一个位置上, 4 小于 5 。

示例 1:

输入:nums = [3,5,2,6], k = 2
输出:[2,6]
解释:在所有可能的子序列集合 {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]} 中,[2,6] 最具竞争力。
示例 2:

输入:nums = [2,4,3,3,5,4,9,6], k = 4
输出:[2,3,3,4]

class Solution {  
public:
    vector<int> mostCompetitive(vector<int>& nums, int k) {
       vector<int> vec;
       int n=nums.size()-k;
       for(int i=0;i<nums.size();i++){
           while(vec.size()!=0&&n&&nums[i]<vec.back()){
               vec.pop_back();
               n--;
           }
           vec.push_back(nums[i]);
       }
    while(vec.size()!=0&&n){
        vec.pop_back();
        n--;
    }
    
    return vec;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值