liuser

7666

liuser
4个月前

liuser
4个月前
/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if (m == n) return head;
ListNode *dummy = new ListNode(0);
ListNode *p = dummy;
for (int i = 0; i < m - 1; i ++ )
p = p->next;

ListNode *a = p, *b = a->next, *c = b->next;
for(int i = m+1; i <= n; i ++)
{
ListNode *d = c->next;
c->next = b;
b = c;
c = d;
}
a->next->next = c;
a->next = b;
return dummy->next;
}
};


liuser
4个月前
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int k = n + m - 1;
int i = m - 1, j = n - 1;

while (i >= 0 && j >= 0) {
if (nums1[i] >= nums2[j]) nums1[k--] = nums1[i--];
else nums1[k--] = nums2[j--];
}

while(j >= 0) nums1[k--] = nums2[j--];
}
};


liuser
4个月前
class Solution {
public:

vector<vector<int>> res;
vector<int> path;

vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(), nums.end());
dfs(nums, 0);
return res;
}

void dfs(vector<int> & nums, int u) {
if (u == nums.size()) {
res.push_back(path);
return ;
}

int k = u + 1;

while (k < nums.size() && nums[k] == nums[u]) k++;

for (int i = 0; i <= k - u; i ++ ) {
dfs(nums, k);
path.push_back(nums[u]);
}

for (int i = 0; i <= k - u; i ++ )
path.pop_back();
}
};


liuser
4个月前
class Solution {
public:
vector<int> grayCode(int n) {
vector<int>res (1, 0);
while (n--) {
for (int i = res.size() -1; i >= 0; i -- ) {
res[i] *= 2;
res.push_back(res[i] + 1);
}
}
return res;
}
};


liuser
5个月前
class Solution {
public:
int climbStairs(int n) {
int a = 1, b = 1;
while (--n)
{
int c = a + b;
a = b;
b = c;
}
return b;
}
};


liuser
5个月前
class Solution {
public:
double myPow(double x, int n) {
typedef long long ll;
bool is_minus = n < 0;
double res = 1;
for (ll k = abs(ll(n)); k;  k>>=1) {
if (k & 1) res *= x;
x *= x;
}

if (is_minus) res = 1 / res;

return res;
}
};


liuser
5个月前
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> hash;
for (auto &str : strs) {
string nstr = str;
sort(nstr.begin(), nstr.end());
hash[nstr].push_back(str);
}

vector<vector<string>> ans;
for (auto& item : hash) ans.push_back(item.second);

return ans;

}
};


liuser
5个月前
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for (int i = 0; i < n; i ++ )
for (int j = 0; j < i; j ++ )
swap(matrix[i][j], matrix[j][i]);

for (int i = 0; i < n; i ++ )
for (int j = 0, k = n - 1; j < k; j ++ , k --)
swap(matrix[i][k], matrix[i][j]);

}
};


liuser
5个月前
class Solution {
public:
vector<vector<int>> ans;
vector<int> path;
vector<bool> st;
vector<vector<int>> permuteUnique(vector<int>& nums) {

sort(nums.begin(), nums.end());
path = vector<int>(nums.size());
st = vector<bool>(nums.size());
dfs(nums, 0);
return ans;

}

void dfs(vector<int> &nums, int u) {
if (u == nums.size())
{
ans.push_back(path);
return ;
}
for (int i = 0; i < nums.size(); i ++ ) {
if (!st[i]) {
if (i && nums[i] != nums[i-1] && !st[i-1]) continue;
st[i] = true;
path[u] = nums[i];
dfs(nums, u+1);
st[i] = false;
}
}
}
};