Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Leetcode
  9. {
  10. class Program
  11. {
  12. static void Main(string[] argsConsole)
  13. {
  14. NextGreaterElement(new int[3] { 4, 1, 2 }, new int[4] { 1, 3, 4, 2 }).ToList().ForEach(Console.WriteLine);
  15. Console.WriteLine();
  16. NextGreaterElement(new int[3] { 4, 1, 2 }, new int[4] { 1, 2, 3, 4 }).ToList().ForEach(Console.WriteLine);
  17. Console.ReadKey();
  18. }
  19.  
  20. //461. Hamming Distance ~ Easy = Accepted 149/149
  21. public static int HammingDistance(int x, int y)
  22. {
  23. string xBinary = Convert.ToString(x, 2);
  24. string yBinary = Convert.ToString(y, 2);
  25. int differences = 0;
  26. int repeat = Math.Max(xBinary.Length, yBinary.Length);
  27. for (int i = 0; i < repeat; i++)
  28. {
  29. if (xBinary.Length < repeat)
  30. {
  31. xBinary = "0" + xBinary;
  32. }
  33.  
  34. if (yBinary.Length < repeat)
  35. {
  36. yBinary = "0" + yBinary;
  37. }
  38. }
  39.  
  40. for (int i = 0; i < repeat; i++)
  41. {
  42. if (xBinary[i] != yBinary[i])
  43. {
  44. differences++;
  45. }
  46. }
  47. return differences;
  48. }
  49.  
  50. //561. Array Partition I ~ Easy = Accepted 81/81
  51. public static int ArrayPairSum(int[] nums)
  52. {
  53. if (nums.Count() == 2)
  54. {
  55. return nums[0];
  56. }
  57.  
  58. List<int> numList = nums.ToList();
  59. numList.Sort();
  60.  
  61. int sum = 0;
  62. for (int i = 0; i < numList.Count; i += 2)
  63. {
  64. sum += numList[i];
  65. }
  66. return sum;
  67. }
  68.  
  69. //476. Number Complement ~ Easy = Accepted 149/149
  70. public static int FindComplement(int num)
  71. {
  72. string numBinary = Convert.ToString(num, 2);
  73. string compBinary = "";
  74.  
  75. for (int i = 0; i < numBinary.Length; i++)
  76. {
  77. compBinary += numBinary[i] == '0' ? 1 : 0;
  78. }
  79. return Convert.ToInt32(compBinary, 2);
  80. }
  81.  
  82. //566. Reshape the Matrix ~ Easy = Accepted 56/56
  83. public static int[,] MatrixReshape(int[,] nums, int r, int c)
  84. {
  85. if (r * c > nums.GetLength(0) * nums.GetLength(1))
  86. {
  87. return nums;
  88. }
  89.  
  90. Queue<int> numQueue = new Queue<int>();
  91. int[,] newMatrix = new int[r, c];
  92.  
  93. for (int i = 0; i < nums.GetLength(0); i++)
  94. {
  95. for (int j = 0; j < nums.GetLength(1); j++)
  96. {
  97. numQueue.Enqueue(nums[i, j]);
  98. }
  99. }
  100.  
  101. for (int i = 0; i < newMatrix.GetLength(0); i++)
  102. {
  103. for (int j = 0; j < newMatrix.GetLength(1); j++)
  104. {
  105. newMatrix[i, j] = numQueue.Dequeue();
  106. }
  107. }
  108.  
  109. return newMatrix;
  110. }
  111.  
  112. //557. Reverse Words in a String III ~ Easy = Accepted ~ 30/30
  113. public static string ReverseWords(string s)
  114. {
  115. string[] splitString = s.Split(null);
  116. StringBuilder sb = new StringBuilder();
  117. for (int i = 0; i < splitString.Count(); i++)
  118. {
  119. char[] currentWord = splitString[i].ToCharArray();
  120. sb.Append(string.Join("", currentWord.Reverse()));
  121. sb.Append(" ");
  122. }
  123. return sb.ToString().Trim();
  124. }
  125.  
  126. //500. Keyboard Row ~ Easy = Accepted ~ 22/22
  127. public static string[] FindWords(string[] words)
  128. {
  129. List<string> oneLineWords = new List<string>();
  130. for (int i = 0; i < words.Length; i++)
  131. {
  132. if (Regex.Matches(words[i].ToLower(), "[qwertyuiop]").Count == words[i].Length ||
  133. Regex.Matches(words[i].ToLower(), "[asdfghjkl]").Count == words[i].Length ||
  134. Regex.Matches(words[i].ToLower(), "[zxcvbnm]").Count == words[i].Length)
  135. {
  136. oneLineWords.Add(words[i]);
  137. }
  138. }
  139. return oneLineWords.ToArray();
  140. }
  141.  
  142. //575. Distribute Candies ~ Easy = Accepted ~ 207/207
  143. public static int DistributeCandies(int[] candies)
  144. {
  145. if (candies.Distinct().Count() > candies.Count() / 2)
  146. {
  147. return candies.Count() / 2;
  148. }
  149. return candies.Distinct().Count();
  150. }
  151.  
  152. //412. Fizz Buzz ~ Easy = Accepted ~ 8/8
  153. public static List<string> FizzBuzz(int n)
  154. {
  155. List<string> fizzBuzz = new List<string>();
  156.  
  157. for (int i = 1; i <= n; i++)
  158. {
  159. string current = "";
  160. if (i % 3 == 0)
  161. {
  162. current += "Fizz";
  163. }
  164. if (i % 5 == 0)
  165. {
  166. current += "Buzz";
  167. }
  168. if (i % 3 != 0 && i % 5 != 0)
  169. {
  170. current += i.ToString();
  171. }
  172.  
  173. fizzBuzz.Add(current);
  174. }
  175.  
  176. return fizzBuzz;
  177. }
  178.  
  179. //344. Reverse String ~ Easy = Accepted ~ 476/476
  180. public static string ReverseString(string s)
  181. {
  182. return string.Join("", s.ToCharArray().Reverse());
  183. }
  184.  
  185. //496. Next Greater Element I ~ Easy = Accepted ~ 17/17
  186. public static int[] NextGreaterElement(int[] findNums, int[] nums)
  187. {
  188. int[] greaterInts = new int[findNums.Length];
  189. int j = 0, flag = 0;
  190. for (int i = 0; i < findNums.Length; i++)
  191. {
  192. j = Array.IndexOf(nums, findNums[i]) + 1;
  193. while (j < nums.Length)
  194. {
  195. if (findNums[i] < nums[j])
  196. {
  197. greaterInts[i] = nums[j];
  198. flag = 1;
  199. j = nums.Length;
  200. }
  201. j++;
  202. }
  203.  
  204. if (flag == 0)
  205. {
  206. greaterInts[i] = -1;
  207. }
  208. flag = 0;
  209. }
  210. return greaterInts;
  211. }
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement