题解 | #DNA序列#

DNA序列

https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a

要注意比例的计算和BigDecimal的compareTo会调换位置

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * DNA序列
 *
 * ACGT
 */
public class DNASeq {
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> inputList = new ArrayList<>();
        while (scanner.hasNext()) {
            String nextLine = scanner.nextLine();
            if (inputList.size() == 1) {
                String dnaSeq = inputList.get(0);
                int subStrLength = Integer.parseInt(nextLine);
                
                List<String> subStrList = new ArrayList<>();
                char[] charArray = dnaSeq.toCharArray();
                for (int i = 0; i <= charArray.length - subStrLength; i++) {
                    String temp = "";
                    for (int j = i; j < i + subStrLength; j++) {
                        temp += String.valueOf(charArray[j]);
                    }
                    subStrList.add(temp);
                }
                
                List<SelfPair<String, BigDecimal>> subStrObjList = new ArrayList<>();
                
                // 算占比
                for (String subStr : subStrList) {
    
                    char[] toCharArray = subStr.toCharArray();
                    Long gCount = 0L;
                    Long cCount = 0L;
                    for (char ch : toCharArray) {
                        if (ch == 'G') {
                            gCount++;
                        }
                        if (ch == 'C') {
                            cCount++;
                        }
                    }
                    BigDecimal divide = BigDecimal.valueOf((gCount + cCount))
                            .divide(BigDecimal.valueOf(subStr.length()),4, 3);
                    subStrObjList.add(new SelfPair<>(subStr, divide));
                }
    
                // compareTo会调换顺序
                List<SelfPair<String, BigDecimal>> collect = subStrObjList.stream()
                        .sorted(Comparator.comparing(SelfPair::getValue)).collect(Collectors.toList());
                SelfPair<String, BigDecimal> resultPair = collect.get(collect.size() - 1);
                List<SelfPair<String, BigDecimal>> result = collect.stream()
                        .filter(obj -> obj.value.equals(resultPair.value)).collect(Collectors.toList());
                System.out.println(result.get(0).key);
                inputList.clear();
            } else {
                inputList.add(nextLine);
            }
        }
    }
    
    private static class SelfPair<K, V> {
        private K key;
        
        private V value;
        
        public SelfPair(K key, V value) {
            this.key = key;
            this.value = value;
        }
    
        public K getKey() {
            return key;
        }
    
        public V getValue() {
            return value;
        }
    }
}

全部评论

相关推荐

有趣的牛油果开挂了:最近这个阶段收到些杂七杂八的短信是真的烦
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务