TC CODE HOME


  • Home

  • Categories

  • Archives

  • Search

leetcode 25.Reverse Nodes in k Group

Posted on 2020-01-02 | In leetcode |

  Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

  Example:

Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

Note:

Only constant extra memory is allowed.
You may not alter the values in the list’s nodes, only nodes itself may be changed.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-nodes-in-k-group
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* cur = head;
return reverse(head, cur, k);
}

ListNode* reverse(ListNode* head, ListNode* cur, int k){
ListNode* tmp = cur;
int count = 0;
while(tmp){
tmp = tmp -> next;
count++;
}
if(count < k) return head;

ListNode* next = NULL;
ListNode* pre = NULL;
int n = k;

while(cur && n-- > 0){
next = cur -> next;
cur -> next = pre;
pre = cur;
cur = next;
}

head -> next = reverse(cur, cur, k);

return pre;
}
};

leetcode 24.Swap Nodes in Pairs

Posted on 2020-01-02 | In leetcode |

  Given a linked list, swap every two adjacent nodes and return its head.

You may not modify the values in the list’s nodes, only nodes itself may be changed.

  Example:

Given 1->2->3->4, you should return the list as 2->1->4->3.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

想要采用递归解法, 首先思考这几个问题:
1) 原始问题能否划分为若干个相同的子问题
2) 递归的结束条件是什么
3) 递归的调用条件

对于本题:

子问题: 需要两两一组地将链表反转, 可以将原问题划分为简单的若干子问题(绿框), 每个子问题的操作就是仅有的两个结点反转, 修改指针即可(蓝线).
结束条件: 当子问题中不足两个结点时, 无法完成反转, 此时递归跳出.
调用条件: 需要将各个子问题中反转的链表连接起来, 对于每个子问题的头节点, 在反转之后变成尾结点, 即第一个子问题的尾结点需要指向下一个子问题的头节点(红线), 即递归调用的结果.

C++:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == NULL || head -> next == NULL){
return head;
}
ListNode *next = head -> next;
head -> next = swapPairs(next -> next);
next -> next = head;
return next;
}
};

MOSSE

Posted on 2019-12-29 |

相关滤波原理

在统计学中,互相关有时用来表示两个随机矢量 X 和 Y 之间的协方差cov(X, Y),以与矢量 X 的“协方差”概念相区分,矢量 X 的“协方差”是 X 的各标量成分之间的协方差矩阵。

在信号处理领域中,互相关(cross-correlation, 有时也称为“互协方差”)是用来表示两个信号之间相似性的一个度量,通常通过与已知信号比较用于寻找未知信号中的特性。它是两个信号之间相对于时间的一个函数,有时也称为“滑动点积”,在模式识别以及密码分析学领域都有应用。

对于离散信号f和g, 两者的互相关表示为:

对于连续型信号, 两者的互相关表示为:

  互相关实质上类似于两个函数的卷积, 其中$f^*$表示f的复共轭. 相关性最直观的解释就是衡量两个函数在某个时刻的相似程度.

Markdown插入数学公式

Posted on 2019-12-29 |

  在markdown里面插入数学公式, 首先需要选取MathJax引擎, 把以下代码插入md文件即可.

1
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=default"></script>

  如果使用hexo之类的框架可以手动配置Mathjax引擎,这样就不需要每次都插入这一行代码.

插入方式

行内插入

1
\(a + b\)

  因为是在行内插入, 因此需要’\\’进行转义, 告诉引擎这是公式的’(‘而不是文本的’(‘. 当然还有另外一种方式:

1
$a + b$

  这是一行(a + b)
换行插入

1
$$a + b$$

上下标

1
$x_1$, $x^2$, $x_1^2$, $x^2_1$, $x_{22}^{(n)}$

  $x_1$,   $x^2$,   $x_1^2$,   $x^2_1$,   $x_{22}^{(n)}$

分式

1
$\frac{x+y}{2}$, $\frac{1}{1+\frac{1}{2}}$

  $\frac{x+y}{2}$,   $\frac{1}{1+\frac{1}{2}}$

根式

1
$\sqrt{2}<\sqrt[3]{27}$, $\sqrt{1+\sqrt[p]{1+a^2}}$

  $\sqrt{2}<\sqrt[3]{27}$,   $\sqrt{1+\sqrt[p]{1+a^2}}$

积分和求和

1
$\sum_{k=1}^{n}\frac{1}{k}$, $\int_a^b f(x)dx$

  $\sum_{k=1}^{n}\frac{1}{k}$,   $\int_a^b f(x)dx$

常用希腊字母

容器库概览

Posted on 2019-12-29 | In C++ |

  一般来说, 每个容器都定义在一个与类型名同名的头文件中. 例如: deque定义在头文件deque中, list定义在头文件list中.

对容器可保存类型的限制
  顺序容器几乎可以保存任意类型的元素. 特别地, 可以定义一个容器, 其元素类型是另一个容器. 例如:

1
vector<vector<string>> lines; //vector的vecotr

  此处的lines是一个vecotr, 其元素类型是string的vector.

  虽然容器可以保存几乎任意类型的元素, 但是某些容器对元素类型自己特殊的要求. 例如: 顺序容器的一个版本接受容器大小的参数, 它可以直接使用元素类型的默认构造函数.

1
vector<int> ivec(); //也可以不指定容器大小, vector<int> ivec;

  但是, 某些类型没有构造函数, 因此在构造保存这类型的容器时, 不能只给它一个参数数目的参数. 例如:

Read more »

顺序容器概述

Posted on 2019-12-29 | In C++ |

表1 顺序容器的类型

vector 可变大小的数组. 支持文件快速随机访问. 在尾部之外的位置插入和删除元素的速度可能很慢
deque 双端队列. 支持快速随机访问. 在头尾插入和删除元素的速度很快.
list 双向链表. 只支持双向顺序访问. 在list中任何位置插入和删除元素的操作都很快
forward_list 单向链表. 只支持单向顺序访问. 在链表任何位置进行插入和删除的操作都很快
array 固定大小的数组. 支持快速随机访问, 不能添加和删除元素
string 与vector相似的容器, 但专门用于保存字符. 随机快速访问速度快. 在尾部插入和删除的速度快

  string和vector将元素保存在连续的内存空间中. 由于元素是连续存储的, 由元素的下标在来计算元素的地址非常快速. 但是在这两种容器中间插入和删除元素会非常耗时. 因为一次插入和删除操作之后, 需要移动插入/删除位置之后的所有元素, 来保持连续存储. 另外, 在添加一个元素时, 有时还可能需要额外分配内存, 这是可能需要将所有的元素移动到新的内存空间中.
  list和forward_list两个容器的设计目的是令容器的插入删除操作都很快. 但是, 作为代价, 它们不支持随机访问. 并且由于这两个容器需要额外的指针域, 因此与vector, deque, array相比, 内存利用率较低.
  deque是一种更加复杂的数据结构. 与string和vector类似, deque支持快速随机访问. 与string和vector一样, 在deque的中间位置添加/删除元素代价(可能)很高, 但是在两端进行插入和删除都是很快的, 速度与lisy和forward_lis添加和删除元素速度相当.

文件输入输出

Posted on 2019-12-29 | In C++ |

  头文件fstream定义了三个类型来支持文件IO:ifstream从一个给定的文件读取数据,ofstream向一个给定文件写入数据,以及fstream可以读写给定文件。

表1:fstream特有操作

fstream strm; 创建一个未绑定的文件流。fstream是头文件中定义的一个类型
fstream fstrm(s); 创建一个fstream,并打开名为s的文件。s可以是string类型,或者是一个指向C风格字符串类型的指针。这些构造函数都是explicit的。默认的文件模式依赖于fstream的模式。
fstream fstrm(s, mode); 与前一个构造函数类似,都是以mode模式打开文件
fstrm.open(s) 打开一个名为s的文件,并与fstrm绑定。s可以是一个string或者一个指向C风格字符串的指针。默认的文件模式依赖于fstream的类型
fstrm.close() 关闭与fstrm绑定的文件。返回void
fstrm.is_open() 返回一个bool值,指出与fstrm关联的文件是否成功打开且未关闭

使用文件流对象

  每个文件流类都定义了一个open成员函数. 在创建文件流对象时, 可以直接提供文件名(可选), 当提供了文件名时, 会自动调用open().

1
2
ifstream in(ifile); //构造一个ifstream类型的流对象并打开文件
ofstream out; //构造一个输出文件流, 但是未与任何文件关联

  在新的C++标准中, 文件名可以是库类型string对象, 也可以是C风格字符数组. 旧版本的标准库只允许是C风格字符数组.

Read more »

IO 类

Posted on 2019-12-26 | In C++ |

C++ IO类

表1

头文件 类型
iostream istream, wistream 从流中读取数据
ostream, wostream 向流中写入数据
iostream, wiostream 读写流
fstream ifstream, wifstream 从文件读取数据
ofstream, wofstrkeam 向文件写数据
fstream, wfstream 读写文件
sstream istringstream, wistringstream 从string中读取数据
ostringstream, wostringstream 向string中写入数据
tringstream, wstringstream 读写string

  iostream定义了用于读写流的基本类型,fstream定义了读写命名文件的类型, sstream定义了读写内存的类型
宽字符版本的类型和函数都以一个w开始。宽字符版本的类型和对象与其对应的普通char版本的类型定义在同一个头文件中。
IO类型间的关系
  类型ifstream和istringstream都继承自istream。因此,可以像使用istream一样使用ifstream和istringstream。
同样,类型ofstream和ostringstream都继承自istream。

Read more »

STL allcator

Posted on 2019-12-26 | In STL源码剖析 |

空间配置器标准接口

1
2
3
4
5
6
7
8
allocator::value_type
allocator::pointer
allocator::const_pointer
allocator::reference
allocator::const_reference
allocator::size_type
allocator::difference_type
allocator::rebind // class rebind<U>拥有唯一成员other;是一个typedef,代表allocator<U>
Read more »

leetcode 242.Valid Anagram

Posted on 2019-12-25 | In leetcode |

242. Valid Anagram

Given two strings s and t , write a function to determine if t is an anagram of s.

Example 1:

Input: s = “anagram”, t = “nagaram”
Output: true

Example 2:

Input: s = “rat”, t = “car”
Output: false

Note:
You may assume the string contains only lowercase alphabets.

Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?

Read more »
12
TomCat代码小屋

TomCat代码小屋

18 posts
7 categories
33 tags
0%
© 2020 TomCat代码小屋
Powered by Hexo
|
Theme — NexT.Gemini v5.1.4