全部评论
//我的代码如下:第二题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p id="box"></p>
<script>
function compare(str, str1) {
var strone = str;
var strtwo = str1;
for(var i = 0;i<strone.length;i++){
for(var j=0;j<strtwo.length;j++){
if(strone.charAt(i) === strtwo.charAt(j)){
var strr1 = strone.split(`${strone.charAt(i)}`).join('');
var strr2 = strtwo.split(`${strtwo.charAt(j)}`).join('');
}
}
}
var arrNovel = [];
arrNovel.push(strr1);
arrNovel.push(strr2);
var result = arrNovel.join("");
var zsort = result.split("").sort();
var fsort = [];
for(var i= zsort.length-1;i>=0;i--){
fsort.push(zsort[i]);
};
var lastResult = zsort.concat(fsort).join("");
return lastResult;
}
window.onload = function () {
document.getElementById('box').innerHTML = compare("as123456dAf", "wqerd");
}
</script>
</body>
</html>
直接用一个map,记录字母出现的次数,然后把次数为1的字母拿出来排序,反转整合就行了
//已经解出来了,完整代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p id="box"></p>
<script>
function compare(str, str1) {
var strone = str;
var strtwo = str1;
var obj1 = {};
var obj2 = {};
var rarr = [];
var rarr2 =[];
for(var x=0;x<strone.length;x++){
var chart = strone.charAt(x);
if(obj1[chart]){
obj1[chart]++;
}else{
obj1[chart]=1;
}
}
for (var k in obj1) {
if (obj1[k] === 1) {
rarr.push(k);
}
}
for (var y = 0; y < strtwo.length; y++) {
var chart2 = strtwo.charAt(y);
if (obj2[chart2]) {
obj2[chart2]++;
} else {
obj2[chart2] = 1;
}
}
for (var l in obj2) {
if (obj2[l] === 1) {
rarr2.push(l);
}
}
strone = rarr.join("");
strtwo = rarr2.join("");
for(var i = 0;i<strone.length;i++){
for(var j=0;j<strtwo.length;j++){
if(strone.charAt(i) === strtwo.charAt(j)){
var strr1 = strone.split(`${strone.charAt(i)}`).join('');
var strr2 = strtwo.split(`${strtwo.charAt(j)}`).join('');
}
}
}
var arrNovel = [];
arrNovel.push(strr1);
arrNovel.push(strr2);
var result = arrNovel.join("");
var zsort = result.split("").sort();
var fsort = [];
for(var i= zsort.length-1;i>=0;i--){
fsort.push(zsort[i]);
};
var lastResult = zsort.concat(fsort).join("");
return lastResult;
}
window.onload = function () {
document.getElementById('box').innerHTML = compare("aaaas1234dasdadassfaf5f4a6f4a684a6dAf", "wqefasf84fs4fsdrd");
}
</script>
</body>
</html>
//给定两个字符串,求差集
function compare(str1,str2) {
var str11=str1.split('');
var str22=str2.split('');
var res=[]
for(var i=0;i<str11.length;i++){
if(str22.indexOf(str11[i])==-1){
res.push(str11[i])
}
}
for(var j=0;j<str22.length;j++){
if(str11.indexOf(str22[j])==-1){
res.push(str22[j])
}
}
var resul=res.sort().reduce(function(ac,cur){
var len=ac.length;
if(len===0||ac[len-1]!=cur){
ac.push(cur)
}
return ac;
},[]);
var result=resul.sort(function (a,b) {
return b-a;
})
var result1=result.concat([]);
var result2=result.reverse();
var result3=result1.concat(result2).join('')
return result3;
}
你第一题ac了吗
相关推荐
s8x:学会了,标题带上找人改简历xx钱,流量密码😂
投递哔哩哔哩等公司10个岗位
点赞 评论 收藏
分享