栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java Codility Training基因组范围查询

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Codility Training基因组范围查询

以下是在codility.com中获得100分之100的解决方案。请阅读有关前缀和的信息,以了解解决方案:

public static int[] solveGenomicRange(String S, int[] P, int[] Q) {        //used jagged array to hold the prefix sums of each A, C and G genoms        //we don't need to get prefix sums of T, you will see why.        int[][] genoms = new int[3][S.length()+1];        //if the char is found in the index i, then we set it to be 1 else they are 0        //3 short values are needed for this reason        short a, c, g;        for (int i=0; i<S.length(); i++) { a = 0; c = 0; g = 0; if ('A' == (S.charAt(i))) {     a=1; } if ('C' == (S.charAt(i))) {     c=1; } if ('G' == (S.charAt(i))) {     g=1; } //here we calculate prefix sums. To learn what's prefix sums look at here https://codility.com/media/train/3-PrefixSums.pdf genoms[0][i+1] = genoms[0][i] + a; genoms[1][i+1] = genoms[1][i] + c; genoms[2][i+1] = genoms[2][i] + g;        }        int[] result = new int[P.length];        //here we go through the provided P[] and Q[] arrays as intervals        for (int i=0; i<P.length; i++) { int fromIndex = P[i]; //we need to add 1 to Q[i],  //because our genoms[0][0], genoms[1][0] and genoms[2][0] //have 0 values by default, look above genoms[0][i+1] = genoms[0][i] + a;  int toIndex = Q[i]+1; if (genoms[0][toIndex] - genoms[0][fromIndex] > 0) {     result[i] = 1; } else if (genoms[1][toIndex] - genoms[1][fromIndex] > 0) {     result[i] = 2; } else if (genoms[2][toIndex] - genoms[2][fromIndex] > 0) {     result[i] = 3; } else {     result[i] = 4; }        }        return result;    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516570.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号