Code Signal_练习题_Array Replace
Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem.
Example
For inputArray = [1, 2, 1], elemToReplace = 1, and substitutionElem = 3, the output should bearrayReplace(inputArray, elemToReplace, substitutionElem) = [3, 2, 3].
我的解答:
def arrayReplace(inputArray, elemToReplace, substitutionElem):
for i in range(len(inputArray)):
if inputArray[i] == elemToReplace:
inputArray[i] = substitutionElem
return inputArray
def arrayReplace(inputArray, elemToReplace, substitutionElem):
return [substitutionElem if x==elemToReplace else x for x in inputArray]
膜拜大佬
Code Signal_练习题_Array Replace的更多相关文章
- 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_练习题_alphabeticShift
Given a string, replace each its character by the next one in the English alphabet (z would be repla ...
- Code Signal_练习题_palindromeRearranging
Given a string, find out if its characters can be rearranged to form a palindrome. Example For input ...
- 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 ...
随机推荐
- 在Load average 高的情况下如何鉴别系统瓶颈
在Load average 高的情况下如何鉴别系统瓶颈.是CPU不足,还是io不够快造成? 或是内存不足? 一:查看系统负载vmstat procs -----------memory-------- ...
- Oracle 数据类型 与C#映射关系
大部分类型的对应关系:原文:http://2143892.blog.51cto.com/2133892/499353 序号 Oracle数据类型 .NET类型 GetOracleValue类型 DbT ...
- jvm高级特性(2)(判断存活对象算法,finaliza(),方法区回收)
JVM高级特性与实践(二):对象存活判定算法(引用) 与 回收 垃圾回收器GC(Garbage Collection) 于1960年诞生在MIT的Lisp是第一门真正使用内存动态分配和垃圾收集技术的语 ...
- HashMap、HashSet、LinkedHashSet、TreeSet的关系
类图及说明如下:
- 剑指offer三十八之二叉树的深度
一.题目 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 二.思路 递归,详见代码. 三.代码 public class So ...
- C++实现的字符串模糊匹配
C++基本没有正则表达式功能,当然像Boost里提供了正则.本文来源于博客园园友的一篇文章,请看: C/C++ 字符串模糊匹配 很早之前就看过这篇文章,原作者的需求很明确.代码实现也很好. 之所以又写 ...
- 再学Java 之 Integer 包装类缓存
前言:本博文将涉及的Java的自动装箱和自动拆箱,可以参考 这篇文章 和 官方教程 ,这里不再赘述. 首先,先看一个小程序: public class Main { public static voi ...
- Vue把父组件的方法传递给子组件调用(评论列表例子)
Vue把父组件的方法传递给子组件调用(评论列表例子) 效果展示: 相关Html: <!DOCTYPE html> <html lang="en"> < ...
- Liunx安装Git
环境:CentOS 6.5 min 安装git yum -y install gcc curl curl-devel zlib-devel openssl-devel perl cpio expat- ...
- 分析org.rpgpoet.Music.wizards.length
例子如下: package bazola; public class Gabriel { static int n = org.rpgpoet.Music.wizards.length; } pack ...