Skip to content

Instantly share code, notes, and snippets.

View Chen-tao's full-sized avatar

Chen-tao Chen-tao

  • ByteDance
  • Beijing China
View GitHub Profile
/**
* https://leetcode.com/submissions/detail/90614988/
* 二分 + 确界 判断
*/
public int splitArray(int[] nums, int m) {
long right = 1;//右界是总和
for(int i = 0; i<nums.length; ++i){
right +=nums[i];
}
long left = 0,ans = 0;
//https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
//---------------------------------------------------------------
// one v
public class Solution {
public int kthSmallest(int[][] matrix, int k) {
int n = matrix.length;
long left = matrix[0][0], right = matrix[n - 1][n - 1];
while(left <= right){
//https://leetcode.com/problems/rotate-array/
public static void rotate(int[] arr, int order) {
if (arr == null || arr.length==0 || order < 0) {
throw new IllegalArgumentException("Illegal argument!");
}
if(order > arr.length){
order = order %arr.length;
}
//https://leetcode.com/problems/rotate-array/
public void rotate0(int[] nums, int k) {
if(k > nums.length)
k=k%nums.length;
int[] result = new int[nums.length];
for(int i=0; i < k; i++){
result[i] = nums[nums.length-k+i];
}
//https://leetcode.com/problems/powx-n/
public class Solution {
public double myPow(double x, int n) {
//x^n = e^(x*ln x)
int flag = 1;
if(x < 0 && n%2 != 0){
flag = -1;
}
x = Math.abs(x);
//https://leetcode.com/problems/reverse-words-in-a-string/
public class Solution {
public String reverseWords(String s) {
if (s == null || s.length() == 0) {
return "";
}
StringBuilder ans = new StringBuilder();
s = s.trim().replaceAll(" +", " ");
String[] anss = s.split(" ");
//https://leetcode.com/problems/reverse-words-in-a-string/
// O(1) space v
public void reverseWords(char[] s) {
int i=0;
for(int j=0; j<s.length; j++){
if(s[j]==' '){
reverse(s, i, j-1);
i=j+1;
}
}
//https://leetcode.com/problems/path-sum/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
//https://leetcode.com/problems/permutations/
/**
* 深度优先搜索框架
*/
public class Solution {
public static List<List<Integer>> ans = new ArrayList<List<Integer>>();
public static int[] path = new int[100];
public static boolean[] v = new boolean[100];
package algorithm.sort;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}