Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace Leetcode
- {
- class Program
- {
- static void Main(string[] argsConsole)
- {
- NextGreaterElement(new int[3] { 4, 1, 2 }, new int[4] { 1, 3, 4, 2 }).ToList().ForEach(Console.WriteLine);
- Console.WriteLine();
- NextGreaterElement(new int[3] { 4, 1, 2 }, new int[4] { 1, 2, 3, 4 }).ToList().ForEach(Console.WriteLine);
- Console.ReadKey();
- }
- //461. Hamming Distance ~ Easy = Accepted 149/149
- public static int HammingDistance(int x, int y)
- {
- string xBinary = Convert.ToString(x, 2);
- string yBinary = Convert.ToString(y, 2);
- int differences = 0;
- int repeat = Math.Max(xBinary.Length, yBinary.Length);
- for (int i = 0; i < repeat; i++)
- {
- if (xBinary.Length < repeat)
- {
- xBinary = "0" + xBinary;
- }
- if (yBinary.Length < repeat)
- {
- yBinary = "0" + yBinary;
- }
- }
- for (int i = 0; i < repeat; i++)
- {
- if (xBinary[i] != yBinary[i])
- {
- differences++;
- }
- }
- return differences;
- }
- //561. Array Partition I ~ Easy = Accepted 81/81
- public static int ArrayPairSum(int[] nums)
- {
- if (nums.Count() == 2)
- {
- return nums[0];
- }
- List<int> numList = nums.ToList();
- numList.Sort();
- int sum = 0;
- for (int i = 0; i < numList.Count; i += 2)
- {
- sum += numList[i];
- }
- return sum;
- }
- //476. Number Complement ~ Easy = Accepted 149/149
- public static int FindComplement(int num)
- {
- string numBinary = Convert.ToString(num, 2);
- string compBinary = "";
- for (int i = 0; i < numBinary.Length; i++)
- {
- compBinary += numBinary[i] == '0' ? 1 : 0;
- }
- return Convert.ToInt32(compBinary, 2);
- }
- //566. Reshape the Matrix ~ Easy = Accepted 56/56
- public static int[,] MatrixReshape(int[,] nums, int r, int c)
- {
- if (r * c > nums.GetLength(0) * nums.GetLength(1))
- {
- return nums;
- }
- Queue<int> numQueue = new Queue<int>();
- int[,] newMatrix = new int[r, c];
- for (int i = 0; i < nums.GetLength(0); i++)
- {
- for (int j = 0; j < nums.GetLength(1); j++)
- {
- numQueue.Enqueue(nums[i, j]);
- }
- }
- for (int i = 0; i < newMatrix.GetLength(0); i++)
- {
- for (int j = 0; j < newMatrix.GetLength(1); j++)
- {
- newMatrix[i, j] = numQueue.Dequeue();
- }
- }
- return newMatrix;
- }
- //557. Reverse Words in a String III ~ Easy = Accepted ~ 30/30
- public static string ReverseWords(string s)
- {
- string[] splitString = s.Split(null);
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < splitString.Count(); i++)
- {
- char[] currentWord = splitString[i].ToCharArray();
- sb.Append(string.Join("", currentWord.Reverse()));
- sb.Append(" ");
- }
- return sb.ToString().Trim();
- }
- //500. Keyboard Row ~ Easy = Accepted ~ 22/22
- public static string[] FindWords(string[] words)
- {
- List<string> oneLineWords = new List<string>();
- for (int i = 0; i < words.Length; i++)
- {
- if (Regex.Matches(words[i].ToLower(), "[qwertyuiop]").Count == words[i].Length ||
- Regex.Matches(words[i].ToLower(), "[asdfghjkl]").Count == words[i].Length ||
- Regex.Matches(words[i].ToLower(), "[zxcvbnm]").Count == words[i].Length)
- {
- oneLineWords.Add(words[i]);
- }
- }
- return oneLineWords.ToArray();
- }
- //575. Distribute Candies ~ Easy = Accepted ~ 207/207
- public static int DistributeCandies(int[] candies)
- {
- if (candies.Distinct().Count() > candies.Count() / 2)
- {
- return candies.Count() / 2;
- }
- return candies.Distinct().Count();
- }
- //412. Fizz Buzz ~ Easy = Accepted ~ 8/8
- public static List<string> FizzBuzz(int n)
- {
- List<string> fizzBuzz = new List<string>();
- for (int i = 1; i <= n; i++)
- {
- string current = "";
- if (i % 3 == 0)
- {
- current += "Fizz";
- }
- if (i % 5 == 0)
- {
- current += "Buzz";
- }
- if (i % 3 != 0 && i % 5 != 0)
- {
- current += i.ToString();
- }
- fizzBuzz.Add(current);
- }
- return fizzBuzz;
- }
- //344. Reverse String ~ Easy = Accepted ~ 476/476
- public static string ReverseString(string s)
- {
- return string.Join("", s.ToCharArray().Reverse());
- }
- //496. Next Greater Element I ~ Easy = Accepted ~ 17/17
- public static int[] NextGreaterElement(int[] findNums, int[] nums)
- {
- int[] greaterInts = new int[findNums.Length];
- int j = 0, flag = 0;
- for (int i = 0; i < findNums.Length; i++)
- {
- j = Array.IndexOf(nums, findNums[i]) + 1;
- while (j < nums.Length)
- {
- if (findNums[i] < nums[j])
- {
- greaterInts[i] = nums[j];
- flag = 1;
- j = nums.Length;
- }
- j++;
- }
- if (flag == 0)
- {
- greaterInts[i] = -1;
- }
- flag = 0;
- }
- return greaterInts;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement