C/C++代码优化的27条建议
摘要:这篇文章是极客上阅读量很大的一篇文章,通过博友的翻译,加上我的转载和小改动,以便经常查看,方便以后进行代码优化时拿来做参考。 1. 记住阿姆达尔定律 · funccost是函数func运行时间百分比,funcspeedup是你优化函数的运行的系数。 · 所以,如果...
阅读全文
LeetCode之longst Consecutive Sequence
摘要:题目:Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1...
阅读全文
LeetCode之Median of Two Sorted Arrays
摘要:题目:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overal...
阅读全文
LeetCode之Search in Rotated Sorted Array二
摘要:题目:Follow up for ”Search in Rotated Sorted Array”: What if duplicates are allowed? Would this affect the run-time complexity? How and wh...
阅读全文
LeetCode之Search in Rotated Sorted Array
摘要:题目:Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). ...
阅读全文
设计模式之单例模式(Singleton Pattern)C++实现
摘要:单例模式(Singleton Pattern):这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。 主要解决:解决一个全局使用的类,频繁地创建和销毁 缺点:没有接口,不能继承 使用场景:...
阅读全文
设计模式之抽象工程模式Abstract Factory Pattern()C++实现
摘要:抽象工厂模式(Abstract Factory Pattern):是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。 意图:提供一个创建一系列相关或相互依赖对象的接口...
阅读全文
设计模式之工厂模式(Factory Pattern)用C++实现
摘要:工厂模式(Factory Pattern):这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。它定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。 主要解决:主要解决接口选择的问题。 何时使用:我们明确地计划不同条件下创建不同实例时...
阅读全文
读书笔记之Effective C++ 1.让自己习惯C++
摘要:1.类的构造函数可以被声明为explicit,这可阻止它们被用来执行隐式类型转换(implicit type conversions),但是它们仍可被用来进行显式类型转换(explicit type conversions) class A{public: explicit A();} 2...
阅读全文
LeetCode之RemoveDuplicates扩展
摘要:题目:Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], ...
阅读全文
一致性哈希(Consistent Hashing)原理和实现(整理)
摘要:前言:对于一致性哈希已经不是罕见概念,在此只是对原有理论概念的一个整理和用自己的理解讲述,希望对新手有些许帮助,利人利己足矣。 1.概念 一致哈希是一种特殊的哈希算法。在使用一致哈希算法后,哈希表槽位数(大小)的改变平均只需要对 K/n 个关键字重新映射,其中 K是关键字的数量,n是槽位数量。然而在
阅读全文
LeetCode之Remove Duplicates from Sorted Array
摘要:描述 1.Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. 2.Do not allocate extra...
阅读全文