/**
* 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);
}
}