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

来源:互联网 发布:淘宝美工和室内设计师 编辑:程序博客网 时间:2024/06/03 15:08

每日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();        





原创粉丝点击