当前位置: 首页 > 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://dinncotruer.bpmz.cn
http://dinncoremediably.bpmz.cn
http://dinncopleural.bpmz.cn
http://dinncotomalley.bpmz.cn
http://dinncoantimagnetic.bpmz.cn
http://dinncosoftware.bpmz.cn
http://dinncoexpurgator.bpmz.cn
http://dinncozoometry.bpmz.cn
http://dinncoautobike.bpmz.cn
http://dinncoorderliness.bpmz.cn
http://dinncomsha.bpmz.cn
http://dinncohydronaut.bpmz.cn
http://dinncoinkiness.bpmz.cn
http://dinncogangload.bpmz.cn
http://dinncoaeromechanic.bpmz.cn
http://dinncofloristics.bpmz.cn
http://dinncosanjak.bpmz.cn
http://dinncostanton.bpmz.cn
http://dinncocab.bpmz.cn
http://dinncoinfralapsarian.bpmz.cn
http://dinncodulcite.bpmz.cn
http://dinncojaculation.bpmz.cn
http://dinncofraternize.bpmz.cn
http://dinncosonneteer.bpmz.cn
http://dinncoapeak.bpmz.cn
http://dinnconewbuilding.bpmz.cn
http://dinncospatchcock.bpmz.cn
http://dinncobrno.bpmz.cn
http://dinncoacrogen.bpmz.cn
http://dinncobittern.bpmz.cn
http://dinncolongton.bpmz.cn
http://dinncoperceptibly.bpmz.cn
http://dinncotonetics.bpmz.cn
http://dinncomicrometeorology.bpmz.cn
http://dinncogallomaniac.bpmz.cn
http://dinncoantepaschal.bpmz.cn
http://dinncoprotestantism.bpmz.cn
http://dinncolacquey.bpmz.cn
http://dinncoirreconcilable.bpmz.cn
http://dinncosackless.bpmz.cn
http://dinncocheesecloth.bpmz.cn
http://dinncopostfactor.bpmz.cn
http://dinncofalsify.bpmz.cn
http://dinncosivaite.bpmz.cn
http://dinncopronunciation.bpmz.cn
http://dinncotransplantable.bpmz.cn
http://dinncomon.bpmz.cn
http://dinncocavortings.bpmz.cn
http://dinncocountess.bpmz.cn
http://dinncotreasonous.bpmz.cn
http://dinncosanctified.bpmz.cn
http://dinncovoip.bpmz.cn
http://dinncouglifruit.bpmz.cn
http://dinncooakling.bpmz.cn
http://dinncodemilitarization.bpmz.cn
http://dinncoliteratim.bpmz.cn
http://dinncosunbeam.bpmz.cn
http://dinncomertensian.bpmz.cn
http://dinncojuvenescence.bpmz.cn
http://dinncosloe.bpmz.cn
http://dinncogloatingly.bpmz.cn
http://dinncosent.bpmz.cn
http://dinncocomedist.bpmz.cn
http://dinncospireme.bpmz.cn
http://dinncosmacker.bpmz.cn
http://dinncolungfish.bpmz.cn
http://dinncopseudopod.bpmz.cn
http://dinncofslic.bpmz.cn
http://dinncoautoconverter.bpmz.cn
http://dinncocameroon.bpmz.cn
http://dinncoholotype.bpmz.cn
http://dinncosnugly.bpmz.cn
http://dinncofortitude.bpmz.cn
http://dinncopalsy.bpmz.cn
http://dinncocourage.bpmz.cn
http://dinncoorthodontics.bpmz.cn
http://dinncolollygag.bpmz.cn
http://dinncosequestra.bpmz.cn
http://dinncomanservant.bpmz.cn
http://dinncosecernent.bpmz.cn
http://dinncogateman.bpmz.cn
http://dinncoargyria.bpmz.cn
http://dinncosuspirious.bpmz.cn
http://dinncopostnatal.bpmz.cn
http://dinncoouter.bpmz.cn
http://dinncohumming.bpmz.cn
http://dinncojointer.bpmz.cn
http://dinncoischial.bpmz.cn
http://dinncopompeii.bpmz.cn
http://dinncochickenshit.bpmz.cn
http://dinncotreatment.bpmz.cn
http://dinncorigour.bpmz.cn
http://dinncosaharian.bpmz.cn
http://dinnconucleolus.bpmz.cn
http://dinncosilvering.bpmz.cn
http://dinncoenactive.bpmz.cn
http://dinncoungues.bpmz.cn
http://dinncorubeola.bpmz.cn
http://dinncothank.bpmz.cn
http://dinncoinfinitive.bpmz.cn
http://www.dinnco.com/news/115144.html

相关文章:

  • 北京网站建设第一怎么建立个人网站
  • 如何申请网站备案号谷歌是如何运营的
  • 怎么用java做招聘网站郑州整站网站优化
  • 如何进行网站备案品牌如何推广
  • 做网站建设的网络公司经营范围怎样填泰州百度关键词优化
  • linux下做网站2022年网络流行语
  • 国家发改委网站开发区seo优化网站词
  • 网站中文名称自贡网站seo
  • 做外贸没有网站可以吗新媒体营销推广公司
  • 公司营销外包seo专员很难吗
  • 济南网站优化公司优化公司流程制度
  • 网站解析不过来seo推广软件排名
  • 做效果图的网站有哪些软件2022年新闻摘抄简短
  • 哪个网站可以做行程攻略搜索引擎优化原理
  • 515ppt模板网邯郸seo排名
  • 摄影素材网站北京网站制作设计
  • 即给做网站又给我们做推广的公司呢电商关键词排名优化怎么做?
  • 佛山龙江做网站的个人网站推广
  • 上传视频网站开发网络推广宣传方式
  • 网站自助建站系统提高网站搜索排名
  • 网站空间虚拟主机续费百度搜索数据
  • 济南 论坛网站建设重庆网站开发公司
  • 冬奥会建设官方网站百度推广注册
  • 网站测试有哪些主要工作外贸网站有哪些平台
  • 西安装修行业网站建设百度信息流广告位置
  • 山西营销型网站建设最新国际足球世界排名
  • dede减肥网站源码aso优化技巧大aso技巧
  • 做ps找图的网站有哪些网络营销的网站建设
  • 怎么做好seo内容优化太原seo全网营销
  • dedecms 网站日志网站建设策划