java 小组转移了弦乐

例如,给定:[“abc”,“bcd”,“acef”,“xyz”,“az”,“ba”,“a”,“z”] <br/>返回:<br/> [“ abc“,”bcd“,”xyz“],<br/> [”az“,”ba“],<br/> [”acef“],<br/> [”a“,”z“]

HashMap
public List<List<String>> groupStrings(String[] args){
    List<List<String>> res = new ArrayList<>();
    HashMap<String,List<String>> map = new HashMap<>();
    for(String item : args){
        String t = "";
        for(char c : item){
            t += (c+26-a[0])%26+",";
        }
        if(map.get(t)==null)
            map.put(t,new ArrayList<Sring>());
        map.put(t,map.get(t).add(item));
    }
    for(Entry<String,List<String>> entry : map.Entry()){
        res.add(entry.getValue());
    }
    return res;
}

java 独特的BST - CATALAN号码

catalanNumber
//To use less space we could use a hashMap!

class Solution {
    public int numTrees(int n) {
       
        HashMap <Integer, Integer> nthCatalan = new HashMap<>();
        nthCatalan.put(0,1);
        nthCatalan.put(1,1);
        nthCatalanNumber(n, nthCatalan);
        
        return nthCatalan.get(n);
        
    
    }
    
    public void nthCatalanNumber(int n, HashMap<Integer, Integer> nthCatalan){            
        //TODO: Body of recursion
        int nth = 0;
        int smaller, bigger;
        for(int i=1; i<=n; i++){
            
            if(!nthCatalan.containsKey(i-1)) nthCatalanNumber(i-1, nthCatalan);
            smaller = nthCatalan.get(i-1);
            
            if(!nthCatalan.containsKey(n-i)) nthCatalanNumber(n-i, nthCatalan);
            bigger = nthCatalan.get(n-i);
            
            nth += smaller*bigger;
        }  
        
        nthCatalan.put(n, nth);
        return;
    }
}

java 比较版本号 - 数字版本比较

输入:version1 =“0.1”,version2 =“1.1”输出:-1;输入:version1 =“1.0.1”,version2 =“1”输出:1;输入:version1 =“7.5.2.4”,version2 =“ 7.5.3“输出:-1

return
public static int compareDigit(String a,String b){
	    int i = 0 , j= 0 ;
	    while(i < a.length() && j < b.length()){
	        if(a.charAt(i)=='.' && b.charAt(j)=='.') {
	        	i++;j++;continue;
	        }
	        else if(a.charAt(i)=='.')
	            return 1;
	        else if(b.charAt(j)=='.')
	            return -1;
	        else{
	            int a_ = Integer.valueOf(a.charAt(i));
	            int b_ = Integer.valueOf(b.charAt(j));
	            if(a_>b_) return 1;
	            else if(a_ < b_) return -1;
	            else {
	            	i++;j++;
	            }
	        }
	    }
	    if(i==a.length()) return -1;
	    else return 1;
	}

java 反转整数

reverse Digits
public int reverseDigit(int num){
    if(num==0) return 0;
    int flag = num<0?-1:1;
    num = Math.abs(num);
    int res = 0 ;
    int temp = num;
    while(temp!=0){
        int cur = temp%10;
        res = res*10+cur;
        temp = temp/10;
    }
    return flag*res;
}

java 字模式 - leetcode字符串

例如输入:pattern =“abba”,str =“dog cat cat dog”输出:true;输入:pattern =“abba”,str =“dog cat cat fish”输出:false。<br/>采用一个HashMap来存储输入中字符与STR中字符串对应关系,对于一个新的字符先检查有没有在HashMap中中,如果在则去除值与当前STR中字符串对比相同就继续下一个字符不同直接假,对于没出现在的HashMap中的新字符则需要遍历一遍HashMap中来检查当前STR中该字符串有没有在HashMap中的值中出现过,如果有则假,没有加入对

hashmap
public boolean wordPattern(String word,String[] pattern){
  if(word.length()!=pattern.length) return false;
  HashMap<Character,String> map = new HashMap<>();
  for(int i = 0 ; i < word.length(); i++){
    char cur = map.get(word.charAt(i));
    if(cur!=null && !map.get(cur).equals(pattern[i]) return false;
    else if(cur!=null){
      for(Entry<Character,String> item : map.entrySet()){
        if(pattern[i].equals(item.getValue())) return false;
      }
    }else if(cur==null){
      map.put(cur,pattern[i]);
    }
  }
  return true;
}

java 最长公共字符串前缀 - 最长公共前缀

nm
//暴力法,以第一个字符串为检测中心来实验,遍历其他字符串看看是否当前第一个字符串所处的位置i是否不超过该字符串长度并且前缀相同。
	public static String LongestCommonPrefix(String[] dictor){
	    if(dictor==null || dictor.length==0) return "";
	    int size = dictor.length;
	    StringBuilder res = new StringBuilder();
	    for(int i = 0 ; i < dictor.length ; i++){
	        //针对第一个字符串来衡量
	    	int j = 1;
	        for(; j < size; j++){
	            if(i>= dictor.length ||  dictor[j].charAt(i)!=dictor[0].charAt(i)) break;        
	        }
	        if(j==size) res.append(dictor[0].charAt(i));
	        else break;
	    }
	    return res.toString();
	}
recurrent
//先检测第一个和第二个字符串和第二个字符串公共前缀,然后用前缀和第三个字符串检测。
	public static String LongestCommonPrefix2(String[] dictor) {
		if(dictor==null || dictor.length==0) return "";
		String prev = dictor[0];
		for(int i = 1; i < dictor.length; i++) {
			String cur = dictor[i];
			prev = commonPrefixOfBothString(prev,cur);
		}
		return prev;
	}
	public static String commonPrefixOfBothString(String prev,String cur) {
		StringBuilder res = new StringBuilder();
		for(int i = 0 ; i < prev.length(); i++) {
			if(i < cur.length() && cur.charAt(i)==prev.charAt(i)) res.append(prev.charAt(i));
			else break;
		}
		return res.toString();
	}

java Udacity _简单的HTTP请求

NetworkUtils
public static String getResponseFromHttpUrl(URL url) throws IOException {
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        try {
            InputStream in = urlConnection.getInputStream();

            Scanner scanner = new Scanner(in);
            scanner.useDelimiter("\\A");

            boolean hasInput = scanner.hasNext();
            if (hasInput) {
                return scanner.next();
            } else {
                return null;
            }
        } finally {
            urlConnection.disconnect();
        }
    }

java KMP算法解决字符串包含问题

kmp
//KMP算法
详见 https://blog.csdn.net/yutianzuijin/article/details/11954939/

java 摆动子序列

例如[3,5,1,10,9,12]这种相邻两个数之间的差距呈现正负数交替的情况的数组叫做“wiggle array”。给定一个数组,求其中最长的wiggle子序列,子序列不一定每个元素相邻。<br/>思路:这些问题有通用的解法,对于子序列问题可以令数组dp [i]表示以第i个数字结尾的自序列的最长(小)长度,那么有dp [i] = dp [k] + 1,k = 0,1,2 ...,i-1,只要数组第k个数字满足条件。最后的答案取dp中的最大(小)值。但是这道题需要加另一个数组才可以,记录nums [i]所处的最长的wiggle子序列中相较于前一个数是上升还是下降,因为后面要用到这个来构成wiggle子序列。而且这种题的最终答案不是最后一个数字,而是需要在循环中记录比较。

wiggle subseq
public int longestWiggleSubseq(int [] nums){
  if(nums==null || nums.length ==0 ) return 0;
  int [] dp = new int[nums.length]{1};
  int [] sign = new int[nums.length];//+1 up,-1 down
  int res = Integer.MIN_VALUE;
  for(int i = 1; i < nums.length; i++){
    int pre_pos = i;
    for(int j = i-1; j >= 0 ; j--){
      if((j==0 || sign[j]*(nums[i]-nums[j])<0) && dp[i]<dp[j]+1){
        pre_pos = j;
        dp[i]=dp[j]+1;
      }
    }
    sign[i]=nums[i]>nums[pre_pos]?1:-1;
    res = Math.max(res,dp[i]);
  }
  return res;
}

java 复制,删除,从存档中删除文件并解压缩

archive withount unpacking
Map<String, String> zip_properties = new HashMap<>();
/* We want to read an existing ZIP File, so we set this to False */
zip_properties.put("create", "false");

/* Specify the path to the ZIP File that you want to read as a File System */
URI zip_disk = URI.create("jar:file:/D:/kms-trunk/KMS-Installer/build/distributions/KMS-Installer-6.0.jar");

/* Create ZIP file System */
try (FileSystem zipfs = FileSystems.newFileSystem(zip_disk, zip_properties)) {
    /* Get the Path inside ZIP File to copy the ZIP Entry */
    Path pathInZipfile = zipfs.getPath("server.kms.xml");
    System.out.println("About to copy an entry from ZIP File" + pathInZipfile.toUri());
    /* Execute Delete */
    Files.copy(pathInZipfile, Paths.get("C:\\Users\\IvanBelorechev\\Downloads\\server.kms.xml.my"));
    System.out.println("File successfully copied");
}