• RPC、算法题等知识学习总结


    问题汇总

    1. 写RPC实现的框架和解决的问题?

    1)跟语言平台绑定的开源 RPC 框架主要有下面几种:

    • Dubbo:国内最早开源的 RPC 框架,由阿里巴巴公司开发并于 2011 年末对外开源,仅支持 Java 语言。
    • Motan:微博内部使用的 RPC 框架,于 2016 年对外开源,仅支持 Java 语言。
    • Tars:腾讯内部使用的 RPC 框架,于 2017 年对外开源,仅支持 C++ 语言。
    • Spring Cloud:国外 Pivotal 公司 2014 年对外开源的 RPC 框架,仅支持 Java 语言

    2)而跨语言平台的开源 RPC 框架主要有以下几种:

    • gRPC:Google 于 2015 年对外开源的跨语言 RPC 框架,支持多种语言。
    • Thrift:最初是由 Facebook 开发的内部系统跨语言的 RPC 框架,2007 年贡献给了 Apache 基金,成为 Apache 开源项目之一,支持多种语言。

    3)解决哪些问题?

    • PC 本身是远程过程调用,主要解决远程的通信问题,而不仅是封装原始的数据通信协议与网络协议。
    • 比如两台服务器 A、B,一个应用部署在 A 服务器上,想要调用 B 服务器上应用提供的函数或者方法,由于不在一个内存空间,则不能直接调用,这时候就可以应用 RPC 框架的实现来解决。

    2. LeetCode 198 打家劫舍

    大疆的无人机可以在一排的物品中取物品,但是取一个后需要冷却取一个的时间,问对于输入的数组,最多能拿到多少价值的物品?

    状态表示:
    f(i):在1~i之中选且必选i的所有方案中最大值
    g(i):在1~i之中选且必不选i的所有方案中最大值
    状态计算:
    f(i) = g(i-1) + wi
    g(i) = max(f(i-1), g(i-1))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    class Solution {
    public:
        int rob(vector<int>& nums) {
    
            int n = nums.size();
            vector<int> f(n + 1), g(n + 1);
            for (int i = 1; i <= n ; i ++) { //i<=n,f,g从1~n
    
                f[i] = g[i - 1] + nums[i - 1];
                g[i] = max(f[i - 1], g[i - 1]); 
            }
    
            return max(f[n], g[n]);
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3. LeetCode 1065 字符串的索引对

    示例 1:
    输入: text = "thestoryofleetcodeandme", 
    		words = ["story","fleet","leetcode"]
    输出: [[3,7],[9,13],[10,17]]
    
    示例 2:
    输入: text = "ababa", words = ["aba","ab"]
    输出: [[0,1],[0,2],[2,3],[2,4]]
    解释: 
    注意,返回的配对可以有交叉,比如,"aba" 既在 [0,2] 中也在 [2,4] 中
     
    提示:
    所有字符串都只包含小写字母。
    保证 words 中的字符串无重复。
    1 <= text.length <= 100
    1 <= words.length <= 20
    1 <= words[i].length <= 50
    按序返回索引对 [i,j](即,按照索引对的第一个索引进行排序,
    				当第一个索引对相同时按照第二个索引对排序)。
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    
    class Solution {
    public:
        vector<vector<int>> indexPairs(string text, vector<string>& words) {
        	int i, len, maxlen = 0;
        	unordered_set<string> s;
        	for(i = 0; i < words.size(); ++i)
        	{
        		s.insert(words[i]);
        		maxlen = max(maxlen, (int)words[i].size());
        	}
        	vector<vector<int>> ans;
        	for(i = 0; i < text.size(); ++i)
        	{
        		for(len = 1; len <= maxlen && i+len-1 < text.size(); ++len)
        		{
        			if(s.find(text.substr(i,len))!=s.end())
        				ans.push_back({i,i+len-1});
        		}
        	}
        	return ans;
        }
    };
    //利用gdb调试
    int main() {
        
        
        vector<string> words;
        words.push_back("story"), words.push_back("fleet"), words.push_back("leetcode");
        string text = "thestoryofleetcodeandme";
        
        Solution s;
        vector<vector<int>> ans = s.indexPairs(text, words);
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    4. leet code45 跳跃游戏 II

    在这里插入图片描述

    dp + 贪心
    状态表示:f(i):到达i的最少步数
    状态转移:f(i)=f(last)+1;
    
    • 1
    • 2
    • 3
    class Solution {
    public:
        int jump(vector<int>& nums) {
            
            const int n = nums.size();
            vector<int> f(n + 1);
            
            for (int i = 1, last = 0; i < n; i ++) {
                while (last + nums[last] < i) last ++;
                f[i] = f[last] + 1;
            }
    
            return f[n - 1];
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    充电站
    推荐一个零声学院免费公开课程,个人觉得老师讲得不错,分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,立即学习

  • 相关阅读:
    jmeter监听每秒点击数(Hits per Second)
    由山东高考今日出分记
    Python入门教程36:urllib网页请求模块的用法
    Lua学习笔记:探究package
    Advances in Graph Neural Networks笔记5:Dynamic Graph Neural Networks
    C/C++航空客运订票系统
    修正TiKnob的指示箭头显示问题
    排序算法总结-C++实现
    Java环境搭建&安装IDE
    Acwing算法提高课——背包问题求具体方案
  • 原文地址:https://blog.csdn.net/weixin_53492721/article/details/126284398