10个算法题目
1、两数之和
public static int[]TaskOne(int[] nums, int target){
HashMap hashMap =new HashMap<>();
for(int i =0;i
hashMap.put(nums[i],i);
}
for(int i =0;i
if(hashMap.containsKey(target - nums[i]) && hashMap.get(target - nums[i])!= i){
return new int[]{nums[i],nums[hashMap.get(target-nums[i])]};
}
}
return new int[]{-1,-1};
}
2、两数相加
public static LinkedLISTTasktwo(LinkedLIST task1, LinkedLIST task2){
LinkedLIST dummy =new LinkedLIST(-1,null);
LinkedLIST curr = dummy;
int carry =0;
while (task1 !=null || task2 !=null || carry !=0){
int task1value = task1 ==null ?0:task1.value;
int task2value = task2 ==null ?0:task2.value;
int kk = (task1value + task2value + carry)%10;
carry = (task1value + task2value + carry)/10;
LinkedLIST node =new LinkedLIST(kk,null);
curr.next = node;
curr = curr.next;
if (task1 !=null){
task1= task1.next;
}
if (task2 !=null){
task2= task2.next;
}
}
return dummy.next;
}
3、无重复字符最长子串
public static StringTaskThree(String task3){
int left =0;
int right =0;
HashMap windows =new HashMap<>();
int max = Integer.MIN_VALUE;
int index =0;
while (right < task3.length()){
char cc = task3.charAt(right);
right++;
windows.put(cc,windows.getOrDefault(cc,0) +1);
while (windows.get(cc) >1){
char dd =task3.charAt(left);
left++;
windows.put(dd,windows.get(dd) -1);
}
if (max < (right - left)){
max = right - left;
index = left;
}
}
return task3.substring(index,index+max);
}
4、2个正序中位数
public static int Task4(int[] nums1, int[] nums2){
int alen = nums1.length;
int blen = nums2.length;
int left =0;
int right =0;
int astart =0;
int bstart =0;
for(int i =0;i<=(alen + blen)/2;i++){
left = right;
if(astart < alen && blen
right = nums1[astart ++];
}else if(astart= bstart){
right = nums1[astart++];
}else{
right = nums2[bstart++];
}
}
if((alen+blen)%2 ==0){
return (left+right)/2;
}else {
return right;
}
}
public static StringTask5(String task5){
boolean[][] dp =new boolean[task5.length()][task5.length()];
int max = Integer.MIN_VALUE;
int index =0;
for(int i =0;i
for(int j =0;j<=i;j++){
if(i-j <2){
dp[i][j] = task5.charAt(i) == task5.charAt(j);
if(dp[i][j]){
if (max
max = i-j+1;
index = j;
}
}
}else{
if (task5.charAt(i) == task5.charAt(j)){
dp[i][j] = dp[i-1][j+1];
if(dp[i][j]){
if (max
max = i-j+1;
index = j;
}
}
}
}
}
}
return task5.substring(index,index+max);
}
public static boolean Task6(int num){
if (num<0){
return false;
}
if(num ==0){
return true;
}
ArrayList path =new ArrayList();
while (num !=0){
int k = num%10;
path.add(k);
num = num/10;
}
for(int i =0;i
if(path.get(i) != path.get(path.size()-1-i)){
return false;
}
}
return true;
}
public static int Task7(int[] nums){
int left =0;
int right = nums.length-1;
int max = Integer.MIN_VALUE;
while (left
if(nums[left]
int value = (right - left)* nums[left];
left++;
max=Math.max(max,value);
}else{
int value = (right - left)* nums[right];
right++;
max=Math.max(max,value);
}
}
return max;
}
public static StringTask8(String[] task8){
int len = task8.length;
if (len ==1){
return task8[0];
}
for(int i =0;i
char c = task8[0].charAt(i);
for(int j=1;j
if (i == task8[j].length()||c != task8[j].charAt(i)){
return task8[0].substring(0,i);
}
}
}
return task8[0];
}
public static LinkedLISTTask9(LinkedLIST heada,int n){
LinkedLIST dummy =new LinkedLIST(-1,null);
dummy.next = heada;
LinkedLIST fast = heada;
LinkedLIST slow = dummy;
for(int i =0;i
fast = fast.next;
}
while (fast !=null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
public static boolean Task10(String task10){
HashMap hashmap =new HashMap<>();
hashmap.put('(',')');
hashmap.put('{','}');
hashmap.put('[',']');
Stack stack =new Stack<>();
for(int i =0;i
if(hashmap.containsKey(task10.charAt(i))){
stack.push(task10.charAt(i));
}else{
if(stack.size() ==0 || hashmap.get(stack.pop()) != task10.charAt(i)){
return false;
}
}
}
return stack.isEmpty();
}
}