func countbit(num int) int { temp := num sum := 0 for temp > 0 { temp = temp >> 1 sum += temp } return num - sum }
func main() { fmt.Println(countbit(11)) }
普通解法
将数据的最后一位求和
总结
原作者给了如下解释。我觉得这个题比较巧,不太通用,意义不是特别大
The interesting thing about this algorithm is that we calculate something that we can’t calculate directly, by calculating everything except for that, and then looking at the difference.
for i := 0; i < len(arr); i++ { temp := arr[i] if temp >= target { continue } another := target - temp
for j := len(arr) - 1; j > i; j-- { if arr[j] == another { return []int{i, j} } } } return []int{} }
func main() { s := []int{2, 7, 11, 15} ans := twoSum(s, 9) fmt.Println(ans) }
别人的思路
构造一个HashMap,通过O(n)的时间复杂度即可找到结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14
func twoSum(arr []int, target int) []int { var table = make(map[int]int) for k, v := range arr { if v >= target { continue } table[v] = k another := target - v if table[another] > 0 { return []int{table[another], k} } } return []int{} }
events { # The effective method, used on Linux 2.6+, optmized to serve many clients with each thread. use epoll; # Determines how many clients will be served by each worker process. worker_connections 4000; # Accept as many connections as possible, after nginx gets notification about a new connection. multi_accept on; }
http { include /etc/nginx/mime.types; default_type application/octet-stream;
# Allow the server to close the connection after a client stops responding. reset_timedout_connection on; client_header_timeout 15; # Send the client a "request timed out" if the body is not loaded by this time. client_body_timeout 10; # If the client stops reading data, free up the stale client connection after this much time. send_timeout 15; # Timeout for keep-alive connections. Server will close connections after this time. keepalive_timeout 30; # Number of requests a client can make over the keep-alive connection. keepalive_requests 30;
# Sendfile copies data between one FD and other from within the kernel. sendfile on; # Don't buffer data-sends (disable Nagle algorithm). tcp_nodelay on; # Causes nginx to attempt to send its HTTP response head in one packet, instead of using partial frames. tcp_nopush on;
# Hide web server information server_tokens off; server_info off; server_tag off;
# redirect server error pages to the static page error_page 404 /404.html; error_page 500 502 503 504 /50x.html;
Country Name (2 letter code) [AU]:US State or Province Name (full name) [Some-State]:New York Locality Name (eg, city) []:New York City Organization Name (eg, company) [Internet Widgits Pty Ltd]:Bouncy Castles, Inc. Organizational Unit Name (eg, section) []:Ministry of Water Slides Common Name (e.g. server FQDN or YOUR name) []:server_IP_address Email Address []:admin@your_domain.com
2.证书Base64编码
The values for all keys in the data field have to be base64-encoded strings. If the conversion to base64 string is not desirable, you can choose to specify the stringData field instead, which accepts arbitrary strings as values.
We can improve this method further. Observe that all primes greater than 3 are of the form 6k ± 1, where k is any integer greater than 0. This is because all integers can be expressed as (6k + i), where i = −1, 0, 1, 2, 3, or 4. Note that 2 divides (6k + 0), (6k + 2), and (6k + 4) and 3 divides (6k + 3). So, a more efficient method is to test whether n is divisible by 2 or 3, then to check through all numbers of the form 6k +-1. This is 3 times faster than testing all numbers up to √n.
判断是iPhone手机,就去掉SDP里面的CVO(Coordination of Video Orientation)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
function iOS() { return [ 'iPhone Simulator', 'iPhone' ].includes(navigator.platform) }
function createdOffer(description) { if(iOS()) { var oldsdp = description["sdp"]; var pattern=/a=extmap:13 urn:3gpp:video-orientation\r\n/gi; var newsdp = oldsdp.replace(pattern,""); description["sdp"]=newsdp; }