Content-Length: 570698 | pFad | http://github.com/AlgoStudyGroup/Leetcode/commit/e9d1b36d311db88291042731c8b611f2612547a5

C7 Merge pull request #285 from hellomrsun/master · AlgoStudyGroup/Leetcode@e9d1b36 · GitHub
Skip to content

Commit e9d1b36

Browse files
authored
Merge pull request #285 from hellomrsun/master
Add C# solutions
2 parents 1a40ddb + 77d7b5a commit e9d1b36

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Solutions in various programming languages are provided. Enjoy it.
2020
11. [Maximum Product Subarray](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/11-Maximum-Product-Subarray)
2121
12. [Combination Sum III](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/12-Combination-Sum-III)
2222
13. [Insert Interval](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/13-Insert-Interval)
23+
14. [House Robber](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/14-House-Robber)
24+
15. [Length of Last Word](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/15-Length-of-Last-Word)
25+
16. [Maximum XOR of Two Numbers in an Array](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/16-Maximum-XOR-of-Two-Numbers-in-an-Array)
26+
17. [Robot Bounded In Circle](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/17-Robot-Bounded-In-Circle)
2327

2428
## August LeetCoding Challenge
2529
Click [here](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/) for problem descriptions.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Solution {
2+
public int Rob(int[] nums) {
3+
if(nums.Length == 0) return 0;
4+
5+
var dp = new int[nums.Length+1];
6+
dp[1] = nums[0];
7+
for(int i=1; i<nums.Length; i++){
8+
dp[i+1] = Math.Max(dp[i], dp[i-1] + nums[i]);
9+
}
10+
return dp[nums.Length];
11+
}
12+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
public class Solution {
2+
3+
//Solution 1
4+
public class Solution {
5+
public int LengthOfLastWord(string s) {
6+
if(s.Length == 0) return 0;
7+
8+
var l = s.Split(' ').ToList();
9+
var i = l.Count - 1;
10+
while(i >= 0){
11+
if(l[i] == ""){
12+
i--;
13+
}
14+
else{
15+
return l[i].Length;
16+
}
17+
}
18+
19+
return 0;
20+
}
21+
}
22+
23+
24+
//Solution 2
25+
public int LengthOfLastWord(string s)
26+
{
27+
if (s.Length == 0) return 0;
28+
29+
int beginPosition = 0;
30+
int endPosition = 0;
31+
bool hasWord = false;
32+
bool hasSpaceBeforeLastWord = false;
33+
34+
//Start by the end of string
35+
for (int i = s.Length - 1; i >= 0; i--)
36+
{
37+
if (s[i] == ' ' && !hasWord)
38+
{
39+
//This condition improves performance
40+
continue;
41+
}
42+
else if (s[i] != ' ' && !hasWord)
43+
{
44+
hasWord = true;
45+
endPosition = i;
46+
continue;
47+
}
48+
else if (s[i] == ' ' && hasWord)
49+
{
50+
beginPosition = i;
51+
hasSpaceBeforeLastWord = true;
52+
break;
53+
}
54+
}
55+
56+
//One word case
57+
if (endPosition - beginPosition == 0 && hasWord)
58+
return 1;
59+
60+
//No words case
61+
if (endPosition - beginPosition == 0 && !hasWord)
62+
return 0;
63+
64+
//Has word and has spaces before last words
65+
if (endPosition - beginPosition != 0 && hasWord && hasSpaceBeforeLastWord)
66+
return endPosition - beginPosition;
67+
68+
//Has word and has no space before last words
69+
if (endPosition - beginPosition != 0 && hasWord && !hasSpaceBeforeLastWord)
70+
return endPosition - beginPosition + 1;
71+
72+
return 0;
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution {
2+
public int FindMaximumXOR(int[] nums) {
3+
int max = 0, mask=0;
4+
for(int i=31; i>=0; i--){
5+
mask |= (1 << i);
6+
7+
var hs = new HashSet<int>();
8+
foreach(int num in nums){
9+
hs.Add(num & mask);
10+
}
11+
12+
int tmp = max | (1 << i);
13+
foreach(int prefix in hs){
14+
if(hs.Contains(tmp ^ prefix)){
15+
max = tmp;
16+
break;
17+
}
18+
}
19+
}
20+
return max;
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution {
2+
public bool IsRobotBounded(string instructions) {
3+
int x = 0;
4+
int y = 0;
5+
var dirs = new int[4][]{new int[]{0,1}, new int[]{1, 0}, new int[]{0,-1}, new int[]{-1,0}};
6+
int d = 0;
7+
8+
foreach(char c in instructions){
9+
if(c == 'L'){
10+
d = (d+3)%4;
11+
}else if(c == 'R'){
12+
d = (d+1)%4;
13+
}else{
14+
x += dirs[d][0];
15+
y += dirs[d][1];
16+
}
17+
}
18+
19+
return x == 0 && y == 0 || d > 0;
20+
}
21+
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/AlgoStudyGroup/Leetcode/commit/e9d1b36d311db88291042731c8b611f2612547a5

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy