Q5.3 Print the next largest and smallest number。 二进制表示中1的个数统计

海康视觉算法平台VisionMaster 4.3.0 C# 二次开发01 加载方案并获取结果 第一次使用海康视觉算法平台VisionMaster 4.3.0,项目中要使用这个平台进行视觉处理并获取结果。 阅读详情

Q:Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation.

A:

思路1:暴力搜索。统计数字的二进制表示中1的位数。统计方法参考http://blog.youkuaiyun.com/steedhorse/article/details/4809493,总结的很详细。其中第二种方法看了很久,总算是有点想通了,接下来主要分析一下第二种统计方法。

对于单个比特位来说,其状态与其表示的十进制数值相同,即二进制0代表的数字是0, 二进制1代表的数字是1。但是不是单个比特位就不是这样了,例如二进制11表示的是3,并不是2(由1+1得到)。

以八个比特位为例:x = 01100111

1)

x1 = x&(0x55) = x&(01010101) = 01000101  即统计奇数位上1的个数

x2 = (x>>1)&(0x55) = (x>>1)&(01010101) = 00010001 即统计偶数位上1的个数

x = x1 + x2 = 01010110   单比特加法  此时 x 实际上为2比特块,即 [01] [01] [01] [10] ,其结果为1+1+1+2 = 5,可以看到结果正是x起始值的时候二进制表示中1的位数

2)通过上面就可以知道,鉴于x起始为8比特,所以我们的目的就是找到x的8比特块。和第一步同样的方法,对2比特块做处理

这一步x是第一步的结果 01010110

x = x&(00110011) + (x>>2)&(00110011) = 00100011   此时x为4比特块 [0010] [0011] 结果为2+3 = 5

3)接下来,同样的操作

x = x&(00001111) + (x>>4)&(00001111) = 00000101  此时x为8比特块,得到最后结果,直接x值,为5

所以,对于32bit的int型来说,其统计一个数二进制表示中1的位数的方法如下:

int count_one(int x){
    x = (x & (0x55555555)) + ((x >> 1) & (0x55555555));
    x = (x & (0x33333333)) + ((x >> 2) & (0x33333333));
    x = (x & (0x0f0f0f0f)) + ((x >> 4) & (0x0f0f0f0f));
    x = (x & (0x00ff00ff)) + ((x >> 8) & (0x00ff00ff));
    x = (x & (0x0000ffff)) + ((x >> 16) & (0x0000ffff));
    return x;
}

当然上面给的参考链接里面,还有更快点的方法,主要是因为这个方法自己琢磨了半天,所以特此记录一下。


思路2:
怎么找到更大的一个数,但是二进制表示中1的个数又相同呢?  
观察二进制表示可以发现,想要找到符合要求的数,必须在比特位为1的位置进行加法,即要有加法的进位,才能满足要求。例如, x  = 00111000,想要找到符合要求的数,首先x 先要加上00001000, 如果不这样,在后三位怎么操作,得到的数1的个数都不可能与x相同。 加上00001000之后,统计一下x和 加和结果中1的个数,计算出差值n, 将加和结果的后n位置1,即是满足要求的数。
找小的数是同样的操作。
#include <iostream>
using namespace std;

int countOne(int x) {
	int cnt = 0;
	while (x) {
		x &= (x-1);
		cnt++;
	}
	return cnt;
}

int next(int x) {
	int x2 = x;
	int bit = 0;
	for (; bit < 32 && (x&1) != 1; bit++, x >>= 1);
	for (; bit < 32 && (x&1) != 0; bit++, x >>= 1);
	if (bit == 31) {
		return -1;
	}
	x |= 1;
	x <<= bit;
	if(bit == 32) x = 0;
	int n = countOne(x2)-countOne(x);
	int m = 1;
	for ( ; n > 0; x |= m, n--, m <<= 1);
	return x;
}

int prev(int x) {
	int x2 = x;
	int bit = 0;
	for (; bit < 32 && (x&1) != 0; bit++, x >>= 1);
	for (; bit < 32 && (x&1) != 1; bit++, x >>= 1);
	if (bit == 31) {
		return -1;
	}
	x -= 1;
	x <<= bit;
	if(bit == 32) x = 0;
	int n = countOne(x2)-countOne(x);
	x >>= bit;
	for ( ; n > 0; n--, x = (x<<1)|1, bit--);
	x <<= bit;
	return x;
}

int main() {
	int a = -976756;//(1<<31)+(1<<29);//-8737776;
    cout<<next(a)<<" "<<prev(a)<<endl; // very slow
    return 0;
}

 

K8s(二十二):k8s集群如何部署一个Spring Boot项目(依赖于MySQL和Redis外部服务) 🔴 K8s(二十二):k8s集群如何部署一个Spring Boot项目(依赖于MySQL和Redis外部服务) 阅读详情

相关推荐

GoDaddy域名解析 到 阿里云域名DNS

一:登录GODaddy,点击“个人中心”,然后点击“管理域名” 二:域名解析 第一种:直接解析到对应的服务器 添加一个,类型默认A,主机填写二级域名前缀(image.主域名),指向填写目标服务器ip即可(注意目标服务器80端口是否开放) 第二种:将主域名dns转到阿里云域名下 三:若要修给为阿里云解析服务器,要做以下修改,点击“更改” 点击更改后出现如下页面,此时需要到阿里云官网云解析DNS下添加域名解析 四:阿里云添加域名解析...

xiaolinzi176的博客 6705

[CrackCode] 5.3 Print the next smallest and next largest number

Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation  ================ Analysis: Assume that we are given 1

伪技回忆录 977

怎么选?一文读懂12种SPC控制图,从基础到进阶,再到Python代码实现

本文系统介绍了12种常用SPC控制图的应用场景和特点,包括计量型(Xbar-R图、Xbar-s图、I-MR图等)、计数型(P图、NP图、U图等)及进阶控制图(EWMA图、CUSUM图、多元控制图)。文章强调控制图是SPC的核心工具,能有效区分过程变异类型,实现"预防优于检测"的质量管理目标。通过数据示例和Python代码展示了各类控制图的实现方法,为质量管理人员提供了实用参考。

m0_56592100的博客 4148

经典算法题目:Cracking the coding interview 问题与解答

Cracking the coding interview--问题与解答 March 14, 2013 作者:Hawstein 出处:http://hawstein.com/posts/ctci-solutions-contents.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注

我的专栏 7584

统信UOS/麒麟ARM桌面版,不联网也能用Docker部署MySQL 5.7(离线部署详解)

本文详细介绍了在统信UOS/银河麒麟ARM桌面版系统中,如何离线部署MySQL 5.7的完整流程。通过Docker技术实现环境隔离和依赖封装,解决了封闭网络环境下的部署难题,包括资源准备、系统配置、容器化部署及安全加固等关键步骤,为政企和科研场景提供实用解决方案。

weixin_30892037的博客 230

移动通信基础(13)TDL、CDL、信道相关性

参考《U型槽无线信道多径传播特性测量与建模方法的研究》、《5G信号源中的信道模拟与仿真研究》衰落衰落理解为信号幅度在时间和频率上的不断波动,是无线信道一个重要的恶化特性。信道幅度恶化的两个主要来源分别是加性噪声和信号衰落,高斯白噪声信道模型就是典型的加性噪声,信号衰落引起的是乘性的信号扰动,与加性噪声从原理上完全不同。信道的频率选择性衰落组为无线信道的重要特征之一,是人们进行无线系统设计考虑的重要因素,决定了导频隔间的大小。...

tybAly的博客 2万+

5.3-比x大的数中最小和比x小的数中最大

转自http://hawstein.com/posts/5.3.html 题目 原文: Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation. 译文: 给定一

oooooops 1382

数据可视化—power bi 基础操作汇总

数据来源: https://pan.baidu.com/s/1U_Y7rKfhnSKbLmh4FcUD_Q 提取码: yq5v 目录 基本功能与视图 编辑与查询—修改column案例 建立关联性 新增column与DAX完成总价运算 习题练习1:建立可视化图表 产品销售统计图 column拖曳可视化 1.基本功能与视图 ①基础操作界面 ②获取数据—以打开excel为例 ③数据文件基本视图 可视化形式:柱状图、数据、关联性 2.编辑与查询—修改column ①编辑与查询—数据处理界面 .

weixin_52730784的博客 2683

面试题-位操作

如何准备: Bit manipulation can be a scary thing to many candidates, but it doesn’t need to be! If you’re shaky on bit manipulation, we recommend doing a couple of arithmetic-like problems to boost your s

coder_xia的专栏 1951

YOLOv5/v7/v8/v9/v10/v11/v12/13目标检测结果解析,超w字精读,IoU、GIoU、DIoU、CIoU、EIoU详细解释,精确率、召回率、AP、mAP与P-R 曲线图深度解析

YOLOv5/v7/v8/v9/v10/v11/v12/13目标检测结果解析,超w字精读,IoU、GIoU、DIoU、CIoU、EIoU详细解释,精确率、召回率、AP、mAP与P-R 曲线图深度解析

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 1414

学 Simulink——基于递推最小二乘法(RLS)的工业电机转动惯量 J在线辨识仿真

Te 估算 = 1.5·p·(ψ·iq + (Ld-Lq)·id·iq)回归量:φ(k)=−ω˙(k), y(k)=Te​(k), θ=J。仿真:电机加速 0→1500rpm,观测 J^(k)→Jnom​。(可选) Jtrue​切 0.04(双惯量负载)→ 看跟踪。电机机械运动方程:Te​−TL​=J⋅ω˙+B⋅ω。J^(0)=0.005→收敛至0.02kg⋅m2✔。Te​:电磁转矩(Nm),已知(来自电流环输出)仅需 Te​,ω(驱动器已有)→ 无额外传感 ✔。

amy_mhd的博客 45

Next Number java 走地牙CTCI 5.4

/* Next Number: Given a positive integer, print the next smallest and the last largest number that have the same number of 1 bits in their binary representation. 方法是:1:最小下一个:11011001111100 相同数量的1的情况下...

yangzhuhappy的博客 236

Xilinx平台SRIO介绍(二)SRIO IP核基础知识

使用SRIO IP核必须掌握的基础知识! 理解了这篇,剩下的只是代码罢了。

数字IC/FPGA杂货铺,找一找,或许有你想要的~ 4万+

Q9.3 Find the magic index

Q: A magic index in an array A[1...n-1] is defined to be an index such that A[i] = i. Given a sorted array of distince integers, write a methon to find a magic index, if one exits, in array A。 FOLLOW

xinxinxv的专栏 1766

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Q4.8 To decide if T2 is a subtree of T1

Q:You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1 A:DFS. T1当前节点是否与T2的根节点相等。 如果不相等,查看T2是不是T

xinxinxv的专栏 1023

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Q9.9 N queens

Q: Write an algorithm to print all ways of arranging eight queens on a chess board so that none of them share the same row, column or diagonal. A: 经典的8皇后问题。DFS #include #include #include using

xinxinxv的专栏 832

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Q1.8 Check if s2 is a rotation of s1

Q: Assume you have a method isSubstring which checks if one word is asubstring of another. Given two strings, s1 and s2, write code tocheck if s2 is a rotation of s1 using only one call to isSubstring

xinxinxv的专栏 824

1.1 Implement an algorithm to determine if a string has all unique characters.

Implement an algorithm to determine if a string has all unique characters.What if you can not use additional data structures? 思路:首先,字符串是ascii编码还是unicode编码,这会影响记录的数组。  用bool ch[256] 记录所有的字符,初始化为fa

xinxinxv的专栏 720

Q3.1 Describe how you could use a single array to implement three stacks

Q:Describe how you could use a single array to implement three stacks A: 思路一:比较直观的想法是将一个数组分为三部分。将数组平分三部分, 每个部分维护一个栈顶指针。对特定的栈进行操作只要用栈顶指针加上偏移量就可以。 思路二:思路一可能会浪费大量的空间。所以不均分数组为三部分。定义一种数据类型,可以记录当前元素值和上一个

xinxinxv的专栏 706

Q5.4 ((n & (n-1)) == 0).

Q:Explain what the following code does: ((n & (n-1)) == 0). A: 其作用是判断n是否为0 或者是2的整数幂 因为如果n为2的m次幂的话,那么其二进制表示中只有第m位为1,其余全部为0(从0位开始)

xinxinxv的专栏 668

Q5.7 Find the missing number

Q: An array A[1…n] contains all the integers from 0 to n except for one number which is missing. In this problem, we cannot access an entire integer in A with a single operation. The elements of A a

xinxinxv的专栏 666

Q4.2 Given a directed graph, design an algorithm to find out whether there is a route.

Q: Given a directed graph, design an algorithm to find out whether there is a route between two nodes. A: 采用bfs, 同时利用哈希表保存当前节点是否已经被访问了。 如果节点i 和节点j 相连,并且j没有被访问过,那么节点j入栈,说明节点j可能是所求路径上的点。 就这样遍历,直到栈为空或

xinxinxv的专栏 649

Q1.7 set Matrix zeroes

Q:Write an algorithm such that if an element in an MxN matrix is 0,its entire row and column is set to 0. A: 通过遍历,可以用(m+n) 哈希表来储存每一列和每一行是否为0, 再根据哈希表的记录,再次遍历置0; 为了节省空间,可以在上面的思路基础之上进行优化: 1、首先遍历矩阵的第一行

xinxinxv的专栏 612

Q5.8 DrawHorizontalLine

Q: A monochrome screen is stored as a singal array of bytes, allowing eight consecutive pixels to be stored in one byte. The screen has width w, where w is a divisible by 8 (that is, no byte will be s

xinxinxv的专栏 607

Q3.5 Implement a MyQueue class which implements a queue using two stacks.

Q:Implement a MyQueue class which implements a queue using two stacks. A:一个栈sin用于进,一个栈sout用于出。 push:直接push进sin即可 pop: 如果sout不空,直接pop,否则将sin中的元素都压入sout之后再sout.pop front: 同pop,只不过不pop,而是top back:同f

xinxinxv的专栏 583

Q1.4 Write a method to replace all spaces in a string with ‘%20’.

思路: 1、遍历, 原来的字符串长度len, 记录空格个数cnt, 则最后的字符串长度len+2*cnt。 2、从后往前遍历,遇到空格将其替换为"%20",否则原样拷贝 #include #include using namespace std; void replace(char *c){ if(c == NULL) return; int len = strlen(

xinxinxv的专栏 578
上一篇: Q5.2 Print the binary representation
下一篇: Q5.4 ((n & (n-1)) == 0).
「已注销」
博客等级 码龄15年 5粉丝 218原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值