美文网首页
10个算法题目

10个算法题目

作者: 啊磊11 | 来源:发表于2021-03-19 23:17 被阅读0次

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();

    }

}

相关文章

  • 算法题目

    一、 分析: 一开始想多了,用了DFS回溯法,然后显示超出内存,可能是当n取值很大时递归太多的原因吧 然后又去搜索...

  • 算法题目

    ZERO 持续更新 请关注:https://zorkelvll.cn/blogs/zorkelvll/artic...

  • 算法题目

  • 图的结构 BFS DFS

    题目:BFS 一个队列,一个set集合 题目:DFS 题目:Kruskal算法 题目:Prim Dijkstra算法

  • 数据算法题目

    [单选题] 1000 个瓶子中有一瓶毒药,一只老鼠吃到毒药一周之内会死,如果要在一周之内检测出有毒药的一瓶,问至...

  • 算法题目集

    1、来自网易:为了找到自己满意的工作,牛牛收集了每种工作的难度和报酬。牛牛选工作的标准是在难度不超过自身能力值的情...

  • 基础算法题目

    爬楼梯 编辑距离 小顶堆 topk 快排序 二分查找 斐波那契数列 两数之和 最大回溯 题目形式:有一个数组,求其...

  • 2019算法题目

    讲完了基本的算法和数据结构之后,准备深入地讲解一下。 https://blog.csdn.net/qq_41681...

  • 算法设计题目

    括号匹配问题 假设表达式中运行包含两种括号:圆括号和方括号,其嵌套顺序随意。即()或者[([][])]都是正确的,...

  • 新手算法题目

    数组 Array力扣 485最大连续1的个数 | Max Consecutive One力扣 283 移动零 |...

网友评论

      本文标题:10个算法题目

      本文链接:https://www.haomeiwen.com/subject/lccicltx.html