141.环形链表 | 647.回文子串 | 128.最长连续序列 | 124.二叉树的最大路径和

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null || head.next == null){
            return false;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        
        while(slow != fast){
            if (fast == null || fast.next == null) {
                return false;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        return true;
    }
}
class Solution {
    public int countSubstrings(String s) {
        int res = 0;
        List<String> list = Arrays.asList(s.split(""));

        for(int i = 0;i<list.size();i++){
           
            //两个一样
            int j = 0;
            while(i-j>=0 && i+j < list.size() && list.get(i-j).equals(list.get(i+j))){
                res++;
                j++;
            }
            j = 0;
            while(i-j>=0 && i+j+1 < list.size() && list.get(i-j).equals(list.get(i+j+1))){
                res++;
                j++;
            }        
        }
        return res;
    }
}
class Solution {
    public int longestConsecutive(int[] nums) {
        if(nums.length == 0 || nums== null){
            return 0;
        }
        Arrays.sort(nums);
        int res = 1;
        int lon = 1;
        for(int i = 1;i<nums.length;i++){
            if(nums[i] == nums[i-1]){
                continue;
            }else if(nums[i] == nums[i-1]+1){
                res++;
            }else{
                res = 1;
            }
            lon = Math.max(lon,res);
        }
        return lon;
    }
}
class Solution {
    private int maxval = Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
       CalSum(root);
       return maxval;
    }
    public int CalSum(TreeNode root){
        if(root == null){
            return 0 ;
        }
        int leftmax = Math.max(CalSum(root.left),0);
        int rightmax = Math.max(CalSum(root.right),0);

        int curSum = root.val + leftmax+ rightmax;
        maxval=Math.max(maxval, curSum);
        return root.val + Math.max(leftmax,rightmax);
    }

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值