Leetcode记录
生活随笔
收集整理的這篇文章主要介紹了
Leetcode记录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
29.兩數相除
用二進制的思想做,中間取絕對值的時候會出現溢出情況;移位的時候會出現溢出;=>這時候用long解決就可。
開始的特判為什么不能用long解決呢?因為測評姬給出的最大整數是1<<31 -1,只能返回設定的INT_MAX.
最后還可以返回余數
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.
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记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cf552 G Minimum Poss
- 下一篇: Ubuntu18.04下安装RRStud