交换排序——2快速排序实现
摘要:快速排序的效率比较高的算法,如果我们只能掌握一种排序,那快速排序是最佳的选择。 private int Division(int[] list, int left, int right) { // 以最左边的数(left)为基准 int bs = list[left]; while (left <
阅读全文
posted @
2017-05-31 08:17
Sempron2800+
阅读(151)
推荐(0) 编辑
交换排序——1冒泡排序实现
摘要:冒泡排序是,最长用的一种排序方式。它效率虽然不是很高,但是思路简单。 public void BubbleSort(int[] a) { int i, j; var n = a.Length; for (i = n - 1; i > 0; i--) { // 将a[0...i]中最大的数据放在末尾
阅读全文
posted @
2017-05-31 08:10
Sempron2800+
阅读(181)
推荐(0) 编辑
插入排序——3希尔排序实现
摘要:希尔排序又称缩小增量排序,这种排序方法先将整体的无序序列进行分组,设定每个组的大小为分组因子dk。分完组后,第i个和第i+dk个,i+2dk个,i+3dk个...元素为一个组。然后对这个组进行某种方式的排序,可以使用插入排序。 对每个组排完序之后,得到一个“有序程度”好一些的序列。然后缩小分组因子d
阅读全文
posted @
2017-05-29 09:46
Sempron2800+
阅读(192)
推荐(0) 编辑
归并排序实现
摘要:public void Merge(int[] a, int start, int mid, int end) { int[] tmp = new int[end - start + 1]; // tmp是汇总2个有序区的临时区域 int i = start; // 第1个有序区的索引 int j
阅读全文
posted @
2017-05-28 11:55
Sempron2800+
阅读(159)
推荐(0) 编辑
插入排序——2折半插入排序实现
摘要:折半插入与直接插入的不同在于,搜索要插入的位置的时候,使用的是折半搜索(二分搜索)。这种查找方式理论上比顺序查找的效率要高。 其代码实现如下: public IList<int> InsertionSort(int[] ary) { for (int i = 1; i < ary.Length; i
阅读全文
posted @
2017-05-27 13:41
Sempron2800+
阅读(182)
推荐(0) 编辑
插入排序——1直接插入排序实现
摘要:直接插入排序的一种实现: public IList<int> InsertionSort(int[] ary) { var len = ary.Length; for (int i = 1; i < len; i++) { var key = ary[i]; for (int j = 0; j <
阅读全文
posted @
2017-05-27 11:11
Sempron2800+
阅读(287)
推荐(0) 编辑
leetcode375
摘要:https://leetcode.com/problems/guess-number-higher-or-lower-ii/#/description
阅读全文
posted @
2017-05-24 09:51
Sempron2800+
阅读(127)
推荐(0) 编辑
leetcode421
摘要:https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/#/description
阅读全文
posted @
2017-05-23 10:30
Sempron2800+
阅读(198)
推荐(0) 编辑
leetcode582
摘要:https://leetcode.com/problems/kill-process/#/solutions tree中记录的是每个进程及其直接的子进程。然后在调用traverse方法,这个方法是递归的进行寻找,每个级别的进程及其子进程,将其全部加入到要删除的进程列表result中。
阅读全文
posted @
2017-05-23 10:25
Sempron2800+
阅读(353)
推荐(0) 编辑
leetcode592
摘要:https://leetcode.com/problems/fraction-addition-and-subtraction/#/description
阅读全文
posted @
2017-05-23 09:44
Sempron2800+
阅读(202)
推荐(0) 编辑
leetcode594
摘要:https://leetcode.com/problems/longest-harmonious-subsequence/#/description
阅读全文
posted @
2017-05-22 17:32
Sempron2800+
阅读(145)
推荐(0) 编辑
leetcode560
摘要:https://leetcode.com/problems/subarray-sum-equals-k/#/solutions 补充一个python的实现,和上面的思路一样: 下面进行解释,字典dic中存储的是某一个连续和的出现频率。例如nums=[1,1,1],k=2。 第一次循环时,sums等于
阅读全文
posted @
2017-05-22 16:44
Sempron2800+
阅读(234)
推荐(0) 编辑
leetcode581
摘要:public class Solution { public int FindUnsortedSubarray(int[] nums) { int n = nums.Length, beg = -1, end = -2, min = nums[n - 1], max = nums[0]; for (
阅读全文
posted @
2017-05-22 16:06
Sempron2800+
阅读(148)
推荐(0) 编辑
leetcode22
摘要:https://leetcode.com/problems/generate-parentheses/#/description 重新实现了一遍,也是使用回溯法。先使用python来做,本地测试正常,但是在leetcode上提交却WA,于是把python代码改写为C#,则AC了。 因此判断是OJ对p
阅读全文
posted @
2017-05-14 09:42
Sempron2800+
阅读(178)
推荐(0) 编辑
leetcode216
摘要:https://leetcode.com/problems/combination-sum-iii/#/description
阅读全文
posted @
2017-05-14 09:36
Sempron2800+
阅读(88)
推荐(0) 编辑
leetcode494
摘要:https://leetcode.com/problems/target-sum/#/description 补充一个python的实现: 这道题的主要思想是利用字典存储之前的已经计算过的值,但是做题的时候状态不太好,所以写了很久。 做算法题,头脑不清醒的时候,效率很低,应该休息,把状态调整好再做。
阅读全文
posted @
2017-05-13 18:55
Sempron2800+
阅读(183)
推荐(0) 编辑
leetcode12
摘要:public class Solution { public string IntToRoman(int num) { string[] M = { "", "M", "MM", "MMM" }; string[] C = { "", "C", "CC", "CCC", "CD", "D", "DC
阅读全文
posted @
2017-05-13 13:57
Sempron2800+
阅读(154)
推荐(0) 编辑
leetcode378
摘要:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/#/description 补充一个多次使用二分查找的解决方案:
阅读全文
posted @
2017-05-13 11:42
Sempron2800+
阅读(218)
推荐(0) 编辑
leetcode144
摘要:https://leetcode.com/problems/binary-tree-preorder-traversal/#/description
阅读全文
posted @
2017-05-13 11:36
Sempron2800+
阅读(125)
推荐(0) 编辑
leetcode392
摘要:https://leetcode.com/problems/is-subsequence/#/description
阅读全文
posted @
2017-05-13 11:32
Sempron2800+
阅读(102)
推荐(0) 编辑
leetcode486
摘要:https://leetcode.com/problems/predict-the-winner/#/description
阅读全文
posted @
2017-05-12 17:35
Sempron2800+
阅读(152)
推荐(0) 编辑
leetcode481
摘要:https://leetcode.com/problems/magical-string/#/description
阅读全文
posted @
2017-05-11 15:58
Sempron2800+
阅读(155)
推荐(0) 编辑
leetcode94
摘要:二叉树的中序遍历。 补充python版本实现:
阅读全文
posted @
2017-05-11 12:14
Sempron2800+
阅读(194)
推荐(0) 编辑
leetcode343
摘要:https://leetcode.com/problems/integer-break/#/description 这道题的解题思路是,从2到n/2每个值依次进行尝试,对于每一个值,计算这种情况下的因子乘积, 如果是可以被整除,则计算当前值的“倍数的次方”;如果不能被整除,则分两种情况分别计算。 第
阅读全文
posted @
2017-05-11 12:07
Sempron2800+
阅读(212)
推荐(0) 编辑
leetcode454
摘要:https://leetcode.com/problems/4sum-ii/#/description 补充一个python的版本:
阅读全文
posted @
2017-05-11 11:47
Sempron2800+
阅读(172)
推荐(0) 编辑
leetcode539
摘要:https://leetcode.com/problems/minimum-time-difference/#/description
阅读全文
posted @
2017-05-11 10:59
Sempron2800+
阅读(148)
推荐(0) 编辑
leetcode357
摘要:https://leetcode.com/problems/count-numbers-with-unique-digits/#/description
阅读全文
posted @
2017-05-10 21:15
Sempron2800+
阅读(169)
推荐(0) 编辑
leetcode384
摘要:https://leetcode.com/problems/shuffle-an-array/#/description
阅读全文
posted @
2017-05-10 21:00
Sempron2800+
阅读(129)
推荐(0) 编辑
leetcode445
摘要:https://leetcode.com/problems/add-two-numbers-ii/#/description 补充一个python的实现:
阅读全文
posted @
2017-05-10 20:16
Sempron2800+
阅读(249)
推荐(0) 编辑
leetcode498
摘要:https://leetcode.com/problems/diagonal-traverse/#/description
阅读全文
posted @
2017-05-10 18:49
Sempron2800+
阅读(214)
推荐(0) 编辑
leetcode477
摘要:https://leetcode.com/problems/total-hamming-distance/#/description
阅读全文
posted @
2017-05-10 16:29
Sempron2800+
阅读(123)
推荐(0) 编辑
leetcode382
摘要:https://leetcode.com/problems/linked-list-random-node/#/description
阅读全文
posted @
2017-05-10 16:18
Sempron2800+
阅读(122)
推荐(0) 编辑
leetcode503
摘要:https://leetcode.com/problems/next-greater-element-ii/#/solutions 关键的思路是将数组延长到2倍的长度,然后用%运算,将索引缩小到1倍之内进行1倍长度内的判断。
阅读全文
posted @
2017-05-10 14:12
Sempron2800+
阅读(148)
推荐(0) 编辑
leetcode529
摘要:https://leetcode.com/problems/minesweeper/#/description 提供一种会超时的解决方案: 按照BFS的思路来写,但是判断比较麻烦。
阅读全文
posted @
2017-05-10 08:16
Sempron2800+
阅读(173)
推荐(0) 编辑
leetcode547
摘要:这是第一种方法,主要是使用dfs来实现。 下面是第二种方法,是同学用python写的,我翻译成了C#,这种思路是union find。 从耗时角度来看,第一种效率略高一些。 https://leetcode.com/problems/friend-circles/#/description
阅读全文
posted @
2017-05-10 08:04
Sempron2800+
阅读(168)
推荐(0) 编辑
leetcode347
摘要:https://leetcode.com/problems/top-k-frequent-elements/#/description 补充一个python的实现,使用封装好的类库,代码很简洁。
阅读全文
posted @
2017-05-10 07:18
Sempron2800+
阅读(155)
推荐(0) 编辑
leetcode238
摘要:https://leetcode.com/problems/product-of-array-except-self/#/description 在第一个循环中,记录当前的索引左侧的数字乘积,和当前的索引右侧的数字乘积。然后两部分乘积相乘,这样的思路比较简单,但是时间复杂度是O(n*n)。 本题的解
阅读全文
posted @
2017-05-09 18:31
Sempron2800+
阅读(138)
推荐(0) 编辑
leetcode260
摘要:https://leetcode.com/problems/single-number-iii/#/description
阅读全文
posted @
2017-05-09 16:36
Sempron2800+
阅读(104)
推荐(0) 编辑
leetcode451
摘要:https://leetcode.com/problems/sort-characters-by-frequency/#/description 补充一个python的实现:
阅读全文
posted @
2017-05-09 16:12
Sempron2800+
阅读(154)
推荐(0) 编辑
leetcode462
摘要:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/#/description
阅读全文
posted @
2017-05-09 16:05
Sempron2800+
阅读(133)
推荐(0) 编辑
leetcode572
摘要:https://leetcode.com/problems/subtree-of-another-tree/#/description 补充一个使用python的实现,思路就是使用二叉树的先序遍历,将节点和空节点存储到字符串中,然后比较t是否是s的字串。 执行效率还是比较高的。 再补充一个双层递归的
阅读全文
posted @
2017-05-09 13:10
Sempron2800+
阅读(201)
推荐(0) 编辑
leetcode566
摘要:https://leetcode.com/problems/reshape-the-matrix/#/description
阅读全文
posted @
2017-05-09 11:59
Sempron2800+
阅读(144)
推荐(0) 编辑
leetcode575
摘要:https://leetcode.com/problems/distribute-candies/#/description
阅读全文
posted @
2017-05-09 11:50
Sempron2800+
阅读(125)
推荐(0) 编辑
leetcode8
摘要:https://leetcode.com/problems/string-to-integer-atoi/#/description 补充一个python的实现:
阅读全文
posted @
2017-05-08 10:24
Sempron2800+
阅读(154)
推荐(0) 编辑
leetcode6
摘要:C#版: public class Solution { public string Convert(string s, int numRows) { char[] c = s.ToArray(); int len = c.Length; StringBuilder[] sb = new Strin
阅读全文
posted @
2017-05-06 18:35
Sempron2800+
阅读(129)
推荐(0) 编辑
leetcode5
摘要:public class Solution { private int lo, maxLen; public String LongestPalindrome(String s) { int len = s.Length; if (len < 2) return s; for (int i = 0;
阅读全文
posted @
2017-05-04 22:19
Sempron2800+
阅读(223)
推荐(0) 编辑
leetcode3
摘要:1 public class Solution 2 { 3 public int LengthOfLongestSubstring(string s) 4 { 5 var dic = new Dictionary<char, int>(); 6 var maxLength = 0; 7 for (i
阅读全文
posted @
2017-05-03 22:07
Sempron2800+
阅读(420)
推荐(0) 编辑
leetcode2
摘要:https://leetcode.com/problems/add-two-numbers/#/description 补充python实现:
阅读全文
posted @
2017-05-03 16:57
Sempron2800+
阅读(597)
推荐(0) 编辑
leetcode495
摘要:https://leetcode.com/problems/teemo-attacking/#/description
阅读全文
posted @
2017-05-03 10:30
Sempron2800+
阅读(123)
推荐(0) 编辑
leetcode508
摘要:https://leetcode.com/problems/most-frequent-subtree-sum/#/description
阅读全文
posted @
2017-05-03 09:39
Sempron2800+
阅读(170)
推荐(0) 编辑
leetcode538
摘要:/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNod
阅读全文
posted @
2017-05-02 21:05
Sempron2800+
阅读(224)
推荐(0) 编辑
leetcode553
摘要:https://leetcode.com/problems/optimal-division/#/description
阅读全文
posted @
2017-05-02 17:54
Sempron2800+
阅读(151)
推荐(0) 编辑
leetcode442
摘要:https://leetcode.com/problems/find-all-duplicates-in-an-array/#/description
阅读全文
posted @
2017-05-02 17:24
Sempron2800+
阅读(92)
推荐(0) 编辑
leetcode515
摘要:https://leetcode.com/problems/find-largest-value-in-each-tree-row/#/description
阅读全文
posted @
2017-05-02 13:55
Sempron2800+
阅读(126)
推荐(0) 编辑
leetcode526
摘要:https://leetcode.com/problems/beautiful-arrangement/#/description
阅读全文
posted @
2017-05-02 13:38
Sempron2800+
阅读(176)
推荐(0) 编辑
leetcode406
摘要:https://leetcode.com/problems/queue-reconstruction-by-height/#/description 上面这个写的够长的了,用python,4行就可以实现: 先按照第一个元素倒序排,再按照第二个元素正序排,然后用insert方法,在指定的index上插
阅读全文
posted @
2017-05-01 20:27
Sempron2800+
阅读(222)
推荐(0) 编辑