Zhuanli&Blog


  • 首页

  • 标签

  • 分类

  • 归档

leetcode 395. Longest Substring with At Least K Repeating Characters

发表于 2018-05-21 | 分类于 leetcode

Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.

Example 1:


Input:
s = “aaabb”, k = 3
Output:
3
The longest substring is “aaa”, as ‘a’ is repeated 3 times.

Example 2:


Input:
s = “ababbc”, k = 2
Output:
5
The longest substring is “ababb”, as ‘a’ is repeated 2 times and ‘b’ is repeated 3 times.

1
2
3
4
5
6
7
8
9
10
11
class Solution(object):
def longestSubstring(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
for c in set(s):
if s.count(c) < k:
return max(self.longestSubstring(t,k) for t in s.split(c))
return len(s)

leetcode 376. Wiggle Subsequence

发表于 2018-05-18 | 分类于 leetcode

A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.

For example, [1,7,4,9,2,5]is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast,[1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.

Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.

Examples:

Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.

Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].

Input: [1,2,3,4,5,6,7,8,9]
Output: 2
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
class Solution(object):
def wiggleMaxLength(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) <= 1:
return len(nums)
dp = [0]*len(nums)
dp[0] = 1
if nums[1] == nums[0]:
dp[1] = 1
else:
dp[1] = 2
target = nums[1] - nums[0]
for i in range(2,len(nums)):
target2 = nums[i] - nums[i-1]
if target*target2 < 0 :#bu tong hao
dp[i] = dp[i-1] + 1
elif target == 0 and target2 != 0:
dp[i] = dp[i-1] + 1
else:
dp[i] = dp[i-1]
if target2 != 0 :
target = target2
return dp[len(nums)-1]

leetcode 393. UTF-8 Validation

发表于 2018-05-17 | 分类于 leetcode

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:

  1. For 1-byte character, the first bit is a 0, followed by its unicode code.
  2. For n-bytes character, the first n-bits are all one’s, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.

This is how the UTF-8 encoding would work:






















Char. number range (hexadecimal)UTF-8 octet sequence (binary)
0000 0000-0000 007F0xxxxxxx
0000 0000-0000 07FF110xxxxx 10xxxxxx
0000 0800-0000 FFFF1110xxxx 10xxxxxx 10xxxxxx
0001 0000-0010 FFFF11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

Given an array of integers representing the data, return whether it is a valid utf-8 encoding.

Note:
The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data.

Example 1:

data = [197, 130, 1], which represents the octet sequence: 11000101 10000010 00000001.
Return true.
It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.

Example 2:


data = [235, 140, 4], which represented the octet sequence: 11101011 10001100 00000100.
Return false.
The first 3 bits are all one’s and the 4th bit is 0 means it is a 3-bytes character.
The next byte is a continuation byte which starts with 10 and that’s correct.
But the second continuation byte does not start with 10, so it is invalid.

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
bool validUtf8(int* data,int length)  
{
unsigned char nBytes = 0;//UFT8可用1-4个字节编码,ASCII用一个字节
unsigned char chr;
for(int i = 0; i < length; ++i)
{
chr = data[i];
if(nBytes == 0) //如果不是ASCII码,应该是多字节符,计算字节数
{
if(chr >= 0x80)
{
if(chr >= 0xF0 && chr <= 0xF7) //11110xxx
nBytes=4;
else if(chr >= 0xE0 && chr <= 0xEF) //1110xxxx
nBytes=3;
else if(chr >= 0xC0 && chr <= 0xDF) //110xxxxx
nBytes=2;
else
return 0;
nBytes--;
}
}
else //多字节符的非首字节,应为 10xxxxxx
{
if( (chr&0xC0) != 0x80 )
return 0;
nBytes--;
}
}
if( nBytes > 0) //违返规则
return 0;
return 1;
}

URL中的#号的作用

发表于 2018-05-15 | 分类于 html/css/js

一、#的意思/作用

  #代表网页中的一个位置。类似于PPT里面的页内超链接。#右面的字符,就是该位置的标识符(即 这个位置的标记)。比如,http://www.XXX.com/index.html#location就代表www.xxx.com这个网站index.html这个网页的location位置。我想这个例子应该很清楚了。浏览器读取这个URL后,会自动将‘location’位置滚动至可视区域。

  为网页位置指定标识符,有两个方法。一是使用锚点,比如<a name="location"></a>,二是使用id属性,比如<div id="location">。

二、HTTP请求不包括

  #是用来指导浏览器产生行为的,对服务器端完全没用。我的理解是帮助你锁定/定位这个页面的某个位置的。所以,HTTP请求中不包括#。

比如,访问下面的网址,http://www.XXX.com/index.html#location,浏览器实际发出的请求是这样的:

GET /index.html HTTP/1.1
Host:www.XXX.com

三、#后的字符

  在第一个#后面出现的任何字符,都会被浏览器解读为位置标识符。这意味着,这些字符都不会被发送到服务器端。
比如,下面URL的原意是指定一个颜色值:http://www.XXX.com/?color=#fff,但是,浏览器实际发出的请求是:

GET /?color= HTTP/1.1
Host: www.XXX.com   

四、改变#不触发网页重载

    单单改变#后的部分,浏览器只会滚动到相应位置,不会重新加载网页。所以不要看页面有个跳的动作,就以为页面跳转或者服务器端有所响应,其实没有。
     比如,从http://www.XXX.com/index.html#location1改成http://www.XXX.com/index.html#location2,浏览器不会重新向服务器请求index.html。

五、改变#会改变浏览器的访问历史

  每一次改变#后的部分,都会在浏览器的访问历史中增加一个记录,使用”后退”按钮,就可以回到上一个位置。这对于ajax应用程序特别有用,可以用不同的#值,表示不同的访问状态,然后向用户给出可以访问某个状态的链接。值得注意的是,上述规则对IE 6和IE 7不成立,它们不会因为#的改变而增加历史记录。

六、Google抓取#的机制

  默认情况下,Google的网络蜘蛛忽视URL的#部分。
  但是,Google还规定,如果你希望Ajax生成的内容被浏览引擎读取,那么URL中可以使用”#!”,Google会自动将其后面的内容转成查询字符串_escaped_fragment_的值。
  比如,Google发现新版twitter的URL:http://twitter.com/#!/username
  就会自动抓取另一个URL:http://twitter.com/?_escaped_fragment_=/username
  通过这种机制,Google就可以索引动态的Ajax内容。

注

AJAX = 异步 JavaScript和XML(标准通用标记语言的子集)。AJAX 是一种用于创建快速动态网页的技术。

实现hash

发表于 2018-05-10 | 分类于 c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define TABLE_SIZE (1024*1024)

/* element of the hash table's chain list */
struct kv
{
struct kv* next;
char* key;
void* value;
void(*free_value)(void*);
};

/* HashTable */
struct HashTable
{
struct kv **table;
};

/* constructor of struct kv */
static void init_kv(struct kv* kv)
{
kv->next = NULL;
kv->key = NULL;
kv->value = NULL;
kv->free_value = NULL;
}
/* destructor of struct kv */
static void free_kv(struct kv* kv)
{
if (kv) {
if (kv->free_value) {
kv->free_value(kv->value);
}
free(kv->key);
kv->key = NULL;
free(kv);
}
}
/* the classic Times33 hash function */
static unsigned int hash_33(char* key)
{
unsigned int hash = 0;
while (*key) {
hash = (hash << 5) + hash + *key++;
}
return hash;
}

/* new a HashTable instance */
HashTable* hash_table_new()
{
HashTable* ht = malloc(sizeof(HashTable));
if (NULL == ht) {
hash_table_delete(ht);
return NULL;
}
ht->table = malloc(sizeof(struct kv*) * TABLE_SIZE);
if (NULL == ht->table) {
hash_table_delete(ht);
return NULL;
}
memset(ht->table, 0, sizeof(struct kv*) * TABLE_SIZE);

return ht;
}
/* delete a HashTable instance */
void hash_table_delete(HashTable* ht)
{
if (ht) {
if (ht->table) {
int i = 0;
for (i = 0; i<TABLE_SIZE; i++) {
struct kv* p = ht->table[i];
struct kv* q = NULL;
while (p) {
q = p->next;
free_kv(p);
p = q;
}
}
free(ht->table);
ht->table = NULL;
}
free(ht);
}
}

/* insert or update a value indexed by key */
int hash_table_put(HashTable* ht, char* key, void* value, void(*free_value)(void*))
{
int i = hash_33(key) % TABLE_SIZE;
struct kv* p = ht->table[i];
struct kv* prep = p;

while (p) { /* if key is already stroed, update its value */
if (strcmp(p->key, key) == 0) {
if (p->free_value) {
p->free_value(p->value);
}
p->value = value;
p->free_value = free_value;
break;
}
prep = p;
p = p->next;
}

if (p == NULL) {/* if key has not been stored, then add it */
char* kstr = malloc(strlen(key) + 1);
if (kstr == NULL) {
return -1;
}
struct kv * kv = malloc(sizeof(struct kv));
if (NULL == kv) {
free(kstr);
kstr = NULL;
return -1;
}
init_kv(kv);
kv->next = NULL;
strcpy(kstr, key);
kv->key = kstr;
kv->value = value;
kv->free_value = free_value;

if (prep == NULL) {
ht->table[i] = kv;
}
else {
prep->next = kv;
}
}
return 0;
}

/* get a value indexed by key */
void* hash_table_get(HashTable* ht, char* key)
{
int i = hash_33(key) % TABLE_SIZE;
struct kv* p = ht->table[i];
while (p) {
if (strcmp(key, p->key) == 0) {
return p->value;
}
p = p->next;
}
return NULL;
}

/* remove a value indexed by key */
void hash_table_rm(HashTable* ht, char* key)
{
int i = hash_33(key) % TABLE_SIZE;

struct kv* p = ht->table[i];
struct kv* prep = p;
while (p) {
if (strcmp(key, p->key) == 0) {
free_kv(p);
if (p == prep) {
ht->table[i] = NULL;
}
else {
prep->next = p->next;
}
}
prep = p;
p = p->next;
}
}

由爬楼梯问题与斐波那契数列的关联到一般化

发表于 2018-05-10 | 分类于 python

问题:

有10阶楼梯,每次只能走1-2步,不能后退,问有几种走法?

设F(n)表示n阶一共有多少走法,从简单的一步步看:

1
2
3
4
5
F(1) = 1
F(2) = 2
F(3) = 3
F(4) = 5
...

可以看到F(n) = F(n-2) + F(n-1)

推想到更一般化的问题:

有n阶楼梯,每次只能走1-m步,不能后退,问有几种走法?

可以推测:
F(n) = F(n-1) + F(n-2) + ... + F(n-m)

代码实现:

1
2
3
4
5
6
7
8
9
def climb(m,n):
dp = [0]*(n+1)
dp[0] = 1
for i in range(1,n+1):
for j in range(1,m+1):
if i < j:break
else:
dp[i] += dp[i-j]
return dp[n]

如何判断一个字符串是否是UTF8编码

发表于 2018-05-09 | 分类于 c

编码原理

  先看这个模板:

1
2
3
4
5
6
7
  UCS-4 range (hex.) UTF-8 octet sequence (binary)
  0000 0000-0000 007F 0xxxxxxx
  0000 0080-0000 07FF 110xxxxx 10xxxxxx
  0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
  0001 0000-001F FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  0020 0000-03FF FFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  0400 0000-7FFF FFFF 1111110x 10xxxxxx ... 10xxxxxx

代码实现

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
BOOL IsTextUTF8(char* str,ULONGLONG length)  
{
DWORD nBytes=0;//UFT8可用1-6个字节编码,ASCII用一个字节
UCHAR chr;
BOOL bAllAscii=TRUE; //如果全部都是ASCII, 说明不是UTF-8
for(int i=0; i<length; ++i)
{
chr= *(str+i);
if( (chr&0x80) != 0 ) // 判断是否ASCII编码,如果不是,说明有可能是UTF-8,ASCII用7位编码,但用一个字节存,最高位标记为0,o0xxxxxxx
bAllAscii= FALSE;
if(nBytes==0) //如果不是ASCII码,应该是多字节符,计算字节数
{
if(chr>=0x80)
{
if(chr>=0xFC&&chr<=0xFD)
nBytes=6;
else if(chr>=0xF8)
nBytes=5;
else if(chr>=0xF0)
nBytes=4;
else if(chr>=0xE0)
nBytes=3;
else if(chr>=0xC0)
nBytes=2;
else
return FALSE;

nBytes--;
}
}
else //多字节符的非首字节,应为 10xxxxxx
{
if( (chr&0xC0) != 0x80 )
return FALSE;

nBytes--;
}
}
if( nBytes > 0 ) //违返规则
return FALSE;
if( bAllAscii ) //如果全部都是ASCII, 说明不是UTF-8
return FALSE;

return TRUE;
}

libxx.a的反汇编

发表于 2018-05-04 | 分类于 linux

前提是知道 libxxx.a 是用什么平台编译出来的,就采用相应的工具链来操作:

(1) ar -x libxxx.a 命令可将 .a 文件生成若干个.o 文件

(2) objdump -D -S yyy.o 把 yyy.o 反汇编成 asm 汇编文件

具体命令的使用:


nm libxxx.a 可查看 .a 所依赖的库文件及源码中的函数接口名称

objdump -S libxxx.a 反汇编成汇编语言 但与平台严重相关, 有可能命令无法识别是什么平台的

ar -x libxxx.a 命令可将 .a 文件生成若干个.o 文件

  创建一个库,请输入:    ar -v -q lib.a strlen.o strcpy.o
  显示库的目录,请输入:    ar -v -t lib.a
  替换或添加新成员到库中,请输入: ar -v -r lib.a strlen.o strcat.o
  指定在何处插入新成员,请输入:     ar -v -r -b strlen.o lib.a strcmp.o
  更新一个已经更改过的成员,请输入: ar -v -r -u lib.a strcpy.o
  更改库成员的顺序,请输入:   ar -v -m -a strcmp.o lib.a strcat.o strcpy.o
  解压缩库成员,请输入:    ar -v -x lib.a strcat.o strcpy.o
  解压缩并重命名一个成员,请输入: ar -p lib.a strcpy.o >stringcopy.o
  删除一个成员,请输入:    ar -v -d lib.a strlen.o
  从多个用 ld 命令创建的共享模块中创建一个压缩文档库,请输入: ar -r -v libshr.a shrsub.o shrsub2.o shrsub3.o ...
  编译并链接使用 libshr.a 压缩文档库的 main 程序,请使用以下命令:cc -o main main.c -L/u/sharedlib -lshr
  列出 lib.a 的内容(忽略任何 32 位目标文件),请输入:   ar -X64 -t -v lib.a
  从 lib.a 解压缩所有 32 位的目标文件,请输入:     ar -X32 -x lib.a
  列出 lib.a 中的所有文件,无论是 32 位、64 位或非对象,请输入: ar -X32_64 -t -v lib.a

可以用“objdump -D”命令来反编译 .o 文件, 看你在编译c/c++用的-g级数, 如果-g3编译的,用objdump返汇编出来可以看到部分c/c++代码

Usage: objdump

Display information from object .
At least one of the following switches must be given:

-a, –archive-headers Display archive header information

-f, –file-headers Display the contents of the overall file header

-p, –private-headers Display object format specific file header contents

-h, –[section-]headers Display the contents of the section headers

-x, –all-headers Display the contents of all headers

-d, –disassemble Display assembler contents of executable sections

-D, –disassemble-all Display assembler contents of all sections

-S, –source Intermix source code with disassembly

-s, –full-contents Display the full contents of all sections requested

-g, –debugging Display debug information in object file

-e, –debugging-tags Display debug information using ctags style

-G, –stabs Display (in raw form) any STABS info in the file

-W, –dwarf Display DWARF info in the file

-t, –syms Display the contents of the symbol table(s)

-T, –dynamic-syms Display the contents of the dynamic symbol table

-r, –reloc Display the relocation entries in the file

-R, –dynamic-reloc Display the dynamic relocation entries in the file

-v, –version Display this program’s version number

-i, –info List object formats and architectures supported

-H, –help

leetcode 813. Largest Sum of Averages

发表于 2018-04-26 | 分类于 leetcode

We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?

Note that our partition must use every number in A, and that scores are not necessarily integers.

Example:


Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.

Note:

  • 1 <= A.length <= 100.
  • 1 <= A[i] <= 10000.
  • 1 <= K <= A.length.

Answers within 10^-6 of the correct answer will be accepted as correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def largestSumOfAverages(self, A, K):

dp = [0]*len(A)
s = 0
for i in range(len(A)):
s += A[i]
dp[i] = s/(i+1)

for i in range(1,K):
for j in range(len(A)-1, i-1, -1):
s = 0
count = 0
for k in range(j, i-1, -1):
s += A[k]
count += 1
dp[j] = max(dp[j], dp[k-1] + s/count)
return dp[-1]

冰雪奇缘

发表于 2018-04-26

冰雪奇缘

1…151617…20

zhuanli

194 日志
9 分类
41 标签
GitHub
© 2018 — 2019 zhuanli
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.4