C#练习题答案: 简单有趣#42:是相似的?【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

简单有趣#42:是相似的?【难度:1级】:

答案1:

namespace myjinxin
{
    using System;
    using System.Linq;
    using System.Collections.Generic;

    public class Kata
    {
        public bool AreSimilar(int[] A, int[] B)
        {
            List<int> diff = Enumerable.Range(0, A.Length).Where(i => A[i] != B[i]).ToList();
            return diff.Count == 0 || (diff.Count == 2 &amp;&amp; A[diff[0]] == B[diff[1]] &amp;&amp; B[diff[0]] == A[diff[1]]);
        }
    }
}

答案2:

using System;
using System.Linq;

namespace myjinxin
{    
    public class Kata
    {
        public bool AreSimilar(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 &amp;&amp; e.Count() == 4);
        }
    }
}

答案3:

using System.Linq;

namespace myjinxin
{
    public class Kata
    {
        public bool AreSimilar(int[] a, int[] b)
        {
            return a.OrderBy(i => i).SequenceEqual(b.OrderBy(i => i)) &amp;&amp; a.Zip(b, (left, right) => left == right ? 0 : 1).Sum() < 3;
        }
    }
}

答案4:

namespace myjinxin
{
    using System;
    using System.Linq;
    
    public class Kata
    {
        public bool AreSimilar(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)
            return false;
          else
            return true;
         }
         else
           return false;
          
        }
    }
}

答案5:

namespace myjinxin {
    public class Kata {

        public bool AreSimilar( int[] A, int[] B ) {
            if ( A == null || B == null ) {
                return false;
            }
            if ( A.Length != B.Length ) {
                return false;
            }
            var swap = false;
            for ( int i = 0; i < A.Length; i++ ) {
                if ( A [ i ] != B [ i ] ) {
                    if ( swap ) {
                        return false;
                    }
                    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 ) {
                        return false;
                    }
                }
            }
            return true;
        }
    }
}

答案6:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public bool AreSimilar(int[] A, int[] B)
        {
          int diff1 = -1;
          int diff2 = -1;
          for (int i = 0; i < A.Length; i++)
          {
            if (A[i] != B[i])
            {
              if (diff1 == -1)
              {
                diff1 = i;
              }
              else if (diff2 == -1)
              {
                diff2 = i;
              }
              else
              {
                return false;
              }
            }
          }
          return (diff1 == -1 &amp;&amp; diff2 == -1) || (diff1 != -1 &amp;&amp; diff2 != -1 &amp;&amp; A[diff1] == B[diff2] &amp;&amp; A[diff2] == B[diff1]);
        }
    }
}

答案7:

namespace myjinxin
{
    using System.Linq;
    
    public class Kata
    {
        public bool AreSimilar(int[] A, int[] B){
          return (A.Select((n, i) => n - B[i]).Count(n => n != 0) <= 2 ? true : false) &amp;&amp;
          A.OrderBy(n=>n).ToList().SequenceEqual(B.OrderBy(n=>n).ToList());
        }
    }
}

答案8:

namespace myjinxin
{
    using System;
    using System.Linq;
    
    public class Kata
    {
        public bool AreSimilar(int[] A, int[] B){
            if (A.Length == B.Length &amp;&amp;  A.Sum() == B.Sum())
            {
                int counter = 0;

                for(int i = 0; i < A.Length; i++)
                {
                    if (A[i] != B[i])
                        counter++;
                }
                return counter < 3 ? true : false;
            }
            return false;
        }
    }
}

答案9:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Kata
    {
        ILookup<int, int> groupsA;
        ILookup<int, int> groupsB;
        
        public bool AreSimilar(int[] A, int[] B)
        {          
            // if the length of array differ, they are not similar
            if (A.Length == 0 || B.Length == 0 || A.Length != B.Length) return false;
          
            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 similar
                if (!HaveSameElements(A[i], B)){
                    break;
                }
            
                // if the number of occurences of an element differ in the 2 arrays they are not similar
                if (!HaveSameNumOfOccurences(A[i])){
                    break;
                }
            
                // For max 1 swap, the elements value can be different at max 2 index locations of the 2 arrays
                if (i == A.Length - 1) 
                    checkedAllArrayElements = true;
                
                if (A[i] == B[i]) {
                    continue;
                }     
            
                count ++;            
                if(count > 2) break;            
            }
          
            if(checkedAllArrayElements &amp;&amp; count <= 2) 
                return true;
          
            return false;          
        }
        
        public bool HaveSameElements(int item, int[] B)
        {        
            if (item == Array.Find(B, x => x == item))
                return true;     
          
            return false;
        }
        
        public bool HaveSameNumOfOccurences(int item){
            var countInA = groupsA[item].Count();
            var countInB = groupsB[item].Count();

            if (countInA != 0 &amp;&amp; countInB != 0 &amp;&amp; countInA == countInB)
                return true;

            return false;
        }
    }
}

答案10:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public bool AreSimilar(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])
              return false;
          }
          
          var swapCount = 0;
          
          for (var i = 0; i < A.Length; i++)
          {
            if (A[i] != B[i])
              swapCount++;
              
            if (swapCount > 2)
              return false;
          }
          
          return true;
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值