《1.9-1.15 Leetcode》

六眼飞鱼酱① 提交于 2020-01-24 10:55:56

1. 路径总和

题目描述:
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
说明: 叶子节点是指没有子节点的节点。

代码:

package leetcode.week4;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-14 17:09
 * @desc 路径总和
 */
public class t112 {
    public static boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        sum -= root.data;
        // 判断当前节点是否为叶子节点
        if (root.left == null && root.right == null) {
            if (sum != 0) {
                return false;
            } else {
                return true;
            }
        }
        // 当前节点的子节点是否有一个满足要求
        return hasPathSum(root.left, sum) || hasPathSum(root.right, sum);
    }


    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(2);
        TreeNode node4 = new TreeNode(3);
        TreeNode node5 = new TreeNode(4);
        TreeNode node6 = new TreeNode(4);
        TreeNode node7 = new TreeNode(3);

        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;

        System.out.println(hasPathSum(node1, 7));

    }
}

2. 杨辉三角

题目描述:
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
在这里插入图片描述
在杨辉三角中,每个数是它左上方和右上方的数的和。

代码:

package leetcode.week4;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-14 17:39
 * @desc 杨辉三角
 */
public class t118 {
    public static List<List<Integer>> generate(int numRows) {
        List<List<Integer>> lists = new ArrayList<>();
        // 如果输入小于等于0,则返回空List
        if (numRows < 1) {
            return lists;
        }
        // 初始化第一行
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        lists.add(arrayList);
        // 动态规划
        for (int i = 1; i < numRows; i++) {
            ArrayList<Integer> temp = (ArrayList<Integer>) lists.get(i - 1);
            ArrayList<Integer> newList = new ArrayList<>();
            newList.add(1);
            for (int j = 0; j < temp.size() - 1; j++) {
                newList.add(temp.get(j) + temp.get(j + 1));
            }
            newList.add(1);
            lists.add(newList);
        }
        return lists;
    }

    public static void main(String[] args) {
        List<List<Integer>> res = generate(10);
        for (List<Integer> list : res) {
            for (Integer num : list) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
}

3. 验证回文串

题目描述:
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。

代码:

package leetcode.week4;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-14 19:55
 * @desc 验证回文串
 */
public class t125 {
    /**
     * 验证一个字符串是否是回文字符串
     *
     * @param s
     * @return
     */
    public static boolean isPalindrome(String s) {
        if (s == null || s.length() == 0) {
            return true;
        }
        s = s.toUpperCase();
        int left = 0;
        int right = s.length() - 1;
        while (left < right) {
            // 过滤左右两端的非数字和字母
            if (!isNumOrAlp(s.charAt(left))) {
                left++;
                continue;
            }
            if (!isNumOrAlp(s.charAt(right))) {
                right--;
                continue;
            }
            if (s.charAt(left) != s.charAt(right)) {
                break;
            }
            left++;
            right--;
        }
        if (left >= right) {
            return true;
        }
        return false;
    }

    /**
     * 判断一个字符是否为数字或字母
     *
     * @param character
     * @return
     */
    public static boolean isNumOrAlp(Character character) {
        return (character >= 'A' && character <= 'Z') ||
                (character >= '0' && character <= '9');
    }


    public static void main(String[] args) {
        System.out.println(isPalindrome("A man, a plan, a canal: Panama"));

    }
}

4. 只出现一次的次数

题目描述:
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
说明:
你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

代码:

package leetcode.week4;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-15 14:02
 * @desc 只出现一次的数字
 */
public class t136 {
    public static int singleNumber(int[] nums) {
        int result = nums[0];
        for (int i = 1; i < nums.length; i++) {
            result ^= nums[i];
        }
        return result;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 2, 3, 3, 4, 4};
        System.out.println(singleNumber(arr));
    }
}

5. 环形链表

题目描述:
给定一个链表,判断链表中是否有环。
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

代码:

package leetcode.week4;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-15 14:11
 * @desc 环形链表
 */
public class t141 {
    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        listNode1.next = listNode2;
        System.out.println(hasCycle(listNode1));
    }

    public static boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }
        ListNode head1 = head;
        ListNode head2 = head;
        while (head1 != null && head1.next != null && head2 != null) {
            head2 = head2.next;
            head1 = head1.next.next;
            if (head1 == head2) {
                return true;
            }
        }
        return false;
    }
}


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!