LintCode第十六天

376. 二叉树的路径和

给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径。

一个有效的路径,指的是从根节点到叶节点的路径。

样例 给定一个二叉树,和 目标值 = 5:

1
/ \
2 4
/ \
2 3
返回:

[
[1, 2, 2],
[1, 4]
]
AC

public List<List<Integer>> binaryTreePathSum(TreeNode root,int target){
        List<List<Integer>> result=new ArrayList<List<Integer>>();
        if(root==null) return result;
        Stack<Integer> stack =new Stack<>();
        binaryTreePathSum(result,stack,root,0,target);
        return result;
    }
    public void binaryTreePathSum(List<List<Integer>> result, Stack<Integer> stack, TreeNode root, int sum, int target) {
        sum += root.val;
        stack.push(root.val);
        if(sum == target && root.left == null && root.right == null) {
            List<Integer> list = new ArrayList<Integer>(stack);
            result.add(list);
            stack.pop();
            return;
        }else {
            if(root.left != null)
                binaryTreePathSum(result, stack, root.left, sum, target);
            if(root.right != null)
                binaryTreePathSum(result, stack, root.right, sum, target);
            stack.pop();
        }
    }

397. Longest Continuous Increasing Subsequence

给定一个整数数组(下标从 0 到 n-1, n
表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。)

样例
给定 [5, 4, 2, 1, 3], 其最长上升连续子序列(LICS)为 [5, 4, 2, 1], 返回 4.

给定 [5, 1, 2, 3, 4], 其最长上升连续子序列(LICS)为 [1, 2, 3, 4], 返回 4.

AC

public int longestIncresingContinuousSubsequence(int[]A){
        if(A.length==0||A.length==1)
            return A.length;
        int result=Integer.MIN_VALUE;
        int count=1;
        for(int i=1;i<A.length;i++){
            if(A[i]>A[i-1])
                count++;
            else count=1;
            result=Math.max(result,count);
        }
        count=1;
        for(int i=A.length-2;i>=0;i--){
            if(A[i]>A[i+1])
                count++;
            else count=1;
            result=Math.max(result,count);
        }
        return result;
        }

407. 加一

给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组。

该数字按照数位高低进行排列,最高位的数在列表的最前面。

样例
给定 [1,2,3] 表示 123, 返回 [1,2,4].

给定 [9,9,9] 表示 999, 返回 [1,0,0,0].

public int[] plusOne(int[] digits){
        int carry=0;
        LinkedList<Integer> result=new LinkedList<>();
        for(int i=0;i<digits.length;i++)
            result.add(digits[i]);
        for(int i=result.size()-1;i>=0;i--){
            int sum=result.get(i)+carry;
            carry=sum/10;
            result.set(i,sum%10);
        }
        if(carry==1)
            result.addFirst(carry);
        int RESULT[]=new int[result.size()];
        for(int i=0;i<result.size();i++)
            RESULT[i]=result.get(i);
        return RESULT;
    }

408. 二进制求和

给定两个二进制字符串,返回他们的和(用二进制表示)。

样例
a = 11

b = 1

返回 100

AC

public String addBinary(String a,String b){
        if(a.equals("0"))
            return b;
        if(b.equals("0"))
            return a;
        int sumA=0;
        for(int i=0;i<a.length();i++){
            int A=(a.charAt(i)-'0');
            sumA=2*sumA+A;
        }
        int sumB=0;
        for (int i=0;i<b.length();i++){
            int B=b.charAt(i)-'0';
            sumB=2*sumB+B;
        }
        int sum=sumA+sumB;
        String result="";
        while (sum!=0){
            result=sum%2+result;
            sum=sum/2;
        }
        return result;
        }

413. 反转整数

将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。

样例
给定 x = 123,返回 321

给定 x = -123,返回 -321
AC

public int reverseInteger(int n){
        if(n==0)
            return 0;
        boolean negative=false;
        if(n<0) {
            negative = true;
            n=-n;
        }
        String result="";
        while (n!=0){
            int N=n%10;
            n/=10;
            result+=N;
        }
        long temp=Long.parseLong(result);

        if(temp-Integer.MAX_VALUE>0)
            return 0;
        else {
            int sum=0;
            for(int i=0;i<result.length();i++)
                sum=10*sum+(result.charAt(i)-'0');
            if(negative)
                return -sum;
            else return sum;
        }

猜你喜欢

转载自blog.csdn.net/Mikeoperfect/article/details/80383589