第37天--算法(牛客网剑指offer4)

1.JZ33 二叉搜索树的后序遍历序列

public boolean VerifySquenceOfBST(int [] sequence) {
  if(sequence == null || sequence.length == 0) {
    return false;
  }
  return process(sequence,0,sequence.length - 1);
}
public boolean process(int [] sequence,int L,int R) {
  if(L >= R) {
    return true;
  }
  int index = 0;
  for(int i = L;i < R;i ++) {
    if(sequence[i] <= sequence[R]) {
      index ++;
    }else {
      break;
    }
  }
  for(int i = L + index;i < R;i ++) {
    if(sequence[i] < sequence[R]) {
      return false;
    }
  }
  return process(sequence,L,L + index - 1) && process(sequence,L + index,R - 1);
}

2.JZ34 二叉树中和为某一值的路径(二)

ArrayList<ArrayList<Integer>> res = new ArrayList<>();
ArrayList<Integer> cur = new ArrayList<>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int expectNumber) {
  if(root == null) {
    return res;
  }
  cur.add(root.val);
  expectNumber -= root.val;
  if(expectNumber == 0 && root.left == null && root.right == null) {
    res.add(new ArrayList<>(cur));
  }
  FindPath(root.left,expectNumber);
  FindPath(root.right,expectNumber);
  cur.remove(cur.size() - 1);
  return res;
}

 3.JZ35 复杂链表的复制

public RandomListNode Clone(RandomListNode pHead) {
  if(pHead == null) {
    return null;
  }
  RandomListNode temp1 = pHead;
  while(temp1 != null) {
    RandomListNode cur = new RandomListNode(temp1.label);
    cur.next = temp1.next;
    temp1.next = cur;
    temp1 = cur.next;
  }
  temp1 = pHead;
  RandomListNode temp2 = pHead.next;
  while(temp1 != null) {
    temp2.random = temp1.random == null ? null : temp1.random.next;
    temp1 = temp1.next.next;
    temp2 = temp2.next == null ? null : temp2.next.next;
  }
  temp1 = pHead;
  temp2 = pHead.next;
  RandomListNode res = pHead.next;
  while(temp1 != null) {
    temp1.next = temp2.next;
    temp1 = temp1.next;
    temp2.next = temp1 == null ? null : temp1.next;
    temp2 = temp2.next;
  }
  return res;
}

4.JZ36 二叉搜索树与双向链表

public TreeNode Convert(TreeNode pRootOfTree) {
  if(pRootOfTree == null) {
    return null;
  }
  LinkedList<Integer> ls = new LinkedList<>();
  process(pRootOfTree,ls);
  TreeNode res = new TreeNode(ls.pollFirst());
  TreeNode cur = res;
  while(!ls.isEmpty()) {
    TreeNode temp = new TreeNode(ls.pollFirst());
    temp.left = cur;
    cur.right = temp;
    cur = temp;
  }
  return res;
}
public void process(TreeNode root,LinkedList<Integer> ls) {
  if(root == null) {
  return;
  }
  process(root.left,ls);
  ls.addLast(root.val);
  process(root.right,ls);
}

5.JZ38 字符串的排列

public ArrayList<String> Permutation(String str) {
  ArrayList<String> res = new ArrayList<>();
  char s[] = str.toCharArray();
  process(res,s,0);
  return res;
}
public void process(ArrayList<String> res,char s[],int index) {
  if(index == s.length) {
    res.add(String.valueOf(s));
  }else {
    HashSet<Character> hs = new HashSet<>();
    for(int j = index;j < s.length;j ++) {
      if(!hs.contains(s[j])) {
        swap(s,j,index);
        process(res,s,index + 1);
        swap(s,j,index);
        hs.add(s[j]);
      }
    }
  }
}
public void swap(char arr[],int x,int y) {
  char temp = arr[x];
  arr[x] = arr[y];
  arr[y] = temp;
}

6.JZ39 数组中出现次数超过一半的数字

public int MoreThanHalfNum_Solution(int [] array) {
  if(array == null || array.length == 0) {
    return 0;
  }
  int res = array[0];
  int times = 1;
  for(int i = 1;i < array.length;i ++) {
    if(array[i] == res) {
      times ++;
    }else {
      if(times > 0) {
        times --;
      }else {
        res = array[i];
        times = 1;
      }
    }
  }
  return res;
}

7.JZ40 最小的K个数

public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
  if(input == null || input.length < k || k < 1) {
    return new ArrayList<>();
  }
  PriorityQueue<Integer> queue = new PriorityQueue<>((o1,o2) -> o2 - o1);
  for(int i = 0;i < input.length;i ++) {
    if(queue.size() < k) {
      queue.add(input[i]);
    }else {
      if(input[i] < queue.peek()) {
        queue.poll();
        queue.add(input[i]);
      }
    }
  }
  return new ArrayList<>(queue);
}

8.JZ41 数据流中的中位数

PriorityQueue<Integer> max = new PriorityQueue<>((o1,o2) -> o2 - o1);
PriorityQueue<Integer> min = new PriorityQueue<>();
int count = 0;
public void Insert(Integer num) {
  if((count & 1) == 0) {
    if(max == null) {
      min.add(num);
    }else {
      max.add(num);
      min.add(max.poll());
    }
  }else {
    if(min == null) {
      max.add(num);
    }else {
      min.add(num);
      max.add(min.poll());
    }
  }
  count ++;
}

public Double GetMedian() {
  if(max.size() != min.size()) {
    return max.size() > min.size() ? (double) max.peek() : (double) min.peek();
  }else {
    return (min.peek() + max.peek()) / 2.0;
  }
}

9.JZ42 连续子数组的最大和

public int FindGreatestSumOfSubArray(int[] array) {
  if(array == null || array.length == 0) {
  return 0;
  }
  int max = Integer.MIN_VALUE;
  int cur = 0;
  for(int i = 0;i < array.length;i ++) {
    if(cur < 0) {
      cur = 0;
    }
    cur += array[i];
    max = Math.max(max,cur);
  }
  return max;
}

10.JZ43 整数中1出现的次数(从1到n整数中1出现的次数)

public int NumberOf1Between1AndN_Solution(int n) {
  int base = 1;
  int ans = 0;
  while(base <= n) {
    int a = n / (base * 10);
    int b = n % base;
    int cur = n / base % 10;
    if(cur < 1) {
      ans += a * base;
    }else if(cur == 1) {
      ans += a * base + b + 1;
    }else {
      ans += (a + 1) * base;
    }
    base *= 10;
  }
  return ans;
}

posted @ 2022-02-28 15:09  现在开始努力  阅读(18)  评论(0)    收藏  举报