日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Leetcode记录

發布時間:2025/4/16 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leetcode记录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

29.兩數相除

用二進制的思想做,中間取絕對值的時候會出現溢出情況;移位的時候會出現溢出;=>這時候用long解決就可。
開始的特判為什么不能用long解決呢?因為測評姬給出的最大整數是1<<31 -1,只能返回設定的INT_MAX.
最后還可以返回余數

class Solution { public:int divide(int dividend, int divisor) {if(dividend == INT_MIN && divisor ==-1) return INT_MAX;int tag = (dividend > 0) ^ (divisor > 0);long _dividend = abs(dividend);long _divisor = abs(divisor);int cnt = 0;while(_divisor <= _dividend){cnt++;_divisor<<=1;}long ans = 0;while(cnt){--cnt;_divisor>>=1;if(_divisor <= _dividend){ans += (1<<cnt);_dividend -= _divisor;}}ans = tag ? -ans : ans;return ans;} }; // 1 << 31 為 2147483648, -1 << 31 為 -2147483648

LCP 03. 機器人大冒險

class Solution { public:bool robot(string command, vector<vector<int>>& obstacles, int x, int y) {unordered_set<long long >s;int x_cnt = 0,y_cnt = 0;s.insert((long long)x_cnt<<30 | y_cnt);for( auto c: command){if(c == 'R'){++x_cnt;s.insert( (long long) x_cnt<<30 |y_cnt);}else{++y_cnt;s.insert( (long long)x_cnt<<30 | y_cnt);}}int loop = min(x/x_cnt,y/y_cnt);if(s.count( (long long)(x-loop*x_cnt)<<30 | y-loop*y_cnt) == 0)return false;for(auto v: obstacles){if(v[0] > x || v[1]>y) continue;loop = min(v[0]/x_cnt,v[1]/y_cnt);if( s.count( ((long long)(v[0]-loop*x_cnt)<<30) | v[1]-loop*y_cnt) == 1)return false;}return true;} };

1310. 子數組異或查詢
主要考慮 x^y^x? = y.

class Solution { public:vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {int l = arr.size();vector<int> pre(l+1); //測評姬需要指定大小pre[0] = 0;for( int i=0 ; i<l; ++i){pre[i+1] = pre[i] ^ arr[i]; }vector<int>re;for(auto &querie :queries){re.push_back(pre[querie[0]] ^ pre[querie[1]+1]);}return re;} };

137. 只出現一次的數字 II

class Solution { public:int singleNumber(vector<int>& nums) {int counts[32];// = new int(32);memset(counts,0,sizeof(counts));for(auto num: nums){for (int i=0;i<32; ++i){if(( num & (1<<i) ))++counts[i];}}for(int i=0 ; i < 32;++i){counts[i] %= 3;}int re=0;for(int i=0; i<32; ++i){re |= (counts[i]<<i);// if(counts[i])// re += ( 1<<i);}return re;} };

?

總結

以上是生活随笔為你收集整理的Leetcode记录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。