完成了我们的专题1——树 部分的刷题练习之后 我们(终于!)来到了第二部分:数组与字符串
经历了专题1大量题目洗礼过后的我们 应该变得对刷题更有自信了!(没看过专题1的内容不妨回去看一眼~)那么 我们继续!
【1】先对
LeetBook
中的内容进行一个学习
数组
是数据结构中的基本模块之一。- 因为
字符串
是由字符数组形成的,所以二者是相似的。
——我们面临的大多数面试问题都属于这个范畴。要刷的题目涉及如下专题——
(1)专题1 理解数组的基本概念
及其操作方式
;
(2)专题2 理解二维数组
的基本概念,熟悉二维数组的使用;
(3)专题3 了解字符串
的概念以及字符串所具有的不同特性;理解字符串匹配中的KMP 算法
;
(4)专题4 能够运用双指针
解决实际问题。
【2】在解决
LeetBook
中推荐的题目的过程中 我发现了非常有趣的两个专题——
(1)专题5 前缀和思想求解子数组&子串问题
(2)专题6 二分查找数组中元素问题
字符串就是 由字符构成的数组 嘛~
维基百科:字符串是由零个或多个字符组成的有限序列。一般记为 s = a1a2…an。它是编程语言中表示文本的数据类型。
本章将深入研究字符串 可以让我们掌握——
- 熟悉字符串中的
基本操作
,尤其是在数组中没有的独特操作;- 理解不同
比较
函数之间的区别;- 理解字符串
是否可变
以及导致连接过程中出现的问题;- 能够解决与字符串相关的基本问题,如排序、子串、字符串匹配等。
下面来做个字符串的简介嗷~先掌握了概念再去刷题!
参考LeetBook
中的内容 字符串简介
为何我们要单独讨论字符串的类型~
字符串和数组其实是有很多相似之处的~
最基础的 使用 字符串名数组名[下标索引]得到元素(数组元素/字符)
然而,存在这样一个问题:
我们可以用 “==” 来比较两个字符串吗?
这取决于下面这个问题的答案:
我们使用的语言是否支持运算符重载?
- C++ Python 中可以 用“==” 来比较两个字符串
- Java 中不可以 用“==” 来比较两个字符串
给出两个例子 对cpp
java
中结果进行比较
#include <iostream>
int main() {
string s1 = "Hello World";
cout << "s1 is \"Hello World\"" << endl;
string s2 = s1;
cout << "s2 is initialized by s1" << endl;
string s3(s1);
cout << "s3 is initialized by s1" << endl;
// compare by '=='
cout << "Compared by '==':" << endl;
cout << "s1 and \"Hello World\": " << (s1 == "Hello World") << endl;
cout << "s1 and s2: " << (s1 == s2) << endl;
cout << "s1 and s3: " << (s1 == s3) << endl;
// compare by 'compare'
cout << "Compared by 'compare':" << endl;
cout << "s1 and \"Hello World\": " << !s1.compare("Hello World") << endl;
cout << "s1 and s2: " << !s1.compare(s2) << endl;
cout << "s1 and s3: " << !s1.compare(s3) << endl;
}
// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
// initialize
String s1 = "Hello World";
System.out.println("s1 is \"" + s1 + "\"");
String s2 = s1;
System.out.println("s2 is another reference to s1.");
String s3 = new String(s1);
System.out.println("s3 is a copy of s1.");
// compare using '=='
System.out.println("Compared by '==':");
// true since string is immutable and s1 is binded to "Hello World"
System.out.println("s1 and \"Hello World\": " + (s1 == "Hello World"));
// true since s1 and s2 is the reference of the same object
System.out.println("s1 and s2: " + (s1 == s2));
// false since s3 is refered to another new object
System.out.println("s1 and s3: " + (s1 == s3));
// compare using 'equals'
System.out.println("Compared by 'equals':");
System.out.println(