标签:lis string com add ima def == rac style
在此祝各位同仁,小年快乐!
package com.qyx.Tree; import java.util.ArrayList; import java.util.List; /** * * @author Q * */ public class PhoneNumber { /*使用二叉树完成题解*/ private String letterMap[] = { " ", //0 "", //1 "abc", //2 "def", //3 "ghi", //4 "jkl", //5 "mno", //6 "pqrs", //7 "tuv", //8 "wxyz" //9 }; private ArrayList<String> res; private void findCombination(String digits,int index,String s) { if(index==digits.length()) { /*长度和电话号码长度相等直接加入list,退出递归*/ res.add(s); return; } Character c=digits.charAt(index); /*寻找数字对应的字母们*/ String letters=letterMap[c-‘0‘]; for(int i=0;i<letters.length();i++){ findCombination(digits, index+1, s+letters.charAt(i)); } return; } public List<String> letterCombinations(String digits) { res=new ArrayList<>(); if(digits.equals("")) { return res; } findCombination(digits, 0, ""); return res; } }
package com.qyx.Tree; import java.util.Arrays; /** * * @author Q * */ public class ThreeNumberSum { /*使用双指针*/ public int threeSumClosest(int[] nums,int target) { //对数组进行排序,花费时间复杂度O(logN) Arrays.sort(nums); int ans=nums[0]+nums[1]+nums[2]; /*花费时间复杂度O(N^2)*/ for(int i=0;i<nums.length;i++) { /*头指针在i+1处*/ int start=i+1; /*尾指针在nums.length-1*/ int end=nums.length-1; while(start<end) { int sum=nums[i]+nums[start]+nums[end]; if(Math.abs(target-sum)<Math.abs(target-ans)) { //如果sum的距离里target则更新ans的值 ans=sum; }else if(sum<target) { start++; }else if(sum>target) { end--; }else{ return ans; } } } return ans; } }
标签:lis string com add ima def == rac style
原文地址:https://www.cnblogs.com/qyx66/p/12207206.html