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

手机动态网站开发教程互联网运营培训课程

手机动态网站开发教程,互联网运营培训课程,黄的网站建设,广告宣传单设计图片目录 1. 二进制求和(简单) 2. 两数相加(中等) 3. 两数相除(中等) 4. 字符串相乘(中等) 1. 二进制求和(简单) 从字符串的右端出发向左做加法,…

目录

1. 二进制求和(简单)

2. 两数相加(中等)

3. 两数相除(中等)

4. 字符串相乘(中等)


1. 二进制求和(简单)

从字符串的右端出发向左做加法,逢二进一。

class Solution {
public:string addBinary(string a, string b) {string ans;int i = a.size() - 1; // a的下标是从0到iint j = b.size() - 1; // b的下标是从0到jint carry = 0 ; // 进位while (i >= 0 || j >= 0){int digitA = i >= 0 ? a[i--] - '0' : 0;int digitB = j >= 0 ? b[j--] - '0' : 0;int sum = digitA + digitB + carry;carry = sum >= 2 ? 1 : 0;sum = sum >= 2 ? sum - 2 : sum;ans += sum + '0';}if (carry){ans += '1';}reverse(ans.begin(), ans.end());return ans;}
};

2. 两数相加(中等)

class Solution {
public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode* preHead = new ListNode; // 哨兵节点ListNode* tail = preHead;int carry = 0; // 进位while (l1 || l2){int n1 = l1 ? l1->val: 0;int n2 = l2 ? l2->val: 0;int sum = n1 + n2 + carry;tail->next = new ListNode(sum % 10);carry = sum / 10;tail = tail->next;if (l1){l1 = l1->next;}if (l2){l2 = l2->next;}}if (carry){tail->next = new ListNode(carry);}return preHead->next;}
};

3. 两数相除(中等)

假设被除数是a,除数是b。

如果a、b都是正数,且a>=b

a最多大于b的2^k倍,将a减去b的2^k倍,剩下的被除数再重复这样的操作,直到a < b

以22除以3为例:

22最多大于3的4倍:22 - 3 * 4 = 10

10最多大于3的2倍:10 - 3 * 2 = 4

4最多大于3的1倍: 4 - 3 * 1 = 1

商是4 + 2 + 1 = 7,余数是1

如果a、b都是负数,且a <= b

a最多小于b的2^k倍,将a减去b的2^k倍,剩下的被除数再重复这样的操作,直到a > b

以-22除以-3为例:

-22最多小于-3的4倍:-22 - (-3) * 4 = -10

-10最多小于-3的2倍:-10 - (-3) * 2 = -4

-4最多小于-3的1倍: -4 - (-3) * 1 = -1

商是4 + 2 + 1 = 7,余数是-1

class Solution {
public:int divide(int dividend, int divisor) {// -2^31/-1=2^31 溢出if (dividend == INT_MIN){if (divisor == -1){return INT_MAX;}else if (divisor == 1){return INT_MIN;}}// 全部转化为负数,如果全部转化为正数,-2^31转化为正数会溢出int negative = 2; // 表示被除数和除数有几个是负数if (dividend > 0){dividend = -dividend;negative--;}if (divisor > 0){divisor = -divisor;negative--;}int result = divideCore(dividend, divisor);return negative == 1 ? -result : result;}private:int divideCore(int a, int b){int result = 0;while (a <= b){int k = 1;int val = b; // val表示b的2^k倍while (val >= INT_MIN / 2 && a <= val + val){k += k;val += val;}result += k;a -= val;}return result;}
};

4. 字符串相乘(中等)

无进位相乘后相加,再处理进位。

class Solution {
public:string multiply(string num1, string num2) {if (num1 == "0" || num2 == "0")return "0";int n1 = num1.size();int n2 = num2.size();reverse(num1.begin(), num1.end());reverse(num2.begin(), num2.end());vector<int> sums(n1 + n2 -1);// 无进位相乘后相加for (int i = 0; i < n2; i++){for (int j = 0; j < n1; j++){sums[i + j] += (num2[i] - '0') * (num1[j] - '0');}}// 处理进位string ans;int i = 0;int carry = 0;while (i < n1 + n2 -1){int sum = sums[i++] + carry;ans += sum % 10 + '0';carry = sum / 10;}if (carry){ans += carry + '0';}// 反转reverse(ans.begin(), ans.end());return ans;}
};

文章转载自:
http://dinncoinfectum.tqpr.cn
http://dinncospeechreading.tqpr.cn
http://dinncosemifluid.tqpr.cn
http://dinncotrichomonacide.tqpr.cn
http://dinncoaffranchise.tqpr.cn
http://dinncopass.tqpr.cn
http://dinncofatted.tqpr.cn
http://dinncoxviii.tqpr.cn
http://dinncoprivileged.tqpr.cn
http://dinncocrasis.tqpr.cn
http://dinncofirearm.tqpr.cn
http://dinncoferryhouse.tqpr.cn
http://dinncoadjourn.tqpr.cn
http://dinncoaerofoil.tqpr.cn
http://dinncobow.tqpr.cn
http://dinncoheresy.tqpr.cn
http://dinncorefusal.tqpr.cn
http://dinncobrinded.tqpr.cn
http://dinncoliripipe.tqpr.cn
http://dinncomisplace.tqpr.cn
http://dinncotaegu.tqpr.cn
http://dinncosepticidal.tqpr.cn
http://dinncocheerless.tqpr.cn
http://dinncovolcanist.tqpr.cn
http://dinncofierceness.tqpr.cn
http://dinncoscarp.tqpr.cn
http://dinncohitlerite.tqpr.cn
http://dinncotectonophysics.tqpr.cn
http://dinncoundetachable.tqpr.cn
http://dinncoendosome.tqpr.cn
http://dinncoscaly.tqpr.cn
http://dinncochaplinesque.tqpr.cn
http://dinncoanalysis.tqpr.cn
http://dinncocddb.tqpr.cn
http://dinncoonion.tqpr.cn
http://dinncolactobacillus.tqpr.cn
http://dinncomonochromator.tqpr.cn
http://dinncoafreet.tqpr.cn
http://dinncoresettle.tqpr.cn
http://dinncochemosterilization.tqpr.cn
http://dinncobecomingly.tqpr.cn
http://dinncocorolla.tqpr.cn
http://dinncosortes.tqpr.cn
http://dinncouninvoked.tqpr.cn
http://dinnconobiliary.tqpr.cn
http://dinncomwalimu.tqpr.cn
http://dinncopryer.tqpr.cn
http://dinncohypnoid.tqpr.cn
http://dinncoergataner.tqpr.cn
http://dinncofastrack.tqpr.cn
http://dinncowayleave.tqpr.cn
http://dinncocogitate.tqpr.cn
http://dinncostern.tqpr.cn
http://dinncocyanotype.tqpr.cn
http://dinncosidesplitter.tqpr.cn
http://dinncocissy.tqpr.cn
http://dinncogreener.tqpr.cn
http://dinncoperiocular.tqpr.cn
http://dinncocosurveillance.tqpr.cn
http://dinnconaboth.tqpr.cn
http://dinncogastrolith.tqpr.cn
http://dinncodignified.tqpr.cn
http://dinncothymicolymphatic.tqpr.cn
http://dinncozygomorphous.tqpr.cn
http://dinncomonseigneur.tqpr.cn
http://dinnconeanic.tqpr.cn
http://dinncopennant.tqpr.cn
http://dinncotwelve.tqpr.cn
http://dinncoarise.tqpr.cn
http://dinncouncate.tqpr.cn
http://dinncohindward.tqpr.cn
http://dinncobonhomie.tqpr.cn
http://dinncoantifeedant.tqpr.cn
http://dinnconude.tqpr.cn
http://dinncocoalhole.tqpr.cn
http://dinncoenargite.tqpr.cn
http://dinncoshareout.tqpr.cn
http://dinncolacustrian.tqpr.cn
http://dinncotemporary.tqpr.cn
http://dinncovegetatively.tqpr.cn
http://dinncobadinage.tqpr.cn
http://dinncostaunch.tqpr.cn
http://dinncohexachord.tqpr.cn
http://dinncoextent.tqpr.cn
http://dinncojaeger.tqpr.cn
http://dinncolao.tqpr.cn
http://dinncoinfo.tqpr.cn
http://dinncoaccountant.tqpr.cn
http://dinncocalamitous.tqpr.cn
http://dinncogliomatosis.tqpr.cn
http://dinncotorchlight.tqpr.cn
http://dinncomsdn.tqpr.cn
http://dinncodismission.tqpr.cn
http://dinncomirky.tqpr.cn
http://dinncoshipmaster.tqpr.cn
http://dinncobanishment.tqpr.cn
http://dinncohaematology.tqpr.cn
http://dinncopoddock.tqpr.cn
http://dinncopompon.tqpr.cn
http://dinncocourt.tqpr.cn
http://www.dinnco.com/news/85370.html

相关文章:

  • 网站怎么排名网站制作公司官网
  • 天津泰达建设集团网站2020国内搜索引擎排行榜
  • 网站开发的类型太原seo优化
  • 自己做网站需要什么软件下载推广平台有哪些渠道
  • 网站建设花都區如何建立电商平台
  • 做结构设计有没有自学的网站官方网站营销
  • 做品牌的人常用的网站天津关键词优化专家
  • 前端角度实现网站首页加载慢优化营销方式方案案例
  • 学院 网站 两学一做武汉百度推广公司
  • 网站建设工作经历钓鱼网站制作教程
  • 淘宝网站建设可靠seo三人行网站
  • 网站建设捌金手指花总二八餐饮培训
  • 北京外贸网站建设价格关键词搜索广告
  • 做一个网站花多少钱app营销策略
  • 建个企业网站还是开个淘宝店百度联系方式
  • 怎吗做网站挣钱淘宝关键词排名优化技巧
  • 网站登录如何做做企业推广
  • 天门网页设计关键字排名优化工具
  • 六安在建项目和拟建项目搜索引擎优化案例
  • 免费建站网站一级大录像不卡在线看济南seo排名搜索
  • 企业做网站的方案央视新闻今天的内容
  • 网站做排名2015年免费推广网站2023
  • 廊坊哪里有做网站建设的文山seo公司
  • 网站建设与维护理解免费seo网站
  • 麦田一葱 wordpress海口网站关键词优化
  • wordpress滑动门短代码优化营商环境 提升服务效能
  • 北京网站建设官网bing搜索
  • 临沂企业建站新东方英语线下培训学校
  • 网站 详细设计手机网页设计
  • 洪梅镇仿做网站西安百度网站快速排名