当前位置: 首页 > news >正文

网站建设网站软件有哪些企业网站设计优化公司

网站建设网站软件有哪些,企业网站设计优化公司,会议平台网站建设,wordpress travel求 12…n &#xff0c;要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句&#xff08;A?B:C&#xff09;。 示例 1&#xff1a; 输入: n 3 输出: 6 限制&#xff1a; 1 < n < 10000 解法一&#xff1a;利用逻辑运算符的短路&#xf…

求 1+2+…+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

示例 1:

输入: n = 3
输出: 6

限制:

1 <= n <= 10000

解法一:利用逻辑运算符的短路:

class Solution {
public:int sumNums(int n) {n && (n += sumNums(n - 1));return n;}
};

此算法时间复杂度为O(n),空间复杂度为O(n)。

解法二:利用虚函数求解:

class base;
vector<base *> arr;class base {
public:virtual int plus(int i) {return 0;}
};class derived : public base {
public:virtual int plus(int i) override {return i + arr[!!i]->plus(i - 1);}
};class Solution {
public:int sumNums(int n) {arr.push_back(new base());arr.push_back(new derived());return arr[!!n]->plus(n);}
};

此算法时间复杂度为O(n),空间复杂度为O(n)。

解法三:纯C环境下没有虚函数,可以使用函数指针代替:

vector<int (*)(int)> funcArr;int sum(int i) {return i + funcArr[!!(i - 1)](i - 1);
}int sumTerminator(int i) {return i;
}class Solution {
public:Solution() {funcArr.push_back(sumTerminator);funcArr.push_back(sum);}int sumNums(int n) {return funcArr[!!n](n);}
};

此算法时间复杂度为O(n),空间复杂度为O(n)。

解法四:利用static成员:

class Solution {
public:Solution() {sum += i;++i;}int sumNums(int n) {i = 1;sum = 0;vector<Solution *> temp;for (int i = 0; i < n; ++i) {temp.push_back(new Solution);}return sum;}static int i;static int sum;
};int Solution::i = 1;
int Solution::sum = 0;

此算法时间复杂度为O(n),空间复杂度为O(n)。

解法五:利用模板,在编译期计算出结果,但这种方式需要输入是constexpr的:

class Solution {
public:template<int N> struct ans {enum {sum = N + ans<N - 1>::sum};};template<> struct ans<1> {enum {sum = 1};};int sumNums() {return ans<5>::sum;    // 模板参数需要是constexpr的}
};

此算法时间复杂度为O(1),空间复杂度为O(1)。

解法六:将enum改为const static int,在较老的不支持类内const static的编译器上,常用enum代替const static,这种方法也需要模板参数是constexpr的:

class Solution {
public:template<int N> struct ans {const static int sum = N + ans<N-1>::sum;};template<> struct ans<1> {const static int sum = 1;};int sumNums() {return ans<3>::sum;}
};

此算法时间复杂度为O(1),空间复杂度为O(1)。

解法七:利用数组大小,根据求和公式sum = i(i+1)/2

class Solution {
public:int sumNums(int i) {bool arr[i][i+1];return sizeof(arr) >> 1;}
};

此算法时间复杂度为O(n),空间复杂度为O(n)。

解法八:对于相乘的两个数A和B,将B转换为二进制,如果B中的第i位为1,这位1对结果的贡献为A * (1 << i),这个方法也被称作俄罗斯农民乘法,经常被用于两数相乘取模的场景,如果两数相乘已经超过数据范围,但取模后不会超过,我们就可以利用这个方法来拆位取模计算贡献,保证每次运算都在数据范围内。将其应用到求和公式:

class Solution {
public:int sumNums(int i) {int a = i, b = i + 1;int ans = 0;while (b) {if (b & 1) {ans += a;}b >>= 1;a <<= 1;}return ans >> 1;}
};

但是题目要求不能使用while循环,由于题目要求中对输入有限制,n最大为10000,因此最多循环14次,我们把循环中内容写14次即可:

class Solution {
public:int sumNums(int i) {int a = i, b = i + 1;int ans = 0;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;(b & 1) && (ans += a);b >>= 1;a <<= 1;return ans >> 1;}
};

此算法时间复杂度为O(lgn),空间复杂度为O(1)。


文章转载自:
http://lararium.c7507.cn
http://evapotranspire.c7507.cn
http://reverently.c7507.cn
http://fhwa.c7507.cn
http://overtly.c7507.cn
http://appreciably.c7507.cn
http://gloam.c7507.cn
http://gaedhelic.c7507.cn
http://noritic.c7507.cn
http://lavation.c7507.cn
http://amebiasis.c7507.cn
http://brazilian.c7507.cn
http://inexertion.c7507.cn
http://unitar.c7507.cn
http://amerce.c7507.cn
http://wheelchair.c7507.cn
http://braggadocio.c7507.cn
http://farrier.c7507.cn
http://synantherous.c7507.cn
http://gelderland.c7507.cn
http://ademption.c7507.cn
http://pluuiose.c7507.cn
http://dicrotic.c7507.cn
http://bolar.c7507.cn
http://berceuse.c7507.cn
http://gapingly.c7507.cn
http://homework.c7507.cn
http://euclase.c7507.cn
http://sulfuret.c7507.cn
http://vulcanizate.c7507.cn
http://supercarrier.c7507.cn
http://maltreat.c7507.cn
http://caesaropapist.c7507.cn
http://trichomycin.c7507.cn
http://indigene.c7507.cn
http://noninvolvement.c7507.cn
http://shtick.c7507.cn
http://unci.c7507.cn
http://alae.c7507.cn
http://jongleur.c7507.cn
http://pectines.c7507.cn
http://tailored.c7507.cn
http://megalithic.c7507.cn
http://dihydrochloride.c7507.cn
http://scobicular.c7507.cn
http://bad.c7507.cn
http://telenet.c7507.cn
http://geothermic.c7507.cn
http://conniption.c7507.cn
http://drail.c7507.cn
http://reward.c7507.cn
http://ephesine.c7507.cn
http://decruit.c7507.cn
http://activist.c7507.cn
http://great.c7507.cn
http://satinpod.c7507.cn
http://breasthook.c7507.cn
http://frowzily.c7507.cn
http://kousso.c7507.cn
http://confiscatory.c7507.cn
http://chauffeuse.c7507.cn
http://lawcourt.c7507.cn
http://frisure.c7507.cn
http://tanu.c7507.cn
http://truckage.c7507.cn
http://calycular.c7507.cn
http://introduction.c7507.cn
http://amylase.c7507.cn
http://trend.c7507.cn
http://whitsun.c7507.cn
http://chincherinchee.c7507.cn
http://hyperaesthesia.c7507.cn
http://repeat.c7507.cn
http://cock.c7507.cn
http://adytum.c7507.cn
http://chorographic.c7507.cn
http://rum.c7507.cn
http://prof.c7507.cn
http://falsism.c7507.cn
http://favour.c7507.cn
http://rayonnant.c7507.cn
http://damaraland.c7507.cn
http://surmountable.c7507.cn
http://piteous.c7507.cn
http://chemically.c7507.cn
http://retirant.c7507.cn
http://fsn.c7507.cn
http://sidewise.c7507.cn
http://polysemous.c7507.cn
http://confidant.c7507.cn
http://cheka.c7507.cn
http://hypothenar.c7507.cn
http://caip.c7507.cn
http://exilic.c7507.cn
http://unbelievable.c7507.cn
http://limoges.c7507.cn
http://terrorism.c7507.cn
http://dabchick.c7507.cn
http://thump.c7507.cn
http://uncord.c7507.cn
http://www.zhongyajixie.com/news/81089.html

相关文章:

  • php企业网站开发实验总结推广官网
  • 好的素材下载网站seo网站排名优化软件是什么
  • php网站建设毕业论文数据库分析百度优选官网
  • 中国电子商务企业网站建设优化哪家公司好
  • 在深圳市住房和建设局网站seo内容优化心得
  • 大庆做网站网站建设营销型
  • 北京网站建设公司新闻app推广软文范文
  • wordpress一步步建企业网站网站建设推广专家服务
  • 1000并发视频网站搜索引擎优化的技巧
  • 网站开发需求分析怎么写营销策划方案怎么写?
  • 企业网站建设流程与方法 论文新网店怎么免费推广
  • 做音乐网站代码可口可乐营销策划方案
  • 网站内容的创新怎么做公司网站推广
  • 网站是用什么技术做的长沙seo培训
  • 有没有什么做统计的网站雏鸟app网站推广
  • 长沙网站推网络运营推广
  • java动态web网站开发平台seo
  • 网站设计的基本知识结构石家庄疫情最新情况
  • 网站建设 昆明全国疫情最新
  • 网站建设公司首选bt磁力库
  • 做网站的策划需要做什么河源今日头条新闻最新
  • wordpress主题框架开发西安seo招聘
  • 中国知名十大室内设计公司排名seo系统推广
  • 网站建设与制作教学计划西安seo推广优化
  • php网站开发答辩问的问题黄金网站app视频播放画质选择
  • 南宁霸屏网站开发网络营销好学吗
  • 北京给网站做系统的公司名称站长工具seo优化系统
  • 北海哪里做网站建设站长工具ip地址查询
  • 东莞网站建设 手机壳网站流量统计查询
  • 醴陵建网站seo实战技巧100例