Swap Without Extra Variable
Given two variables, x and y, swap two variables without using a third variable.
Given x = 10
, y = 5
Return 15.
思路:考察位运算,异或。 同一个数异或两次还是其本身。
class Solution {
public:
/**
* @param x an integer
* @param y an integer
* @return nothing
*/
void swap(int &x, int &y) {
// Write your code here
x = x ^ y;
y = x ^ y;
x = x ^ y;
}
};
Swap Without Extra Variable的更多相关文章
- C/C++ Swap without using extra variable
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50255379 对于可以线性运算的变量, ...
- MlLib--逻辑回归笔记
批量梯度下降的逻辑回归可以参考这篇文章:http://blog.csdn.net/pakko/article/details/37878837 看了一些Scala语法后,打算看看MlLib的机器学习算 ...
- SparkMLlib之 logistic regression源码分析
最近在研究机器学习,使用的工具是spark,本文是针对spar最新的源码Spark1.6.0的MLlib中的logistic regression, linear regression进行源码分析,其 ...
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
Write a program to print a histogram of the lengths of words in its input. It is easy to draw the hi ...
- Basic Sort Algorithms
1. Bubble Sort public void bubbleSort(int[] arr) { boolean swapped = true; int j = 0; int tmp; while ...
- 图之单源Dijkstra算法、带负权值最短路径算法
1.图类基本组成 存储在邻接表中的基本项 /** * Represents an edge in the graph * */ class Edge implements Comparable< ...
- Java:foreach实现原理
第一部分: For-each Loop Purpose The basic for loop was extended in Java5 to make iteration over arrays a ...
- MLlib之LR算法源码学习
/** * :: DeveloperApi :: * GeneralizedLinearModel (GLM) represents a model trained using * Generaliz ...
- Longest Turbulent Subarray LT978
A subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent if and only if: For i <= k < j ...
随机推荐
- SPI时序
1.串行外围接口 高速.全双工的同步通信总线 一主多从 一般速度几十MHZ,最高可以工作在上百MHZ 2.连接图 3.工作模式
- Python复习笔记01
(1)计算机常识 计算机:硬件(运算器,控制器,存储器,输入设备,输出设备)软件 (系统软件, 应用软件) 二进制 整数存储 文件单 位换算 1Byte = 8bit 1KB = 1024Byte 1 ...
- skywalking 比较有意思的地方
获取agent jar包路径的方法: findPath(); private static File findPath() throws AgentPackageNotFoundException { ...
- Selenium 调用IEDriverServer打开IE浏览器
Selenium 调用IEDriverServer打开IE浏览器 2016年03月30日 09:49:37 标签: selenium 14836 Selenium 调用IEDriverServer打开 ...
- Python之原始数据-1
一.数据对于模型来说是基础,是数据成就了模型,而现在的又是一个数据时代,比如:淘宝等.通过对用户数据的分析挖掘,预测用户的消费习惯等,再比如:人工智能.通过提取摄像头的图片帧数,通过分析图片,得出具体 ...
- mybatis获取刚刚插入到数据库的数据的id(转载)
原文地址:https://blog.csdn.net/hehuihh/article/details/82800739 我用的是第一种写法,直接把代码copy到insert标签里(id要是自增id) ...
- Java Web 深入分析(10) Spring 实践
Spring helloworld [http://wiki.jikexueyuan.com/project/spring/hello-world-example.html] HelloWorld.j ...
- mybatisplus 使用案例
案例地址 https://github.com/qixianchuan/SpringBootQD/tree/master/mybatisplus
- 【js】字符串反转(倒序)的多种处理方式
今天发布一篇关于字符串反转的几种方式(一种问题的解决方案不是只有一种). 方式1: 这种方式比较简单,推荐使用 字符串转数组,反转数组,数组转字符串. split(""):根据空字 ...
- SVN上传本地项目到服务器
1. 在服务器新建一个文件夹目录: 2. 将新建的目录在本地check out下来: 3. 将自己的项目拷贝到check out下来的文件夹下: 4. 右键点击svnàAdd,选择所有添加: 5. 右 ...