leetcode859
class Solution {
public:
bool buddyStrings(string A, string B) {
if (A.length() != B.length()) {
return false;
}
// 交换两个字符使得AB相同
// 检测A和B中不同的字符位置个数
int diff_cnt = ;
int idxs[] = { , };
for (int i = ; i < A.length(); ++i) {
if (A[i] != B[i]) {
idxs[diff_cnt++] = i;
}
}
if (diff_cnt == ) {
// 没有不同
// 看A里是否有相同的字符串
map<char, bool> m;
for (auto &ch : A) {
if (m[ch]) {
return true;
}
else {
m[ch] = true;
}
}
return false;
}
if (diff_cnt != ) {
return false;
}
swap(A[idxs[]], A[idxs[]]);
return A == B;
}
};
网上的答案,以备复习使用。
leetcode859的更多相关文章
- [Swift]LeetCode859. 亲密字符串 | Buddy Strings
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- Leetcode859.Buddy Strings亲密字符串
给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回 false . 示例 1: 输入: A = "ab& ...
随机推荐
- 【C++】STL之队列queue
1.头文件 # include<queue> 2.成员函数 empty() 当队列为空时,返回true size() 返回队列内元素个数 front() 返回队首元素 back() 返回队 ...
- python对文件的读写
文件 File 什么是文件 文件是用于数据存储和单位 文件通常用来长期存储数据 文件中的数据是以字节为单位进行顺序存储的 文件的操作流程: 1. 打开文件 2. 读/写文件 3. 关闭文件 注: 任何 ...
- HDU3555 Bomb 数位DP第一题
The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the ti ...
- 每天一个linux命令(文件操作):【转载】whiereis命令
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis查找的速度非 ...
- hadoop2.2使用手册2:如何运行自带wordcount
问题导读:1.hadoop2.x自带wordcount在什么位置?2.运行wordcount程序,需要做哪些准备? 此篇是在hadoop2完全分布式最新高可靠安装文档 hadoop2.X使用手册1:通 ...
- js中怎么去掉数组的空值
for(var i = 0 ;i<array.length;i++) { if(array[i] == "" || typeof(array[i] ...
- 面试常考知识点——Java(JVM,JDK,JRE)
1. 什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? 答:(1)Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行的字节码文件. ...
- 工业标准接口OPC Server
工业标准接口OPC Server OPC Server服务器软件,简称OPCServer,是针对企业生产过程中所涉及到的各种DCS.PLC.组态软件.电力综合自动化等控制系统.测量系统.其它辅助生产 ...
- net start mongodb发生系统错误2 系统找不到指定的文件
安装mongodb时, 将mongodb 作为系统服务启动 net start mongodb,报错发生系统错误2 系统找不到指定的文件 . 查找原因是因为,系统服务的可执行文件地址有误. 修改服务地 ...
- java代码-----------继承练习
总结:父类和子类拥有相同的 方法时,父类的方法被覆盖,子类 package com.sads; class fong { void pprint() { this.print(); this.prin ...