String解题方法小结

LeetCode上的相关题目(easy)

9, 14, 20, 28, 58, 66, 73,

125, 168, 171, 186, 189,

205,242, 246, 266, 290, 293,

344, 345, 383, 387, 389,

408, 409, 412, 415, 422, 434, 438, 459

520, 551, 557, 559

常用方法:

  • String.substring(start,end)
  • Integer.toString()
  • Character.isLetterOrDigit()
  • Character.toLowerCase()
  • Character.isDigit()
  • map.containsValue()
  • map.containsKey()
  • Integer.parseInt(abbr.charAt(i)+””);  // 碰到char转换成数字的
  • ArrayList.clear()
  • list.toArray(new String[0]);
  • toCharArray()

技巧:

  • 碰到需要计数的,使用int[], array[s.charAt(i) – ‘a’],大写的话用大A(这个特别好用)
  • 碰到对应关系的,用hashmap互相存着对应关系,如Isomorphic Strings
  • palindrome什么的直接用hashset存,最后确认hashset的大小(1和0是对的)
  • 碰到从中间倒置的,可以考虑倒置 -> 处理 -> 再倒置
  • 遍历的,能用双指针就用双指针
  • 需要对char进行置换操作的,都先变成array

注意事项:

  1. 如果需要调换顺序,要使用char[] s1 = s.toCharArray();,将string变换成char的数组,对数组进行操作
  2. string使用length(),array使用length,ArrayList使用size()
  3. 使用while的时候,里面的while必须加上最外层的条件

需要review的特殊题目:

https://blog.jing.do/4642

https://blog.jing.do/4636

https://blog.jing.do/4705

https://blog.jing.do/4878

https://blog.jing.do/4858

 

This site Original article All followed" Attribution—NonCommercial—ShareAlike 4.0 (CC BY-NC-SA 4.0) ”。 Please keep the following marks for sharing and interpretation:

Original author: Jake Tao Source: 「String解题方法小结」

Praise 161
0 0 161

Further reading

Post a reply

Log in can only be commented on later
Share this page
Back to top