一些棘手的问题 主要是解决问题思路(递归 map 迭代器)

原创
2016/07/28 16:06
阅读数 111

1:(递归)有一个字符串,根据输入参数m,找出字符串的m个字符的所有字符串

例如 
String str ="abc", m=2  得到结果是 "ab" "ac" "bc"
String str ="abcd" , m=3 得到结果是"abc" "acd" "bcd" "abd"
 

public class CalCountSuan {
    public static List<String> choose(String target, int m) {
        List<String> resultList = new LinkedList<>();
        doChoose(resultList, target, "", m, 0);
        return resultList;
    }
    
    private static void doChoose(List<String> resultList, String target, String resultStr, int m, int head) {
    
        // 递归头
        if (resultStr.length() == m) {
            resultList.add(resultStr);
            return;
        }
    
        // 递归体
        for (int i = head; i < target.length(); i++) {
            doChoose(resultList, target, resultStr + target.charAt(i), m, i+1);
        }
    }
 
    
    public static void main(String[] args) {
        List<String> list = choose("abcdefssss",3);
        HashSet<String> hashSet = new HashSet<>(list);
        System.out.println(hashSet+"/n"+hashSet.size());
        
    }
}

2:计算每个城市个数

 /**
     * 通过Map计算重复值次数
     */  
    public class CalCount {  
          
        private static final Map<Object,Object> map = new HashMap<Object,Object>();
        
        public static void main(String[] args) {  
              
           String s = "长沙,湘潭,湘西,长沙,娄底,株洲,娄底,";  
             
          String [] array = s.split(",");  
          
          if(array.length>0){
              for(int i = 0; i < array.length; i++) {  
                  if(map.containsKey(array[i])) {  
                        
                      Integer count = (Integer) map.get(array[i]);  
                        
                      count++;
                     
                        
                      map.put(array[i], count);  
                        
                  } else {  
                      map.put(array[i], 1);  
                  }  
              }  
                
              System.out.println(map);   
              
          }
          
        }  
    } 

3:

package com.bailiangroup.osp.modules.mp.test;
import java.util.Date;
import org.joda.time.DateTime;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TimeDateObjectMapper {
    private static final ObjectMapper MAPPER = new ObjectMapper();
    public static void main(String [] args){
        
        DateTime time = new DateTime();
        DateTime days = time.minusDays(1).minusMinutes(44);//向前推
        DateTime hours = time.plusHours(2);//向后退
        Date date = hours.toDate();
        System.out.println(date);
          try {
               net.sf.json.JSONObject object = new net.sf.json.JSONObject();
              //这个是去除为空的字段过滤掉  阿里巴巴
              MpUsers mpUsers = new MpUsers();
              String string = net.sf.json.JSONObject.fromObject(mpUsers).toString();
              System.out.println(string);
              String jsonString = JSON.toJSONString(mpUsers);
              System.out.println(jsonString);
//              Object object = MAPPER.readValue("json",MAPPER.getTypeFactory().constructCollectionType(List.class, Object.class));
//              Object readValue = MAPPER.readValue("json",Object.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

 

转义
    public static void main(String[] args) {
        String input="&lt;pre class=&quot;brush: java;&quot;&gt;";
        String script = StringEscapeUtils.unescapeHtml3(input);
        String input1=StringEscapeUtils.escapeHtml3(script);
        
        System.out.println(script);
        System.err.println(input);
        int int1 = RandomUtils.nextInt(1000, 9999);
        System.out.println(int1);
        
        
        
        
    }

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部