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 ...
随机推荐
- [转帖]Hive基础(一)
Hive基础(一) 2018-12-19 15:35:03 人间怪物 阅读数 234 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接 ...
- 关于使用 symfony 3.4.32中Luckynumber 页面的 route 书写
关于symfony 3.4.32的安装与配置及第一个页面Luckynumber 的route书写 1.symfony 的安装与配置 symfony官网文档:https://symfony.com/do ...
- C 语言字符串的比较
C 语言字符串的比较 #include <stdio.h> #include <Windows.h> #include <string.h> int main(vo ...
- eclise -The method onClick(View) of type new View.OnClickListener(){} must override a superclass method 在做arcgis android开发的时候,突然遇到这种错误,The method onClick(View) of type new View.OnClickListener(){} mus
eclise -The method onClick(View) of type new View.OnClickListener(){} must override a superclass met ...
- c++ (1) c++ 与c 的区别
可以说c++ 语言在c基础上扩展了许多 在学习玩c语言之后 学习c++ 会发现容易一些 但是c++也有优越于c 的地方 c++ 与c 语言都属于本地编译型语言 ,直接编译成本地编译码,运行特别快 ...
- Arm-Linux 移植 alsa
ref : https://www.cnblogs.com/yutingliuyl/p/6718875.html https://blog.csdn.net/yuanxinfei920/article ...
- Eclipse下使用Maven创建项目出现的archetype错误,记,转
记自:http://blog.csdn.net/ZhuboSun/article/details/50099635 [1]出现的错误提示: Unable to create project from ...
- vue-ueditor-wrap报错,vue ueditor 加载ueditor.config.js失败,请检查您的配置地址UEDITOR_HOME_URL填写是否正确!
1.第一次报错后,仔细看了官方文档,少了第一步,下载UEditor源码,放到public目录(前提你用的是vue-cli 3.x的版本). 好那就下载,下载的是vue-ueditor-wrap作者修复 ...
- 使用Feign通过服务名调用服务,找不到服务
fegineureka 报错环境: eureka注册中心在远程服务器上 本地服务注册到远程的eureka注册中心 本地服务通过Fegin组件+服务名调用服务 报错时,注册中心的情况: Applicat ...
- java基本数据类型包装
1. 2. 左边的是对象,自动装箱为对象,右边的是基本的数据类型. 3. 如果m,n换成128就超出范围,结果就不一样. 是因为把在这区间内的值都放在了常量池里面. Integer m = Integ ...