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/

  1. class Solution {
  2. public boolean isValid(char c){
  3. return (Character.isLetter(c) || Character.isDigit(c));
  4. }
  5.  
  6. public boolean isPalindrome(String Ss) {
  7. String s=Ss.toLowerCase();
  8. int sl=s.length();
  9. int xl=0, xr=sl-1;
  10. boolean res=true;
  11. while(xl<xr){
  12. while(xl<sl && (!isValid(s.charAt(xl)))) xl+=1;
  13. while(xr>=0 && (!isValid(s.charAt(xr)))) xr-=1;
  14. if(xr<0 || xl>=sl)
  15. break;
  16. if(s.charAt(xl)!=s.charAt(xr)){
  17. res=false;
  18. break;
  19. }
  20. xl+=1;
  21. xr-=1;
  22. }
  23. return(res);
  24. }
  25. }

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

算法原理:

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

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

  1. class Solution {
  2. public int MOD=1000000;
  3. public int INC=31;
  4. public int strStr(String src, String dsc) {
  5. if(src==null || dsc==null)
  6. return -1;
  7. int dl=dsc.length(), sl=src.length();
  8. if(dl==0)
  9. return 0;
  10.  
  11. int power=1;
  12. for(int i=0;i<dl;i++)
  13. power=(power*INC)%MOD;
  14.  
  15. int dschash=0;
  16. for(int i=0;i<dl;i++)
  17. dschash = (dschash*INC + dsc.charAt(i)) % MOD; //hash code of dsc
  18.  
  19. int srchash=0;
  20. for(int i=0;i<sl;i++){
  21. srchash = (srchash*INC + src.charAt(i)) % MOD; //calculate hash of src
  22. if(i<dl-1)
  23. continue;
  24. else if(i>=dl){
  25. srchash = srchash - (src.charAt(i-dl) * power) % MOD;
  26. while(srchash<0)
  27. srchash += MOD;
  28. }
  29. if(srchash == dschash){ //hash(src[i-m+1..i]) == hash(dsc)
  30. if(src.substring(i-dl+1, i+1).equals(dsc))
  31. return(i-dl+1);
  32. }
  33. }
  34.  
  35. return(-1);
  36. }
  37. }

子数组与前缀和

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)的。

  1. class Solution {
  2. public int maxSubArray(int[] nums) {
  3. int nl=nums.length;
  4. int[] prefix=new int[nl+1];
  5. prefix[0]=0;
  6. for(int i=1;i<nl+1;i++)
  7. prefix[i]=prefix[i-1]+nums[i-1];
  8.  
  9. int[] f=new int[nl];
  10. int minp=0;
  11.  
  12. for(int i=0;i<nl;i++){
  13. minp=Math.min(minp, prefix[i]);
  14. f[i]=prefix[i+1]-minp;
  15. }
  16.  
  17. int ans=f[0];
  18. for(int i=1;i<nl;i++)
  19. ans=Math.max(ans, f[i]);
  20. return(ans);
  21. }
  22. }

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. Elasticsearch Java Low Level REST Client(嗅探器)

    https://segmentfault.com/a/1190000016828977?utm_source=tag-newest#articleHeader0 嗅探器 允许从正在运行的Elastic ...

  2. Informatica ODBC的使用

    1.在服务器端配置odbc.ini 注意:添加环境变量才能生效 2.测试连通性 3.使用

  3. $2019$ 暑期刷题记录1:(算法竞赛DP练习)

    $ 2019 $ 暑期刷题记录: $ POJ~1952~~BUY~LOW, BUY~LOWER: $ (复杂度优化) 题目大意:统计可重序列中最长上升子序列的方案数. 题目很直接的说明了所求为 $ L ...

  4. python tkinter实时显示曲线

    from tkinter import *from tkinter import ttkimport time#画窗口root = Tk()root.geometry('1000x500')root. ...

  5. 1N4148

    摘自http://baike.baidu.com/link?url=0iTO7zZvHpCeJiZurTPpjDT95YdJu7cKdTeCWfol36b4JG5ii15leQ7K4wJWAZIBNb ...

  6. python开发之virtualenv与virtualenvwrapper

    在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...

  7. linux运维、架构之路-正则表达式

    一.通配符的含义 符号 参数说明 其他说明 | 管道 把前一个命令结果通过管道传递给后面一个命令 ; 命令的分隔符 ll /oldboy/;cat oldboy.tx . 表示当前目录 * 匹配文本或 ...

  8. asp.net+扫描仪+图片上传

    问题: IE浏览器下使用Activex插件调用客户端扫描仪扫描文件并山传,可以将纸质档案(如合同.文件.资料等)扫描并将扫描图像保存到服务器,可以用于合同管理.档案管理等. 通过插件方式调用扫描仪扫描 ...

  9. luogu P1020 导弹拦截 x

    首先上题目~ luogu P1020 导弹拦截 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都 ...

  10. 【HDOJ6651】Final Exam(贪心)

    题意:有n门课,价值之和为m,每门课的价值可能是0到m 一门价值为x的课需要花至少x+1时间准备才能通过 问不管价值如何分配都能通过至少k门课的最小总准备时间 m,n,k<=1e9 思路: #i ...