Java 的 String 类基本用法介绍:http://www.runoob.com/java/java-string.html

Java 的 String.substring 函数:https://beginnersbook.com/2013/12/java-string-substring-method-example/

Google coding style:https://google.github.io/styleguide/javaguide.html

String 不支持下标索引的方式访问,所以需要使用charAt(i)的方式访问对应位置的字符。同时也就没有办法使用下标的方式对String进行修改。

String是一种不可变类,字符串一但生成就不能被改变。例如我们使用**‘+’进行字符串连接,会产生新的字符串,原串不会发生任何变化;使用replace()** 进行替换某些字符的时候也是产生新的字符串,不会更改原有字符串。

https://leetcode.com/problems/valid-palindrome/submissions/

 class Solution {
public boolean isValid(char c){
return (Character.isLetter(c) || Character.isDigit(c));
} public boolean isPalindrome(String Ss) {
String s=Ss.toLowerCase();
int sl=s.length();
int xl=0, xr=sl-1;
boolean res=true;
while(xl<xr){
while(xl<sl && (!isValid(s.charAt(xl)))) xl+=1;
while(xr>=0 && (!isValid(s.charAt(xr)))) xr-=1;
if(xr<0 || xl>=sl)
break;
if(s.charAt(xl)!=s.charAt(xr)){
res=false;
break;
}
xl+=1;
xr-=1;
}
return(res);
}
}

Rabin-Karp Algorithm:在字符串中找子串

算法原理:

Ref: https://www.jianshu.com/p/68cbe955103e

https://leetcode.com/problems/implement-strstr/

 class Solution {
public int MOD=1000000;
public int INC=31;
public int strStr(String src, String dsc) {
if(src==null || dsc==null)
return -1;
int dl=dsc.length(), sl=src.length();
if(dl==0)
return 0; int power=1;
for(int i=0;i<dl;i++)
power=(power*INC)%MOD; int dschash=0;
for(int i=0;i<dl;i++)
dschash = (dschash*INC + dsc.charAt(i)) % MOD; //hash code of dsc int srchash=0;
for(int i=0;i<sl;i++){
srchash = (srchash*INC + src.charAt(i)) % MOD; //calculate hash of src
if(i<dl-1)
continue;
else if(i>=dl){
srchash = srchash - (src.charAt(i-dl) * power) % MOD;
while(srchash<0)
srchash += MOD;
}
if(srchash == dschash){ //hash(src[i-m+1..i]) == hash(dsc)
if(src.substring(i-dl+1, i+1).equals(dsc))
return(i-dl+1);
}
} return(-1);
}
}

子数组与前缀和

https://leetcode.com/problems/maximum-subarray/

使用前缀和,令p[i]=sum{ A[0]...A[i-1] }(p[0]=0,这个是为了方便后面计算用),那么就有sum{A[i]...A[j]} = p[j+1]-p[i]

设f[j]表示所有以A[j]结尾的子数组中最大的subarray,那么就有f[j] = p[j+1] - min{ p[0]...p[j] }

单纯的计算某个f[j]的时间复杂度是O(N)的,但其实计算好f[j]后,可以用O(1)的时间再计算出f[j+1]。所以整体的时间复杂度仍然是O(N)的。

 class Solution {
public int maxSubArray(int[] nums) {
int nl=nums.length;
int[] prefix=new int[nl+1];
prefix[0]=0;
for(int i=1;i<nl+1;i++)
prefix[i]=prefix[i-1]+nums[i-1]; int[] f=new int[nl];
int minp=0; for(int i=0;i<nl;i++){
minp=Math.min(minp, prefix[i]);
f[i]=prefix[i+1]-minp;
} int ans=f[0];
for(int i=1;i<nl;i++)
ans=Math.max(ans, f[i]);
return(ans);
}
}

https://www.lintcode.com/problem/subarray-sum/description

Q:找和为0的subarray

A:求出prefix[],用hashmap找里面相等的元素。O(N)

https://www.lintcode.com/problem/subarray-sum-closest/description

Q:找和最接近0的subarray

A:求出prefix[],找prefix[]中最接近的两个元素(排序)。 O(NlogN)

其他

UNDER CONSTRUCTION

https://leetcode.com/problems/subsets/

https://leetcode.com/problems/subsets-ii/

Leetcode Lect1 String相关题目的更多相关文章

  1. [LeetCode] 链表反转相关题目

    暂时接触到LeetCode上与链表反转相关的题目一共有3道,在这篇博文里面总结一下.首先要讲一下我一开始思考的误区:链表的反转,不是改变节点的位置,而是改变每一个节点next指针的指向. 下面直接看看 ...

  2. leetcode 单链表相关题目汇总

      leetcode-19-Remove Nth From End of List—移除链表中倒数第n个元素 leetcode-21-Merge Two Sorted Lists—两个已排序链表归并 ...

  3. [leetcode] 根据String数组构造TreeNode,用于LeetCode树结构相关的测试用例

    LeetCode 跟树结构相关的题目的测试用例中大多是通过String数组来构造树.例如{2,#,3,#,4,#,5,#,6},可以构造出如下的树(将树结构逆时针选择90度显示): 6         ...

  4. LeetCode: Palindrome 回文相关题目

    LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...

  5. leetcode tree相关题目总结

    leetcode tree相关题目小结 所使用的方法不外乎递归,DFS,BFS. 1. 题100 Same Tree Given two binary trees, write a function ...

  6. LeetCode All in One 题目讲解汇总(转...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...

  7. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  8. [LeetCode] Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  9. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

随机推荐

  1. join 按两个文件的相同字段合并

    1.命令功能 join对每一对具相同内容的输入行,合并为一行输出.默认情况是把输入的第一个字段作为连接字段,字段间用空格隔开. 2.语法格式 join  option  file1  file2 jo ...

  2. 解析 Java 反射题中一个有趣的坑

    public class Test { public void age(int age) { System.out.println("int age="+age); } publi ...

  3. MYSQL数据库类型与JAVA类型对应表

    MYSQL数据库类型与JAVA类型对应表   MYSQL数据库类型与JAVA类型对应表 类型名称 显示长度 数据库类型 JAVA类型 JDBC类型 索引(int) VARCHAR L+N VARCHA ...

  4. man hdparm

    HDPARM(8)                                                            HDPARM(8) NAME       hdparm - 获 ...

  5. Python---基础---爱因斯坦阶梯问题

    写一个程序,打印出0-100所有奇数 ls = range(0, 101)for i in ls: if i % 2 == 1: print(i)--------------------------- ...

  6. linux-Centos 7下bond与vlan技术的结合[推荐]

    https://blog.51cto.com/sf1314/2073519 服务器eth0与eth1作bonding,捆绑成bond0接口,服务器对端交换机端口,同属于100.101号vlan接口 v ...

  7. php怎么获取js的变量值

    使用php做网站的时候,经常需要我们与前端的页面进行交互,有时候我们还需要通过php来获得js变量中的值,这种情况我们可以通过在其中嵌入js代码的方式来获得这个变量. 首先我们创建一个test的php ...

  8. luogu P1307 数字反转 x

    题目描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2). 输入输出格式 输入格式: 输入 ...

  9. 本页面用来演示如何通过JS SDK,创建完整的QQ登录流程,并调用openapi接口

    QQ登录将用户信息存储在cookie中,命名为__qc__k ,请不要占用 __qc__k : 1) :: 在页面顶部引入JS SDK库: 将“js?”后面的appid参数(示例代码中的:100229 ...

  10. ward's method分层聚类凝聚法

    ward's method是分层聚类凝聚法的一种常见的度量cluster之间距离的方法,其基本过程是这样的(参考:http://blog.sciencenet.cn/blog-2827057-9217 ...