手撕_2
1、1,2...n,中随意取几个数,使其和等于sum,把所有可能组合列出来(和组合总和类似)
package mianjing;
import java.util.ArrayList;
import java.util.List;
public class test_2 {
//1,2...n,中随意取几个数,使其和等于target,把所有可能组合列出来
public static void main(String[] args) {
int n=10;
int target = 20;
if(target<n) {
n=target;
}
List<List<Integer>> result =combinationSum(n,target);
System.out.println(result.toString());
}
static List<List<Integer>> list1 = new ArrayList<>();
public static List<List<Integer>> combinationSum(int n, int target) {
List<Integer> list2 = new ArrayList<>();
dfc(1,n,list2,target);
return list1;
}
public static void dfc(int begin,int len,List<Integer> list2,int target) {
if(target == 0) {
if(!list1.contains(new ArrayList<>(list2))) {
list1.add(new ArrayList<>(list2));
}
return;
}
for(int i = begin; i <= len; i++) {
if(target-i < 0) {
break;
}
list2.add(i);
dfc(i+1,len,list2,target-i);
list2.remove(list2.size()-1);
}
}
}
2、全是数字的字符串相加
public String bigNumberPlus(String a, String b) {
int lenA = a.length();
int lenB = b.length();
if(lenA > lenB) {
b = StringUtils.leftPad(b, lenA, "0");
} else {
a = StringUtils.leftPad(a, lenB, "0");
}
int[] arrC = new int[a.length() + 1];
for(int i = a.length()-1; i>=0; i--) {
int ai = Integer.parseInt(a.charAt(i) + "" );
int bi = Integer.parseInt(b.charAt(i) + "" );
int ci = arrC[i+1];
int t = ai + bi + ci;
arrC[i+1] = t%10;
arrC[i] = t/10;
}
StringBuffer res = new StringBuffer();
for(int i = 0; i<arrC.length; i++) {
if(i==0 && arrC[i]==0) continue;
res.append(arrC[i]);
}
return res.toString();
}
public static String leftPad(String str,int length,String key){
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < length - str.length(); i++) {
stringBuilder.append(key);
}
stringBuilder.append(str);
return stringBuilder.toString();
}
给定一个非空的整数数组,返回其中出现频率前 k 高的元素。
示例 1:
输入: nums = [1,1,1,2,2,3], k = 2
输出: [1,2]
示例 2:
输入: nums = [1], k = 1 输出: [1]
时间复杂度:O(nlogk)
空间复杂度:O(n)O(n)
class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
// 使用字典,统计每个元素出现的次数,元素为键,元素出现的次数为值
HashMap<Integer,Integer> map = new HashMap();
for(int num : nums){
if (map.containsKey(num)) {
map.put(num, map.get(num) + 1);
} else {
map.put(num, 1);
}
}
// 遍历map,用最小堆保存频率最大的k个元素
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return map.get(a) - map.get(b);
}
});
for (Integer key : map.keySet()) {
if (pq.size() < k) {
pq.add(key);
} else if (map.get(key) > map.get(pq.peek())) {
pq.remove();
pq.add(key);
}
}
// 取出最小堆中的元素
List<Integer> res = new ArrayList<>();
while (!pq.isEmpty()) {
res.add(pq.remove());
}
return res;
}
}
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
有效的 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0
),整数之间用 '.'
分隔。
例如:"0.1.2.201" 和 "192.168.1.1" 是 有效的 IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 无效的 IP 地址。
示例 1:
输入:s = "25525511135"
输出:["255.255.11.135","255.255.111.35"]
public static List<String> restoreIpAddresses(String s) {
List<String> ret = new ArrayList<>();
StringBuilder ip = new StringBuilder();
for(int a = 1 ; a < 4 ; ++ a)
for(int b = 1 ; b < 4 ; ++ b)
for(int c = 1 ; c < 4 ; ++ c)
for(int d = 1 ; d < 4 ; ++ d)
{
if(a + b + c + d == s.length() )
{
int n1 = Integer.parseInt(s.substring(0, a));
int n2 = Integer.parseInt(s.substring(a, a+b));
int n3 = Integer.parseInt(s.substring(a+b, a+b+c));
int n4 = Integer.parseInt(s.substring(a+b+c));
if(n1 <= 255 && n2 <= 255 && n3 <= 255 && n4 <= 255)
{
ip.append(n1).append('.').append(n2)
.append('.').append(n3).append('.').append(n4);
if(ip.length() == s.length() + 3) ret.add(ip.toString());
ip.delete(0, ip.length());
}
}
}
return ret;
}
5、查找字符串中的所有的回文子串
中心拓展法O(n^2)
import java.util.ArrayList; public class test5 { /** * @param args */ public static void main(String[] args) { String s="a"; ArrayList<String> res = new ArrayList<>(); for(int i=0;i<s.length();i++){ find(s,i,i,res);//类似 abba find(s,i,i+1,res);//类似abcba } System.out.println(res); } public static void find(String s,int left,int right,ArrayList<String> res){ while(left>=0&&right<s.length()&&s.charAt(left)==s.charAt(right)){ String str=s.substring(left,right+1); if(!res.contains(str)){ res.add(str); System.out.println(left);//打印起始位置 } left--; right++; } } }
6、
给定一个非空的整数数组,返回其中出现频率前 k 高的元素。
示例 1:
输入: nums = [1,1,1,2,2,3], k = 2
输出: [1,2]
示例 2:
输入: nums = [1], k = 1 输出: [1]
import java.util.Map;
import java.util.HashMap;
class Solution {
public int[] topKFrequent(int[] nums, int k) {
int[] res=new int[k];
if(k<=0||k>nums.length)return res;
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<nums.length;i++){
map.put(nums[i],map.getOrDefault(nums[i],0)+1);
}
PriorityQueue<Integer> pq=new PriorityQueue<>(new Comparator<Integer>(){
public int compare(Integer a,Integer b){
return map.get(a)-map.get(b);
}
});
for(Integer key:map.keySet()){
if(pq.size()<k){
pq.offer(key);
}else if(map.get(pq.peek())<map.get(key)){
pq.poll();
pq.offer(key);
}
}
for(int i=pq.size()-1;i>=0;i--){
res[i]=pq.poll();
}
return res;
}
}