这是剑指offer数组中重复的数字那个题,直接使用的swap函数

class Solution {
public:
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
bool duplicate(int numbers[], int length, int* duplication) {
if(length <= ){
*duplication = -;
return false;
}
int start = ;
while(start < length){
if(numbers[start] == numbers[numbers[start]] && numbers[start] != start){
*duplication = numbers[start];
return true;
}
if(start == numbers[start]){
start++;
continue;
}
int index = numbers[start];
while(numbers[start] != numbers[index]){
swap(numbers[start],numbers[index]);
index = numbers[start];
}
}
*duplication = -;
return false;
}
};

字符串的全排列也用到了swap

class Solution {
public:
vector<string> Permutation(string str) {
if(str.size() == )
return ans;
length = str.size();
int begin = ;
Permutation(str,begin);
//res.clear();
set<string>::iterator it;
for (it = res.begin(); it != res.end(); ++it)
ans.push_back(*it);
return ans;
}
void Permutation(string str,int begin){
if(begin == length){
res.insert(str);
return;
}
for(int i = begin;i < length;i++){
swap(str[begin],str[i]);
Permutation(str,begin+);
swap(str[begin],str[i]);
}
}
set<string> res;
vector<string> ans;
int length = ;
};

c++ 有swap函数的更多相关文章

  1. 关于swap函数传值的问题

    #include <stdio.h> void swap(int * p3,int * p4); int main() {  int a = 9;  int b = 8;  int * p ...

  2. 自己写一个swap函数交换任意两个相同类型元素的值 对空指针的使用 字节大小的判断(二)了解原理

    验证的代码: #include <stdio.h> int main(){ char c = 'z'; ) + (c << ) + () + 'a'; printf(" ...

  3. swap函数的四种写法

    swap 函数的四种写法 (1)经典型 --- 嫁衣法 void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } ( ...

  4. c++ swap 函数

    转载地址 1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T ...

  5. [转]谈谈C++中的swap函数

    1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T c(a) ...

  6. [Effective C++ --025]考虑写出一个不抛异常的swap函数

    引言 在我的上一篇博客中,讲述了swap函数. 原本swap只是STL的一部分,而后成为异常安全性编程的脊柱,以及用来处理自我赋值可能性. 一.swap函数 标准库的swap函数如下: namespa ...

  7. [020]转--C++ swap函数

    原文来自:http://www.cnblogs.com/xloogson/p/3360847.html 1.C++最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符 template < ...

  8. 从Swap函数谈加法溢出问题

    1.      初始题目 面试题:). 这个题目太经典,也太简单,有很多人都会不假思索结出答案: //Code 1 void Swap(int* a, int* b) { *a = *a + *b; ...

  9. EC读书笔记系列之13:条款25 考虑写出一个不抛异常的swap函数

    记住: ★当std::swap对你的类型效率不高时,提供一个swap成员函数,并确定其不抛出异常 ★若你提供一个member swap,也该提供一个non-member swap来调用前者.对于cla ...

  10. 【转】 谈谈C++中的swap函数

    1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T c(a) ...

随机推荐

  1. Foxmail邮件收取网易企业邮件配置

  2. Code Signal_练习题_arrayChange

    You are given an array of integers. On each move you are allowed to increase exactly one of its elem ...

  3. Node.js 的安装

    Node.js 是一个基于 Chrome V8 引擎的 JavaScript 的运行环境,简单的说就是运行在服务端的 JavaScript.所以学起来还是比较容易接受的. Node.js 使用事件驱动 ...

  4. WinForm实现Rabbitmq官网6个案例-Publishe/Subscribe

    代码: namespace RabbitMQDemo { public partial class PublishSubscribe : Form { private string exchangeN ...

  5. 【转载记录】Accessing Device Drivers from C#

    来源:http://www.drdobbs.com/cpp/accessing-device-drivers-from-c/184416423/   Device Drivers are writte ...

  6. Function Object in C++

    Function object is very userful to use member function or non-member function as callback mechanism, ...

  7. JavaScript : Array assignment creates reference not copy

    JavaScript : Array assignment creates reference not copy 29 May 2015 Consider we have an array var a ...

  8. Benefits of encapsulation

    ①:通过方法来控制成员变量的操作,提高了代码的安全性. ②:把代码用方法进行封装,提高了代码的复用性.

  9. Dancing Line、网易蜗牛读书——创新性分析

    Dancing Line——视听效果极佳的解压游戏 介绍:跳舞的线是由猎豹移动公司和BoomBitInc制作的一款游戏,发行于2016年12月12日. 游戏规则:跟着音乐的节奏点击屏幕,完成转向,躲避 ...

  10. C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题

    如何:对 Windows 窗体控件进行线程安全调用 访问 Windows 窗体控件本质上不是线程安全的. 如果有两个或多个线程操作某一控件的状态,则可能会迫使该控件进入一种不一致的状态. 还可能会出现 ...