class Solution {
public:
bool isUgly(int num){int d[]={2,3,5};for(auto prime : d){while(num >0&& num % prime ==0){
num /= prime;}}return num ==1;}};
LeetCode 67. Add Binary
class Solution {
public:
string addBinary(string a, string b){reverse(a.begin(), a.end());reverse(b.begin(), b.end());int t =0;
string res;for(int i =0; i < a.size()|| i < b.size(); i++){int va = i >= a.size()?0: a[i]-'0';int vb = i >= b.size()?0: b[i]-'0';int s = va + vb + t;
t = s/2, s %=2;
res +=to_string(s);}if(t)res +='1';returnstring(res.rbegin(), res.rend());}};
LeetCode 504. Base 7
class Solution {
public:
string convertToBase7(int num){
string res;
bool is_neg = false;if(num <0)num *=-1, is_neg = true;elseif(!num)res ='0';while(num){
res +=to_string(num%7);
num /=7;}if(is_neg)res +='-';returnstring(res.rbegin(), res.rend());}};
LeetCode 54. Spiral Matrix
class Solution {
public:
vector<int>spiralOrder(vector<vector<int>>& matrix){
vector<int> res;if(matrix.empty())return res;int n = matrix.size(), m = matrix[0].size();
vector<vector<bool>>st(n, vector<bool>(m,false));int dx[4]={-1,0,1,0};int dy[4]={0,1,0,-1};int x =0, y =0, d =1;for(int i =0; i < n*m; i++){
res.push_back(matrix[x][y]);
st[x][y]= true;int a = x + dx[d], b = y + dy[d];if(a <0|| a >= n || b <0|| b >= m || st[a][b]){
d =(d +1)%4;
a = x + dx[d], b = y + dy[d];}
x = a, y = b;}return res;}};
LeetCode 24. Swap Nodes in Pairs
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode*swapPairs(ListNode* head){auto dummy = new ListNode(0);
dummy->next = head;auto p = dummy;while(p && p->next && p->next->next){auto a = p->next, b = a->next;
p->next = b;
a->next = b->next;
b->next = a;
p = a;}return dummy->next;}};//画个图,可以很清楚的表示清楚
LeetCode 299. Bulls and Cows
class Solution {
public:
string getHint(string secret, string guess){int a =0, b =0, n = guess.size();int ds[10]={0}, dg[10]={0};for(int i =0; i < n; i++){int x = secret[i]-'0';int y = guess[i]-'0';if(x == y)a++;
ds[x]++, dg[y]++;}//统计频率for(int i =0; i <10; i++){
b +=min(ds[i], dg[i]);//cout << i << " " << b << endl;}
b -= a;returnto_string(a)+'A'+to_string(b)+'B';}};
LeetCode 481. Magical String
class Solution {
public:intmagicalString(int n){
string s ="122";for(int i =2, k =1; i < n; i++, k =3- k){for(int j =0; j < s[i]-'0'; j++)s +=to_string(k);}int res =0;for(int i =0; i < n; i++)res += s[i]=='1';return res;}};
LeetCode 71. Simplify Path
class Solution {
public:
string simplifyPath(string path){
path +='/';
string res, s;for(auto c : path){if(res.empty())res += c;elseif(c !='/')s += c;else{if(s ==".."){if(res.size()>1){
res.pop_back();while(res.back()!='/')res.pop_back();}}elseif(s !=""&& s !=".")res += s +'/';
s ="";}}if(res.size()>1)res.pop_back();return res;}};
LeetCode 12. Integer to Roman
class Solution {
public:
string intToRoman(int num){int values[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
string reps[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
string res;for(int i =0; i <13; i++){while(num >= values[i]){
num -= values[i];
res += reps[i];}}return res;}};
LeetCode 68. Text Justification
class Solution {
public:
string space(int x){
string res;while(x--)res +=' ';return res;}
vector<string>fullJustify(vector<string>& words,int maxWidth){
vector<string> res;for(int i =0; i < words.size();){int j = i +1, s = words[i].size(), rs = words[i].size();while(j < words.size()&& s +1+ words[j].size()<= maxWidth){
s += words[j].size()+1;
rs += words[j].size();
j++;}
rs = maxWidth - rs;
string line = words[i];if(j == words.size()){for(int k = i +1; k < j; k++)line +=' '+ words[k];
line +=space(maxWidth - line.size());}elseif(j - i ==1){
line +=space(maxWidth - line.size());}else{int base = rs /(j - i -1);int rem = rs %(j - i -1);
i ++;for(int k =0; i < j; i++, k++)
line +=space(base +(k < rem))+words[i];}
i = j;
res.push_back(line);}return res;}};