Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.48 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. //461. Hamming Distance ~ Easy = Accepted 149/149
  13. public static int HammingDistance(int x, int y)
  14. {
  15. string xBinary = Convert.ToString(x, 2);
  16. string yBinary = Convert.ToString(y, 2);
  17. int differences = 0;
  18. int repeat = Math.Max(xBinary.Length, yBinary.Length);
  19. for (int i = 0; i < repeat; i++)
  20. {
  21. if (xBinary.Length < repeat)
  22. {
  23. xBinary = "0" + xBinary;
  24. }
  25.  
  26. if (yBinary.Length < repeat)
  27. {
  28. yBinary = "0" + yBinary;
  29. }
  30. }
  31.  
  32. for (int i = 0; i < repeat; i++)
  33. {
  34. if (xBinary[i] != yBinary[i])
  35. {
  36. differences++;
  37. }
  38. }
  39. return differences;
  40. }
  41.  
  42. //561. Array Partition I ~ Easy = Accepted 81/81
  43. public static int ArrayPairSum(int[] nums)
  44. {
  45. if (nums.Count() == 2)
  46. {
  47. return nums[0];
  48. }
  49.  
  50. List<int> numList = nums.ToList();
  51. numList.Sort();
  52.  
  53. int sum = 0;
  54. for (int i = 0; i < numList.Count; i += 2)
  55. {
  56. sum += numList[i];
  57. }
  58. return sum;
  59. }
  60.  
  61. //476. Number Complement ~ Easy = Accepted 149/149
  62. public static int FindComplement(int num)
  63. {
  64. string numBinary = Convert.ToString(num, 2);
  65. string compBinary = "";
  66.  
  67. for (int i = 0; i < numBinary.Length; i++)
  68. {
  69. compBinary += numBinary[i] == '0' ? 1 : 0;
  70. }
  71. return Convert.ToInt32(compBinary, 2);
  72. }
  73.  
  74. //566. Reshape the Matrix ~ Easy = Accepted 56/56
  75. public static int[,] MatrixReshape(int[,] nums, int r, int c)
  76. {
  77. if (r * c > nums.GetLength(0) * nums.GetLength(1))
  78. {
  79. return nums;
  80. }
  81.  
  82. Queue<int> numQueue = new Queue<int>();
  83. int[,] newMatrix = new int[r, c];
  84.  
  85. for (int i = 0; i < nums.GetLength(0); i++)
  86. {
  87. for (int j = 0; j < nums.GetLength(1); j++)
  88. {
  89. numQueue.Enqueue(nums[i, j]);
  90. }
  91. }
  92.  
  93. for (int i = 0; i < newMatrix.GetLength(0); i++)
  94. {
  95. for (int j = 0; j < newMatrix.GetLength(1); j++)
  96. {
  97. newMatrix[i, j] = numQueue.Dequeue();
  98. }
  99. }
  100.  
  101. return newMatrix;
  102. }
  103.  
  104. //557. Reverse Words in a String III ~ Easy = Accepted ~ 30/30
  105. public static string ReverseWords(string s)
  106. {
  107. string[] splitString = s.Split(null);
  108. StringBuilder sb = new StringBuilder();
  109. for (int i = 0; i < splitString.Count(); i++)
  110. {
  111. char[] currentWord = splitString[i].ToCharArray();
  112. sb.Append(string.Join("", currentWord.Reverse()));
  113. sb.Append(" ");
  114. }
  115. return sb.ToString().Trim();
  116. }
  117.  
  118. //500. Keyboard Row ~ Easy = Accepted ~ 22/22
  119. public static string[] FindWords(string[] words)
  120. {
  121. List<string> oneLineWords = new List<string>();
  122. for (int i = 0; i < words.Length; i++)
  123. {
  124. if (Regex.Matches(words[i].ToLower(), "[qwertyuiop]").Count == words[i].Length ||
  125. Regex.Matches(words[i].ToLower(), "[asdfghjkl]").Count == words[i].Length ||
  126. Regex.Matches(words[i].ToLower(), "[zxcvbnm]").Count == words[i].Length)
  127. {
  128. oneLineWords.Add(words[i]);
  129. }
  130. }
  131. return oneLineWords.ToArray();
  132. }
  133.  
  134. //575. Distribute Candies ~ Easy = Accepted ~ 207/207
  135. public static int DistributeCandies(int[] candies)
  136. {
  137. if (candies.Distinct().Count() > candies.Count() / 2)
  138. {
  139. return candies.Count() / 2;
  140. }
  141. return candies.Distinct().Count();
  142. }
  143.  
  144. //412. Fizz Buzz ~ Easy = Accepted ~ 8/8
  145. public static List<string> FizzBuzz(int n)
  146. {
  147. List<string> fizzBuzz = new List<string>();
  148.  
  149. for (int i = 1; i <= n; i++)
  150. {
  151. string current = "";
  152. if (i % 3 == 0)
  153. {
  154. current += "Fizz";
  155. }
  156. if (i % 5 == 0)
  157. {
  158. current += "Buzz";
  159. }
  160. if (i % 3 != 0 && i % 5 != 0)
  161. {
  162. current += i.ToString();
  163. }
  164.  
  165. fizzBuzz.Add(current);
  166. }
  167.  
  168. return fizzBuzz;
  169. }
  170.  
  171. //344. Reverse String ~ Easy = Accepted ~ 476/476
  172. public static string ReverseString(string s)
  173. {
  174. return string.Join("", s.ToCharArray().Reverse());
  175. }
  176.  
  177. //496. Next Greater Element I ~ Easy = Accepted ~ 17/17
  178. public static int[] NextGreaterElement(int[] findNums, int[] nums)
  179. {
  180. int[] greaterInts = new int[findNums.Length];
  181. int j = 0, flag = 0;
  182. for (int i = 0; i < findNums.Length; i++)
  183. {
  184. j = Array.IndexOf(nums, findNums[i]) + 1;
  185. while (j < nums.Length)
  186. {
  187. if (findNums[i] < nums[j])
  188. {
  189. greaterInts[i] = nums[j];
  190. flag = 1;
  191. j = nums.Length;
  192. }
  193. j++;
  194. }
  195.  
  196. if (flag == 0)
  197. {
  198. greaterInts[i] = -1;
  199. }
  200. flag = 0;
  201. }
  202. return greaterInts;
  203. }
  204.  
  205. //463. Island Perimeter ~ Easy = Accepted ~ 5833/5833
  206. public static int IslandPerimeter(int[,] grid)
  207. {
  208. int perimeter = 0;
  209. for (int i = 0; i < grid.GetLength(0); i++)
  210. {
  211. for (int j = 0; j < grid.GetLength(1); j++)
  212. {
  213. if ((int)grid.GetValue(i, j) == 1)
  214. {
  215. if (i - 1 < 0 || grid[i - 1, j] == 0)
  216. {
  217. perimeter++;
  218. }
  219. if (j - 1 < 0 || grid[i, j - 1] == 0)
  220. {
  221. perimeter++;
  222. }
  223. if (j + 1 >= grid.GetLength(1) || grid[i, j + 1] == 0)
  224. {
  225. perimeter++;
  226. }
  227. if (i + 1 >= grid.GetLength(0) || grid[i + 1, j] == 0)
  228. {
  229. perimeter++;
  230. }
  231. }
  232. }
  233. }
  234. return perimeter;
  235. }
  236.  
  237. //292. Nim Game ~ Easy = Accepted ~ 60/60
  238. public static bool CanWinNim(int n)
  239. {
  240. return !(n % 4 == 0);
  241. }
  242.  
  243. //485. Max Consecutive Ones ~ Easy = Accepted ~ 41/41
  244. public static int FindMaxConsecutiveOnes(int[] nums)
  245. {
  246. if (!nums.Contains(0))
  247. {
  248. return nums.Length;
  249. }
  250. int count = 0, maxCount = 0;
  251. for (int i = 0; i < nums.Length; i++)
  252. {
  253. if (nums[i] == 1)
  254. {
  255. count++;
  256. }
  257. else
  258. {
  259. maxCount = Math.Max(maxCount, count);
  260. count = 0;
  261. }
  262. }
  263. return Math.Max(maxCount, count);
  264. }
  265.  
  266. //136. Single Number ~ Easy = Accepted ~ 15/15
  267. public static int SingleNumber(int[] nums)
  268. {
  269. Array.Sort(nums);
  270. if (nums.Length == 1)
  271. {
  272. return nums[0];
  273. }
  274.  
  275. for (int i = 0; i < nums.Length - 1; i += 2)
  276. {
  277. if (nums[i] != nums[i + 1])
  278. {
  279. return nums[i];
  280. }
  281. }
  282.  
  283. if (nums[nums.Length - 1] != nums[nums.Length - 2])
  284. {
  285. return nums[nums.Length - 1];
  286. }
  287. return -1;
  288. }
  289.  
  290. //521. Longest Uncommon Subsequence I ~ Easy = Accepted ~ 41/41
  291. public static int FindLUSlength(string a, string b)
  292. {
  293. if (a.Equals(b))
  294. {
  295. return -1;
  296. }
  297. return Math.Max(a.Length, b.Length);
  298. }
  299.  
  300. //520. Detect Capital ~ Easy = Accepted ~ 550/550
  301. public static bool DetectCapitalUse(string word)
  302. {
  303. char[] wordChar = word.ToCharArray();
  304. if (word.Length == 1)
  305. {
  306. return true;
  307. }
  308. else if (word.Length == 2)
  309. {
  310. if (Char.IsUpper(wordChar[0]))
  311. {
  312. return true;
  313. }
  314. else
  315. {
  316. return !Char.IsUpper(wordChar[1]);
  317. }
  318. }
  319.  
  320. for (int i = 1; i < word.Length; i++)
  321. {
  322. if (Char.IsUpper(wordChar[0]))
  323. {
  324. if (Char.IsUpper(wordChar[1]))
  325. {
  326. if (!Char.IsUpper(wordChar[i]))
  327. {
  328. return false;
  329. }
  330. }
  331. else
  332. {
  333. if (Char.IsUpper(wordChar[i]))
  334. {
  335. return false;
  336. }
  337. }
  338. }
  339. else
  340. {
  341. if (Char.IsUpper(wordChar[i]))
  342. {
  343. return false;
  344. }
  345. }
  346. }
  347. return true;
  348. }
  349.  
  350. //448. Find All Numbers Disappeared in an Array
  351. public static List<int> FindDisappearedNumbers(int[] nums)
  352. {
  353. Array.Sort(nums);
  354. List<int> missing = new List<int>();
  355. List<int> numsList = nums.ToList();
  356.  
  357. for (int i=0; i< numsList.Count; i++)
  358. {
  359. if(!(numsList[i] == numsList[i + 1] || numsList[i] + 1 == numsList[i + 1]))
  360. {
  361. //missing.Add(i+1);
  362. numsList.Insert(i, i + 1);
  363. i-=2;
  364. }
  365. }
  366.  
  367. return numsList.ToList();
  368. }
  369.  
  370. static void Main(string[] argsConsole)
  371. {
  372. FindDisappearedNumbers(new int[8] { 1,2,2,3,3,4,7,8 }).ForEach(Console.WriteLine);
  373. //Console.WriteLine(FindDisappearedNumbers(new int[8] { 1,2,2,3,3,4,7,8 }));
  374. Console.WriteLine("fin");
  375. Console.ReadKey();
  376. }
  377. }
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement