useContext is a React Hook that lets you read and subscribe to context from your component.
深層子組件取用母組件更新後的值
官方範例
import { createContext, useContext } from 'react';
const ThemeContext = createContext(null);
export default function MyApp() {
Boris Chien changed a year agoView mode Like Bookmark
權限控制模型 ACL、DAC、MAC、RBAC、ABAC
ACL 存取控制列表
資源可以被哪些主體進行哪些操作
DAC 自主存取控制
規定資源可以被哪些主體進行哪些操作同時,主體可以將資源、操作的權限,授予其他主體
MAC 強制存取控制
a. 規定資源可以被哪些類別的主體進行哪些操作
b. 規定主體可以對哪些等級的資源進行哪些操作當一個操作,同時滿足a與b時,允許操作
Boris Chien changed a year agoView mode Like Bookmark
You are given a string num, representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num, or an empty string "" if no odd integer exists.
A substring is a contiguous sequence of characters within a string.
var largestOddNumber = function (num) {
for (let i = num.length - 1; i >= 0; i--) {
if (Number(num[i]) % 2 === 1) {
return num.slice(0,i+1)
}
}
Boris Chien changed a year agoView mode Like Bookmark
You are given a 0-indexed integer array nums whose length is a power of 2.
Apply the following algorithm on nums:
Let n be the length of nums. If n == 1, end the process. Otherwise, create a new 0-indexed integer array newNums of length n / 2.
For every even index i where 0 <= i < n / 2, assign the value of newNums[i] as min(nums[2 * i], nums[2 * i + 1]).
For every odd index i where 0 <= i < n / 2, assign the value of newNums[i] as max(nums[2 * i], nums[2 * i + 1]).
Replace the array nums with newNums.
Repeat the entire process starting from step 1.
Return the last number that remains in nums after applying the algorithm.
Boris Chien changed a year agoView mode Like Bookmark
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.
Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:
Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.
Return k.
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
題幹給定輸入一個陣列與目標數字,希望能夠回傳不等於目標數字的元素總數,同時要求以In-place處理陣列。
Boris Chien changed a year agoView mode Like Bookmark
You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].
Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:
0 <= j <= nums[i] and
i + j < n
Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].
var jump = function(nums) {
let left=0;
let right=0;
let steps=0;
Boris Chien changed a year agoView mode Like Bookmark
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.
Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.
Return k
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
var removeDuplicates = function(nums) {
let slow=0;
for(let fast=0;fast<nums.length;fast++){
Boris Chien changed a year agoView mode Like Bookmark
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Hashmap
var majorityElement = function(nums) {
let map={};
let maxChar=nums[0];
for(let i=0;i<nums.length;++i){
const char=nums[i];
if(!map[char] || map[char]===0){
Boris Chien changed a year agoView mode Like Bookmark