Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb208658

Browse files
authored
Merge branch 'main' into main
2 parents9ef16f5 +30e92c7 commitb208658

File tree

43 files changed

+1000
-29
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1000
-29
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
13+
classSolution {
14+
public:
15+
vector<vector<int>>levelOrder(TreeNode* root) {
16+
queue< TreeNode* > q;
17+
q.push(root);
18+
vector<vector<int>> v;
19+
vector<int> k;
20+
TreeNode* c;
21+
int j;
22+
while(!q.empty() )
23+
{
24+
k.clear();
25+
j=q.size();
26+
for(int i=0;i<j;i++)
27+
{
28+
c=q.front();
29+
q.pop();
30+
if(c)
31+
{
32+
k.push_back(c->val);
33+
q.push(c->left);
34+
q.push(c->right);
35+
}
36+
}
37+
if(!k.empty())
38+
v.push_back(k);
39+
40+
}
41+
return v;
42+
43+
}
44+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
classSolution {
2+
public:
3+
intmaxArea(vector<int>& height) {
4+
int i=0,j=height.size()-1;
5+
int maxA=0,minh;
6+
while(i<j)
7+
{
8+
minh=min(height[i],height[j]);
9+
if(minh*(j-i)>maxA)
10+
maxA=minh*(j-i);
11+
if(minh==height[i])
12+
i++;
13+
else
14+
j--;
15+
}
16+
return maxA;
17+
}
18+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
classSolution {
2+
public:
3+
intlongestConsecutive(vector<int>& nums) {
4+
unordered_set<int> hs;
5+
for (int n : nums) {
6+
hs.insert(n);
7+
}
8+
9+
int longest =0;
10+
for (int n : nums) {
11+
// if n-1 is not in the set, the element can be the start of a sequence
12+
if (hs.find(n -1) == hs.end()) {
13+
int length =1;
14+
while (hs.find(n + length) != hs.end())
15+
++length;
16+
longest =max(longest, length);
17+
}
18+
}
19+
return longest;
20+
}
21+
};

‎cpp/136-Single-Number.cpp‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
// Time complexity : O(n)
2+
// Space complexity : O(1)
3+
14
classSolution {
25
public:
3-
intswap(int& a,int& b) {
4-
a = a^b;
5-
b = a^a;
6+
intsingleNumber(vector<int>& nums) {
7+
int res =0;
8+
for(int num : nums){
9+
res^=num;
10+
}
11+
return res;
612
}
7-
};
13+
};

‎cpp/141-Linked-List-Cycle.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
classSolution {
10+
public:
11+
boolhasCycle(ListNode *head) {
12+
if(!head)return0;
13+
unordered_set<ListNode*> s;
14+
s.insert(head);
15+
ListNode * ptr=head->next;
16+
while(ptr)
17+
{
18+
if(s.find(ptr)==s.end())
19+
{
20+
s.insert(ptr);
21+
ptr=ptr->next;
22+
}
23+
else
24+
return1;
25+
}
26+
return0;
27+
28+
}
29+
};

‎cpp/202-Happy-Number.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Time Complexity: O(log n)
3+
Space Complexity: O(log n)
4+
*/
5+
6+
classSolution {
7+
public:
8+
boolisHappy(int n) {
9+
unordered_set<int> us;
10+
while (us.find(n) == us.end()) {
11+
us.insert(n);
12+
int temp =0;
13+
while (n >0) {
14+
int digit = n %10;
15+
digit = digit * digit;
16+
temp += digit;
17+
n = n /10;
18+
}
19+
n = temp;
20+
if (n ==1) {
21+
returntrue;
22+
}
23+
}
24+
returnfalse;
25+
}
26+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
classSolution {
2+
public:
3+
vector<int>productExceptSelf(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<int>res(n);
6+
7+
// compute with prefix
8+
int prefix =1;
9+
for (int i =0; i < n; ++i) {
10+
res[i] = prefix;
11+
prefix *= nums[i];
12+
}
13+
// compute with posfix
14+
int postfix =1;
15+
for (int i = n-1; i >=0; --i) {
16+
res[i] *= postfix;
17+
postfix *= nums[i];
18+
}
19+
return res;
20+
}
21+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
classSolution {
2+
public:
3+
intlengthOfLongestSubstring(string s) {
4+
if(s.length()==1)
5+
return1;
6+
7+
int j,max=0,count=0,found;
8+
string ss="";
9+
for(j=0;j<s.length();j++){
10+
found=ss.find(s[j]);
11+
if(found<0){
12+
count++;
13+
ss+=s[j];
14+
}else{
15+
ss.erase(0,found+1);
16+
ss+=s[j];
17+
count=ss.length();
18+
}
19+
max=std::max(max,count);
20+
}
21+
return max;
22+
}
23+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Time complexity: O(log(N))
2+
// Space complexity: O(1)
3+
4+
classSolution {
5+
public:
6+
intsearch(vector<int>& nums,int target) {
7+
int start =0, end = nums.size() -1;
8+
while(start <= end){
9+
int mid = start + (end - start) /2;
10+
if(nums[mid] == target)
11+
return mid;
12+
elseif(nums[mid] >= nums[start]){
13+
if(target >= nums[start] && target < nums[mid])
14+
end = mid -1;
15+
else
16+
start = mid +1;
17+
}else{
18+
if(target > nums[mid] && target <= nums[end])
19+
start = mid +1;
20+
else
21+
end = mid -1;
22+
}
23+
}
24+
return -1;
25+
}
26+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
classSolution {
2+
public:
3+
vector<int>topKFrequent(vector<int>& nums,int k) {
4+
unordered_map<int,int> count;
5+
for (int n : nums) {
6+
++count[n];
7+
}
8+
9+
vector<vector<int>>freq(nums.size() +1, vector<int>());
10+
for (auto e : count) {
11+
freq[e.second].push_back(e.first);
12+
}
13+
14+
vector<int> result;
15+
for (int i = freq.size()-1; i >=0; --i) {
16+
for (int n : freq[i]) {
17+
result.push_back(n);
18+
if (result.size() == k)
19+
return result;
20+
}
21+
}
22+
return result;
23+
}
24+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp