每日AC-leetcode word-break-ii -与dubbo 异步回调问题

深入理解 JVM 原理
深入理解 JVM 原理
wangxiaoming

每日AC-leetcode word-break-ii -与dubbo 异步回调问题

题目描述


Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s ="catsanddog",
dict =["cat", "cats", "and", "sand", "dog"].

A solution is["cats and dog", "cat sand dog"].



未AC代码:相对来说比较诡异,总之AC不了, 等待解决

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年7月20日    Create this file
 * </pre>
 * 
 */

public class WordBreak {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String s = "catsanddog";
        // String dict ="cat, cats, and, sand, dog";
        Set<String> set = new HashSet<String>();
        set.add("cat");
        set.add("cats");
        set.add("and");
        set.add("sand");
        set.add("dog");
        ArrayList<String> list = wordBreak(s, set);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    public static ArrayList<String> wordBreak(String s, Set<String> dict) {

        // ArrayList<String> list = new ArrayList<String>();

        Map<String, ArrayList> map = new HashMap<String, ArrayList>();

        ArrayList<String> pos = dp(s, dict, map);
        /*
         * for(int i = 0; i < s.length();i++){ ArrayList<String> pos = dp(i,
         * s.length(),s, dict); String str = s.substring(i,pos); list.add(str);
         * if(pos != s.length()-1){
         * 
         * } }
         */
        return pos;
    }

    /**
     * @param i
     * @param length
     * @param s
     * @param dict
     * @return
     */
    public static ArrayList<String> dp(String s, Set<String> dict, Map<String, ArrayList> map) {

        ArrayList<String> list = new ArrayList<String>();
        int len = s.length();

        if (null != map.get(s)) {
            return map.get(s);
        }

        if ("".equals(s) || null == s) {
            list.add("");
        } else {
            for (int k = 1; k <= len; ++k) {
                String str = s.substring(0, k);
                if (dict.contains(str)) {
                   
                    ArrayList<String> sublist = dp(s.substring(k, len), dict, map);
                    if (sublist.size() != 0) {
                        for (int j = 0; j < sublist.size(); j++) {
                            String strbuilder = new String();
                            String subStr = sublist.get(j);
                            strbuilder += str + " " + subStr + " ";
                            list.add(strbuilder.trim());
                        }
                    } else {
                        continue;
                    }
                }
            }

        }
        map.put(s, list);
        return list;
    }

}



关于Dubbo 异步回调这个问题, 暂时不详细说:


配置文件:

    <dubbo:reference id="statExternalService" interface="cpcn.payment.statement.api.rpc.statexternal.StatExternalService" version="1.0.1" cluster="failfast" connections="1" >
          <dubbo:method name="tx1001" async="true" onreturn = "statementExternalServiceImpl.onReturn" onthrow="statementExternalServiceImpl.onThrow" />
    </dubbo:reference>
    
 <!--    <dubbo:reference id="statExternalService1" interface="cpcn.payment.statement.api.rpc.statexternal.StatExternalService" version="1.0.1">
        <dubbo:method name="tx1001" async="true"  />
    </dubbo:reference> -->


定义回调接口:

import cpcn.payment.statement.api.rpc.statexternal.Stat1001Request;
import cpcn.payment.statement.api.rpc.statexternal.Stat1001Response;

/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * wangming      2017年7月6日    Create this file
 * </pre>
 * 
 */

public interface StatementExternalService {
    
    /**
     * 服务端正常返回异步返回 返回 onReturn
     * 
     * @param stat1001Request
     * @return
     * @throws Exception
     */
    Stat1001Response onReturn(Stat1001Response stat1001Response,Stat1001Request stat1001Request) throws Exception;

    /**
     * 服务端出现异常,调用onThrow
     * 
     * @param stat1001Request
     * @return
     * @throws Exception
     */
    void onThrow(Throwable ex,  Stat1001Request stat1001Request) throws Exception;

}



实现回调接口函数:

import org.springframework.stereotype.Service;

import cpcn.payment.settlement.async.StatementExternalService;
import cpcn.payment.statement.api.rpc.statexternal.Stat1001Request;
import cpcn.payment.statement.api.rpc.statexternal.Stat1001Response;

/**
 * 
 * <pre>
 *
 * 类说明: 异步回调方法
 * Modify Information:
 * Author       Date         Description
 * ============ =========== ============================
 * xiaoming     2017年7月6日    Create this file
 * 
 * </pre>
 */
@Service("statementExternalServiceImpl")
public class StatementExternalServiceImpl implements StatementExternalService {


    @Override
    public Stat1001Response onReturn(Stat1001Response stat1001Response, Stat1001Request stat1001Request) throws Exception {
        System.out.println(stat1001Response);
        return stat1001Response;
       
    }

    @Override
    public void onThrow(Throwable ex, Stat1001Request stat1001Request) throws Exception {
        System.out.println(ex.getMessage());
        System.out.println(stat1001Request);
    }
    
 
}



代码调用:

        Stat1001Response response  = statExternalService.tx1001(request);
        Future<Stat1001Response> responseFuture =  RpcContext.getContext().getFuture();
        response =   responseFuture.get();
        





wangxiaoming CSDN认证博客专家 架构 Spring Boot Redis
博客是很好的总结和记录工具,如果有问题,来不及回复,关注微信公众号:程序员开发者社区,获取我的联系方式,向我提问,也可以给我发送邮件,联系 1275801617@qq.com
©️2020 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值