lint 1900

https://www.lintcode.com/problem/1900/description

// 1 see number & char pair as a whole
// 2 locate number, as number string has left, right bound, two pointer here
// 3 use top down thinking style, use but NOT implement isDigit(), toNum()
// 4 after processing number/char pair, how to move to next iteration.
// write diagram to make sure pointer is correct.
// 5 issue: out of memory

public class Solution {
    /**
     * @param Gene1: a string
     * @param Gene2: a string
     * @return: return the similarity of two gene fragments
     */
    public String GeneSimilarity(String Gene1, String Gene2) {
        // write your code here

        String str1 = transform(Gene1);
        String str2 = transform(Gene2);

        int n = str1.length();
        int cnt=0;
        for(int i=0;i<str1.length();i++){
            if(str1.charAt(i)==str2.charAt(i)){
                cnt++;
            }
        }
        return ""+cnt+"/"+n;
    }

    String transform(String gene, Map map, List list){
        
        StringBuilder sb = new StringBuilder();

        int i=0;        
        int j=0;
        while(i<gene.length()){

            while(isDigit(gene.charAt(j))){
                j++;                
            }                
            String numStr = gene.substring(i, j);
            int num = toNum(numStr);

            char ch = gene.charAt(j);

            for(int k=0;k<num;k++){
                 sb.append(ch);
            }
            i = j+1;
            j=i;
        }        
        return sb.toString();
    }

    boolean isDigit(char ch){
        
        return ch >= '0' && ch <= '9';
    }

    int toNum(String num){

        int ret=0;
        for(int i=0;i<num.length();i++){
            ret = ret*10;
            int digit = num.charAt(i) - '0';            
            ret += digit;
        }
        return ret;
    }
            
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值