Comments (3)
/**
*
* 思路:挨个遍历A,如果当前位置i,A[i]!=B[i],则开始交换,
* 交换时,需要将A[j] = B[i] 的元素放在i位置,注意,这里j可能有多个值,所以需要来个循环
* 还有个要注意的是:如果A[j]=B[i] && A[j] == B[j] 这种情况,是不需要交换的
* 举个例子:
* abcb bbca ,第一个交换位置是0,这个时候,有两个选择,把A[1]或A[3]换到0位置,那么如果是位置1,这个时候本来A[1]和B[1]就相等,如果换了,就要多一次交换次数
* 所以这里我们只选择交换A[3]
*
* 另外,在处理循环时,要注意,如果交换j1时,会得到后面一系列的结果,这时,可以将这些结果保存起来,
* 如果在交换j2时,遇到已经处理过的结果,就直接调用
*
* 这里我用递归做,思路应该会清晰点
* @param {string} A
* @param {string} B
* @return {number}
*/
var kSimilarity = function(A, B) {
var map = {};
return temp(0, A);
function temp(start, tA) {
if (start == tA.length) {
return 0;
}
if (map[tA]) {
return map[tA];
}
if (tA[start] == B[start]) {
return temp(start + 1, tA);
} else {
var min = Infinity;
for (var i = start + 1; i < tA.length; i++) {
if (tA[i] == B[start] && tA[i] != B[i]) {
var newA =
tA.substring(0, start) +
tA[i] +
tA.substring(start + 1, i) +
tA[start] +
tA.substring(i + 1);
min = Math.min(min, 1 + temp(start + 1, newA));
}
}
map[tA] = min;
return min;
}
}
};
from leetcode.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
from leetcode.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
from leetcode.
Related Issues (20)
- 算法学习 HOT 1
- 树专题中双色标记法后序和前序写反了 HOT 2
- leetcode/thinkings/tree.md 出错 HOT 1
- some error
- 二分查找专题,寻找最左/右插入位置算法模板错误问题 HOT 9
- possible code error in thinkings/heap.md HOT 1
- link error HOT 4
- link is not correct
- [695.最大岛屿面积,360,面试原题]【每日一题】 HOT 3
- 【专题】 反向思考 HOT 3
- 【专题】 考虑每一项对结果到的贡献
- 【专题】递推方程时间复杂度优化
- 已发布文章的代码错误 HOT 7
- Remove duplicate CPP solution and add Python solution for problem 100.same-tree
- Add OSSF Scorecard security workflow
- 题目的排版可否改一改
- 关于二分法中查找中间点索引的算式 HOT 6
- leetcode-thinkings-tree.md BFS 模版调整 HOT 3
- anki-card 中只有10道题吗?截止到2023.11 HOT 1
- 【每日一题】- 2020-xx-xx - xxx
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leetcode.