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; } }
class Node { public: bool isWord=false; unordered_map<char,Node*> next; Node() { isWord=false; } Node(unordered_map<char,Node*> _next){ next=_next; isWord=false; } }; class Trie { public: Node* root; /** Initialize your data structure here. */ Trie() { root=new Node(); } /** Inserts a word into the trie. */ void insert(string word) { Node* temp=root; for(int i=0;i<word.length();i++){ auto it=temp->next.find(word[i]); if(it==temp->next.end()){ temp->next[word[i]]=new Node(); temp=temp->next[word[i]]; }else temp=temp->next[word[i]];
} temp->isWord=true; } /** Returns if the word is in the trie. */ bool search(string word) { Node* temp=root; for(int i=0;i<word.length();i++){ auto it=temp->next.count(word[i]); if(!it) returnfalse; temp=temp->next[word[i]]; } if(temp->isWord) returntrue; returnfalse; } /** Returns if there is any word in the trie that starts with the given prefix. */ bool startsWith(string prefix) { Node* temp=root; for(int i=0;i<prefix.length();i++){ if(temp->next.count(prefix[i])==0) returnfalse; temp=temp->next[prefix[i]]; } returntrue; } };
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
二叉搜索树的最近公共祖先 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Given binary search tree: root = [6,2,8,0,4,7,9,null,null,3,5]
Example 1:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 Output: 6 Explanation: The LCA of nodes 2 and 8 is 6.
Example 2:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4 Output: 2 Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
Note:
All of the nodes’ values will be unique. p and q are different and both values will exist in the BST.
这是个markdown语法示例,我懒得删了。以后还可以参考下。 Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.