代码片段

倒序匹配获取正确区域名称

问题:有些区域直属于省,但其区域编码按层级又在市级别以下,如何正确获取这些区域的名称
方案:递归查找时,标识是否找到,若找到才倒序插入列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<html>

<head>
<script type="text/javascript">
var list = [];
var data = [{
value: "51",
children: [{
value: "5101",
children: [{
value: "510104",
children: []
}]
}]
}, {
value: "61",
children: [{
value: "6101",
children: [{
value: "610101",
children: []
}]
}, {
value: "610194",
children: [{
value: "61019401",
children: []
}]
}]
}];
var selectedValue = "61019401";
var got = false;
var find = function (item) {
if (selectedValue.startsWith(item.value)) {
if(item.children && item.children.length > 0) {
for (var j = 0; j < item.children.length; j++) {
find(item.children[j])
}
}
if (selectedValue == item.value) got = true
if (got) {
list.push(item);
}
}
}
find(data);
console.log(list);
</script>
</head>

</html>

由列表构建层级

区域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private static List<dynamic> GetAreaList(List<QR_Area> areaList, int type, string code = "86")
{
var list = new List<dynamic>();
var l = areaList.Where(a => (int)a.RealAreaType == type && (a.SortCode.StartsWith(code) || code == "86") && !string.IsNullOrWhiteSpace(a.Name)).
Select(a => new {
value = a.SortCode,
label = a.Name,
children = GetAreaList(areaList, type + 1, a.SortCode)
}).ToList();

if (l.Count > 0)
{
if (code == "6101")
{
l = l.Where(a => !a.value.StartsWith("610194")).ToList();
}
list.AddRange(l);
}
else
{
list = null;
}
return list;
}

组织

构建组织

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private static dynamic ConstructContractor(List<SimpleContractor> list)
{
if (list == null || list.Count == 0) return null;
var l = list;
var top = l[0];
if (l.Count > 1)
{
l.RemoveAt(0);
var groups = l.GroupBy(c => c.C.Substring(0, top.C.Length + 4));
return new { Code = top.C, Name = top.N, Children = groups.Select(g => {
var sub = g.ToList().OrderBy(c => c.C.Length).OrderBy(c => c.C).ToList();
if (sub[0].C.Length > top.C.Length + 4) return null;
return ConstructContractor(sub);
}).Where(x => x != null).ToList() };
}
return new { Code = top.C, Name = top.N };
}

获取组织

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private static dynamic GetContractor(dynamic contractor, string code)
{
if (contractor.Code.Equals(code)) return contractor;

if (code.StartsWith(contractor.Code))
{
if (contractor.Children != null && contractor.Children.Count > 0)
{
for (var i = 0; i < contractor.Children.Count; i++)
{
var result = GetContractor(contractor.Children[i], code);
if (result != null) return result;
}
}
}

return null;
}

找出字符串的最大深度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func findDepth(s string) int {
var current int = 0
var max int = 0

b := []byte(s)
for i := 0; i < len(b); i++ {
x := b[i]
if x == '(' {
current += 1
if current > max {
max = current
}
}
if x == ')' && current > 0 {
current -= 1
}
}

return max - current
}
1
2
input:  (1+2+(1*2)(1+2))
output: 3

找出序列中第K大的数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
func getKMax(s []int, k int) int {
if k < 1 || len(s) < k {
return -1
}

//将前k个数构建最小堆
constructMinHeap(s, k)

//将k以后的数与s[0]进行比较
//若比s[0]大, 则与s[0]交换, 并自顶向下重构最小堆
for i := k; i < len(s); i++ {
if s[i] <= s[0] {
continue
}

s[0], s[i] = s[i], s[0]

x := 0
for x < k {
x = reConstructMinHeap(s, k, x)
}
}

return s[0]
}

func reConstructMinHeap(s []int, k int, current int) int {
//将current与 left=2*current+1 right=2*current+2 中最小的进行比较, 若current较大,则进行交换
left := 2*current + 1
if left < k {
minIndex := left
right := 2*current + 2
if right < k && s[right] < s[left] {
minIndex = right
}
if s[minIndex] < s[current] {
s[current], s[minIndex] = s[minIndex], s[current]
return minIndex
}
}

return k
}

func constructMinHeap(s []int, k int) {
for i := k - 1; i > 0; i = i - 2 {
parent := (i - 1) / 2
left := 2*parent + 1
right := 2*parent + 2
minIndex := left
if right < k && s[right] < s[left] {
minIndex = right
}
if s[parent] > s[minIndex] {
s[parent], s[minIndex] = s[minIndex], s[parent]
}
}
}
1
2
input:  [10,9,8,7,6,5,4,3,2,1], 3
output: 8