Skip to content

Commit de143a3

Browse files
Mehul ShreyMehul Shrey
Mehul Shrey
authored and
Mehul Shrey
committed
5/2/22 solutions
1 parent e79a101 commit de143a3

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

125.IsPalindrome.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// 125. Valid Palindrome
2+
// A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
3+
// Given a string s, return true if it is a palindrome, or false otherwise.
4+
public class Solution
5+
{
6+
public bool IsPalindrome(string s)
7+
{
8+
Regex rgx = new Regex("[^a-zA-Z]");
9+
s = rgx.Replace(s, "").ToLower();
10+
char[] charArray = s.ToCharArray();
11+
Array.Reverse(charArray);
12+
return s.Equals(new string(charArray));
13+
}
14+
}
15+
16+
public class Solution
17+
{
18+
public bool IsPalindrome(string s)
19+
{
20+
List<char> strArr = new List<char>();
21+
for (int i = 0; i < s.Length; i++)
22+
{
23+
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') ||
24+
(s[i] >= '0' && s[i] <= '9'))
25+
strArr.Add(Char.ToLower(s[i]));
26+
}
27+
List<char> revStrArr = Enumerable.Reverse(strArr).ToList();
28+
return (new string(strArr.ToArray())).Equals(new string(revStrArr.ToArray()));
29+
}
30+
}

136.SingleNumber.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// 136. Single Number
2+
// Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
3+
// You must implement a solution with a linear runtime complexity and use only constant extra space.
4+
5+
public class Solution {
6+
public int SingleNumber(int[] nums) {
7+
8+
int result = nums[0];
9+
for(int i = 1; i< nums.Length; i++)
10+
result ^= nums[i];
11+
return result;
12+
}
13+
}

141.HasCycle.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// 141. Linked List Cycle
2+
// Given head, the head of a linked list, determine if the linked list has a cycle in it.
3+
// There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.
4+
// Return true if there is a cycle in the linked list. Otherwise, return false.
5+
6+
/**
7+
* Definition for singly-linked list.
8+
* public class ListNode {
9+
* public int val;
10+
* public ListNode next;
11+
* public ListNode(int x) {
12+
* val = x;
13+
* next = null;
14+
* }
15+
* }
16+
*/
17+
public class Solution
18+
{
19+
public bool HasCycle(ListNode head) {
20+
if(head == null || head.next == null)
21+
return false;
22+
ListNode slow, fast;
23+
slow = head;
24+
fast =head.next;
25+
while(slow!=null && slow.next!=null
26+
&& fast.next != null && fast.next.next!= null){
27+
if(slow == fast)
28+
return true;
29+
slow=slow.next;
30+
fast=fast.next.next;
31+
}
32+
return false;
33+
}
34+
}

88.Merge.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// 88. Merge Sorted Array
2+
// You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.
3+
// Merge nums1 and nums2 into a single array sorted in non-decreasing order.
4+
// The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.
5+
6+
public class Solution
7+
{
8+
public void Merge(int[] nums1, int m, int[] nums2, int n)
9+
{
10+
for (int i = 0; i < n; i++)
11+
{
12+
nums1[m + i] = nums2[i];
13+
}
14+
Array.Sort(nums1);
15+
}
16+
}

0 commit comments

Comments
 (0)