Code Signal_练习题_Are Similar?
Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays.
Given two arrays a and b, check whether they are similar.
Example
For
a = [1, 2, 3]andb = [1, 2, 3], the output should beareSimilar(a, b) = true.The arrays are equal, no need to swap any elements.
For
a = [1, 2, 3]andb = [2, 1, 3], the output should beareSimilar(a, b) = true.We can obtain
bfromaby swapping2and1inb.For
a = [1, 2, 2]andb = [2, 1, 1], the output should beareSimilar(a, b) = false.Any swap of any two elements either in
aor inbwon't makeaandbequal.
我的解答:
def areSimilar(a, b):
count = 0
if sorted(a) == sorted(b):
for i in zip(a,b):
if i[0] != i[1]:
count +=1
if count > 2:
return False
else:
return True
else:
return False
膜拜大佬:
def areSimilar(A, B):
return sorted(A)==sorted(B) and sum([a!=b for a,b in zip(A,B)])<=2
Code Signal_练习题_Are Similar?的更多相关文章
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- Code Signal_练习题_absoluteValuesSumMinimization
Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...
随机推荐
- [JS] js 判断用户是否在浏览当前页面
var hiddenProperty = 'hidden' in document ? 'hidden' : 'webkitHidden' in document ? 'webkitHidden' : ...
- Redis存储
redis库提供了两个类:Redis和StrictRedis来实现Redis的命令操作,前者是为了兼容老版本库的集中方法,一般就用StrictRedis 一. redis基本操作 . 设置redis密 ...
- 分析解决Java运行时异常
1 ,基础知识 http://my.oschina.net/u/140462/blog/421128 JVM运行时内存结构 2 ,相关命令 http ...
- (Lua) C++ 加入 Lua 環境擴充應用強度
Lua 在網上有非常多的介紹,就是一個小而巧的語言,可以放入嵌入式系統 也可以在一般的應用上非常強大,這邊主要記錄如何讓Lua加入C++裡頭應用 Lua source code 是以 C 語言下去編寫 ...
- Mybatis中的@Param注解
前言 今天在使用Mybatis时遇到一个问题,主要出错原因在于Mybatis的@Param注解,如果我不在参数前面加上@Param注解,数据库操作就会报错,如下: @Param作用 @Param注解的 ...
- Asp.NET MVC 拍卖网站,拆解【1】预览与目录
本人最近带创业团队基本做完了一个艺术品拍卖的外包项目,分为网站前台(asp.net mvc5),网站管理员管理的后台使用的9900端口(asp.net mvc5),监听拍卖状态的windows服务,为 ...
- JS检测数据类型
如果你要判断的是基本数据类型或JavaScript内置对象,使用toString: 如果要判断的时自定义类型,请使用instanceof. 1.typeof typeof操作符返回的是类型字符串,它的 ...
- JavaScript设计模式-1.函数
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- PHP之mb_internal_encoding使用
mb_internal_encoding (PHP 4 >= 4.0.6, PHP 5, PHP 7) mb_internal_encoding - Set/Get internal chara ...
- MultipartFile的使用小结
Multipartfile转File?File转MultipartFile?可千万别转晕了. 题图:from Google 1. MultipartFile类型转File类型 想要将Multipart ...