using System;using System.Linq;namespace myjinxin
{publicclassKata{publicboolAreSimilar(int[] a1,int[] a2){var e = a1.Zip(a2,(i1,i2)=>new{i1,i2}).Where(t => t.i1 != t.i2).SelectMany(t =>new[]{t.i1, t.i2});return!e.Any()||(e.Distinct().Count()==2&& e.Count()==4);}}}
答案3:
using System.Linq;namespace myjinxin
{publicclassKata{publicboolAreSimilar(int[] a,int[] b){return a.OrderBy(i => i).SequenceEqual(b.OrderBy(i => i))&& a.Zip(b,(left, right)=> left == right ?0:1).Sum()<3;}}}
答案4:
namespace myjinxin
{using System;using System.Linq;publicclassKata{publicboolAreSimilar(int[] A,int[] B){if(A.Sum()== B.Sum()){int swaps =0;for(int i =0; i < A.Length; i++){if(A[i]!= B[i])
swaps++;}if(swaps >2)returnfalse;elsereturntrue;}elsereturnfalse;}}}
答案5:
namespace myjinxin {publicclassKata{publicboolAreSimilar(int[] A,int[] B ){if( A ==null|| B ==null){returnfalse;}if( A.Length != B.Length ){returnfalse;}var swap =false;for(int i =0; i < A.Length; i++){if( A [ i ]!= B [ i ]){if( swap ){returnfalse;}for(int j =0; j < B.Length; j++){if( B [ j ]== A [ i ]){if( B [ i ]== A [ j ]){var t = B [ i ];
B [ i ]= A [ i ];
B [ j ]= t;
swap =true;break;}}}if(!swap ){returnfalse;}}}returntrue;}}}
namespace myjinxin
{using System;using System.Collections.Generic;using System.Linq;publicclassKata{
ILookup<int,int> groupsA;
ILookup<int,int> groupsB;publicboolAreSimilar(int[] A,int[] B){// if the length of array differ, they are not similarif(A.Length ==0|| B.Length ==0|| A.Length != B.Length)returnfalse;var count =0;var checkedAllArrayElements =false;
groupsA = A.ToLookup(e => e);
groupsB = B.ToLookup(e => e);for(int i =0; i <= A.Length -1; i++){// if any element in the array 1 doesn't exist in 2 they are not similarif(!HaveSameElements(A[i], B)){break;}// if the number of occurences of an element differ in the 2 arrays they are not similarif(!HaveSameNumOfOccurences(A[i])){break;}// For max 1 swap, the elements value can be different at max 2 index locations of the 2 arraysif(i == A.Length -1)
checkedAllArrayElements =true;if(A[i]== B[i]){continue;}
count ++;if(count >2)break;}if(checkedAllArrayElements && count <=2)returntrue;returnfalse;}publicboolHaveSameElements(int item,int[] B){if(item == Array.Find(B, x => x == item))returntrue;returnfalse;}publicboolHaveSameNumOfOccurences(int item){var countInA = groupsA[item].Count();var countInB = groupsB[item].Count();if(countInA !=0&& countInB !=0&& countInA == countInB)returntrue;returnfalse;}}}
答案10:
namespace myjinxin
{using System;publicclassKata{publicboolAreSimilar(int[] A,int[] B){var ASorted =(int[]) A.Clone();
Array.Sort(ASorted);var BSorted =(int[]) B.Clone();
Array.Sort(BSorted);for(var i =0; i < ASorted.Length; i++){if(ASorted[i]!= BSorted[i])returnfalse;}var swapCount =0;for(var i =0; i < A.Length; i++){if(A[i]!= B[i])
swapCount++;if(swapCount >2)returnfalse;}returntrue;}}}
简单有趣#42:是相似的?【难度:1级】:答案1:namespace myjinxin{ using System; using System.Linq; using System.Collections.Generic; public class Kata { public bool AreSimilar(int[] A, int[...