/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Codec {
// Encodes a tree to a single string. public String serialize(TreeNode root) { Queue<TreeNode> queue=new LinkedList<>(); StringBuffer res=new StringBuffer(); if(root==null) return res.toString(); res.append(String.valueOf(root.val)); queue.offer(root.left); queue.offer(root.right); while(!queue.isEmpty()) { int size=queue.size(); for(int i=0;i<size;i++){ TreeNode t=queue.poll(); if(t==null){ res.append(",null"); } else{ res.append(","); res.append(String.valueOf(t.val)); queue.offer(t.left); queue.offer(t.right); } } } //System.out.println(res); return res.toString(); }
// Decodes your encoded data to tree. public TreeNode deserialize(String data) { if(data.length()==0) return null; String[] s=data.split(","); int n=s.length; for(int i=s.length-1;i>=0;i--) if(s[i].equals("null")) n--; else break; // for(int i=0;i<n;i++) // System.out.println(s[i]);
class Solution { public int trailingZeroes(int n) { int max=0; int t=n; while(t!=0){ t=t/5; max++; } int res=0; for(int i=1;i<max;i++) res+=n/Math.pow(5,i); return res; } }
public String longestPalindrome(String s) { if (s == null || s.length() < 1) return ""; int start = 0, end = 0; for (int i = 0; i < s.length(); i++) { int len1 = expandAroundCenter(s, i, i); int len2 = expandAroundCenter(s, i, i + 1); int len = Math.max(len1, len2); if (len > end - start) { start = i - (len - 1) / 2; end = i + len / 2; } } return s.substring(start, end + 1); }
private int expandAroundCenter(String s, int left, int right) { int L = left, R = right; while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) { L--; R++; } return R - L - 1; }
class Solution { int[] Arr; int[] Nature; public Solution(int[] nums) { Arr=nums; Nature= Arrays.copyOf(Arr, Arr.length); } /** Resets the array to its original configuration and return it. */ public int[] reset() { for(int i=0;i<Arr.length;i++) Arr[i]=Nature[i]; return Arrays.copyOf(Arr, Arr.length); } /** Returns a random shuffling of the array. */ public int[] shuffle() { for(int i=Arr.length-1;i>0;i--){ Random rand=new Random(); int t=rand.nextInt(i+1); //随机数生成范围要注意 int temp=Arr[t]; Arr[t]=Arr[i]; Arr[i]=temp; } return Arr; } }
/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * int[] param_1 = obj.reset(); * int[] param_2 = obj.shuffle(); */
// class Solution { // int[] nums;
// public Solution(int[] nums) { // this.nums = nums; // } // /** Resets the array to its original configuration and return it. */ // public int[] reset() { // return nums; // } // /** Returns a random shuffling of the array. */ // public int[] shuffle() { // int[] result = Arrays.copyOf(nums, nums.length); // Random rand=new Random(); // for(int i = nums.length - 1; i > 0; i--) { // int n = rand.nextInt(i + 1); // // swap n and i // int tmp = result[n]; // result[n] = result[i]; // result[i] = tmp; // } // return result; // } // }
class Solution { public int countPrimes(int n) { int res=0; for(int i=1;i<n;i++) if(isPrime(i)) res++; return res; } public static boolean isPrime(int num) { if (num <= 3) { return num > 1; } // 不在6的倍数两侧的一定不是质数 if (num % 6 != 1 && num % 6 != 5) { returnfalse; } int sqrt = (int) Math.sqrt(num); for (int i = 5; i <= sqrt; i += 6) { if (num % i == 0 || num % (i + 2) == 0) { returnfalse; } } returntrue; }
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int count = 0; while (n != 0) { count++; n = n & (n - 1); } return count; } }