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

Commitcc23247

Browse files
authored
Merge pull request#291 from hellomrsun/master
Add C# solutions
2 parents8034524 +fbdd550 commitcc23247

File tree

34 files changed

+718
-0
lines changed

34 files changed

+718
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace_1290_Convert_Binary_Number_in_a_Linked_List_to_Integer
2+
{
3+
publicclassSolution
4+
{
5+
publicintGetDecimalValue(ListNodehead)
6+
{
7+
//Solution 1
8+
varres=0;
9+
while(head!=null)
10+
{
11+
res=res*2+head.val;
12+
head=head.next;
13+
}
14+
returnres;
15+
16+
//Solution 2: Stack, from end to beginning
17+
/*
18+
var stack = new Stack<int>();
19+
while(head != null){
20+
stack.Push(head.val);
21+
head = head.next;
22+
}
23+
24+
var res = 0;
25+
var lvl = 1;
26+
res += stack.Pop();
27+
28+
while(stack.Count > 0){
29+
lvl *= 2;
30+
res += lvl * stack.Pop();
31+
}
32+
33+
return res;
34+
*/
35+
}
36+
}
37+
38+
publicclassListNode
39+
{
40+
publicintval;
41+
publicListNodenext;
42+
publicListNode(intval=0,ListNodenext=null)
43+
{
44+
this.val=val;
45+
this.next=next;
46+
}
47+
}
48+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace_147_Insertion_Sort_List
2+
{
3+
publicclassSolution
4+
{
5+
publicListNodeInsertionSortList(ListNodehead)
6+
{
7+
ListNodedummy=newListNode(0);
8+
ListNodeprev=dummy;
9+
while(head!=null)
10+
{
11+
ListNodetemp=head.next;
12+
13+
/* Before insert, the prev is at the last node of the sorted list.
14+
Only the last node's value is larger than the current inserting node
15+
should we move the temp back to the head*/
16+
if(prev.val>=head.val)prev=dummy;
17+
18+
while(prev.next!=null&&prev.next.val<head.val)
19+
{
20+
prev=prev.next;
21+
}
22+
23+
head.next=prev.next;
24+
prev.next=head;
25+
// prev = dummy; // Don't set prev to the head of the list after insert
26+
head=temp;
27+
}
28+
returndummy.next;
29+
}
30+
}
31+
publicclassListNode
32+
{
33+
publicintval;
34+
publicListNodenext;
35+
publicListNode(intval=0,ListNodenext=null)
36+
{
37+
this.val=val;
38+
this.next=next;
39+
}
40+
}
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
usingSystem;
2+
3+
namespace_1446_Consecutive_Characters
4+
{
5+
publicclassSolution
6+
{
7+
publicintMaxPower(strings)
8+
{
9+
intres=1,cnt=1;
10+
for(inti=1;i<s.Length;i++)
11+
{
12+
if(s[i]==s[i-1])
13+
{
14+
cnt++;
15+
res=Math.Max(res,cnt);
16+
}
17+
else
18+
{
19+
cnt=1;
20+
}
21+
}
22+
returnres;
23+
}
24+
}
25+
}

‎November-LeetCoding-Challenge/04-Minimum-Height-Trees/Minimum-Height-Trees.cs‎

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
publicclassSolution{
2+
publicintMinCostToMoveChips(int[]position){
3+
inteven=0,odd=0;
4+
for(inti=0;i<position.Length;i++){
5+
if(position[i]%2==0){
6+
even++;
7+
}else{
8+
odd++;
9+
}
10+
}
11+
12+
returnMath.Min(even,odd);
13+
}
14+
}

‎November-LeetCoding-Challenge/06-Find-the-Smallest-Divisor-Given-a-Threshold/Find-the-Smallest-Divisor-Given-a-Threshold.cs‎

Whitespace-only changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
usingSystem.Collections.Generic;
2+
3+
namespace_445_Add_Two_Numbers_II
4+
{
5+
publicclassSolution
6+
{
7+
publicListNodeAddTwoNumbers(ListNodel1,ListNodel2)
8+
{
9+
Stack<int>s1=newStack<int>(),s2=newStack<int>();
10+
while(l1!=null)
11+
{
12+
s1.Push(l1.val);
13+
l1=l1.next;
14+
}
15+
while(l2!=null)
16+
{
17+
s2.Push(l2.val);
18+
l2=l2.next;
19+
}
20+
21+
varsum=0;
22+
varnode=newListNode(0);
23+
while(s1.Count>0||s2.Count>0)
24+
{
25+
if(s1.Count>0)sum+=s1.Pop();
26+
if(s2.Count>0)sum+=s2.Pop();
27+
node.val=sum%10;
28+
29+
varhead=newListNode(sum/10);
30+
head.next=node;
31+
node=head;
32+
33+
sum/=10;
34+
}
35+
36+
returnnode.val==0?node.next:node;
37+
}
38+
}
39+
40+
publicclassListNode
41+
{
42+
publicintval;
43+
publicListNodenext;
44+
publicListNode(intval=0,ListNodenext=null)
45+
{
46+
this.val=val;
47+
this.next=next;
48+
}
49+
}
50+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
usingSystem;
2+
3+
namespace_563_Binary_Tree_Tilt
4+
{
5+
publicclassSolution
6+
{
7+
privateintdiff=0;
8+
9+
publicintFindTilt(TreeNoderoot){
10+
PostOrder(root);
11+
returndiff;
12+
}
13+
14+
privateintPostOrder(TreeNodenode){
15+
if(node==null)return0;
16+
17+
varl=PostOrder(node.left);
18+
varr=PostOrder(node.right);
19+
20+
diff+=Math.Abs(l-r);
21+
22+
returnl+r+node.val;
23+
}
24+
}
25+
26+
publicclassTreeNode{
27+
publicintval;
28+
publicTreeNodeleft;
29+
publicTreeNoderight;
30+
publicTreeNode(intval=0,TreeNodeleft=null,TreeNoderight=null){
31+
this.val=val;
32+
this.left=left;
33+
this.right=right;
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
usingSystem;
2+
3+
namespace_1026_Maximum_Difference_Between_Node_and_Ancestor
4+
{
5+
publicclassSolution
6+
{
7+
publicintMaxAncestorDiff(TreeNoderoot)
8+
{
9+
returnDfs(root,root.val,root.val);
10+
}
11+
12+
publicintDfs(TreeNodenode,intmin,intmax)
13+
{
14+
if(node==null)returnmax-min;
15+
16+
min=Math.Min(min,node.val);
17+
max=Math.Max(max,node.val);
18+
19+
returnMath.Max(Dfs(node.left,min,max),Dfs(node.right,min,max));
20+
}
21+
}
22+
23+
publicclassTreeNode
24+
{
25+
publicintval;
26+
publicTreeNodeleft;
27+
publicTreeNoderight;
28+
publicTreeNode(intval=0,TreeNodeleft=null,TreeNoderight=null)
29+
{
30+
this.val=val;
31+
this.left=left;
32+
this.right=right;
33+
}
34+
}
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace_832_Flipping_an_Image
2+
{
3+
publicclassSolution
4+
{
5+
publicint[][]FlipAndInvertImage(int[][]A){
6+
if(A==null||A.Length==0)returnnewint[0][];
7+
varlevelLength=A[0].Length-1;
8+
intb=0;
9+
inte=levelLength;
10+
for(inti=0;i<=A.Length-1;i++){
11+
b=0;
12+
e=levelLength;
13+
while(b<e){
14+
inttemp=A[i][b];
15+
A[i][b]=A[i][e];
16+
A[i][e]=temp;
17+
b++;
18+
e--;
19+
}
20+
for(intj=0;j<=levelLength;j++){
21+
A[i][j]=1-A[i][j];
22+
}
23+
}
24+
returnA;
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp