LeetCode Top Interview Questions Easy Collections 总结二(String)

Reverse String

Write a function that takes a string as input and returns the string reversed.

要求:反转字符串
分析:建立一个新字符串从末尾拼接

Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.
Note:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

要求: 反转一个int, 若溢出则返回0
分析: 用sum = sum * 10 + num%10; num/= 10 这样的方法来承接到倒数一位, 因为int范围是2147483647 即MAX_INTEGER, 所以只需判断sum 和 214748364 即 MAX_INTEGER / 10的大小, 最后一位不用比的原因是 给定的int值第一位是不可能大于2的. 最后还要注意int为负数的情况, 需要提前提取出负号

First Unique Character in a String

Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.
Note: You may assume the string contain only lowercase letters.

要求: 给定一个只有小写的字符串, 找出第一个没有重复的字符的下标, 若没有返回-1
分析: 建立char数组,长度最大字符的种类, 遍历字符串, 找到一个就char[index]++. 之后再遍历一次, 找到第一个char[index]即可

Valid Anagram

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

For example,
s = “anagram”, t = “nagaram”, return true.
s = “rat”, t = “car”, return false.

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

要求: 判断s和t是否组成相同(但是内部字符的顺序不同)
分析:hashmap存入字符和个数, 然后再remove 看最后是否为空

Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
“A man, a plan, a canal: Panama” is a palindrome.
“race a car” is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

要求: 判断给定的string是否为回文
分析: 首先看例子, 需要排除句子内部除了字母外的所有字符, 再进行对比无视大小写, 另外空字符串也是符合题意的

String to Integer (atoi)

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

要求: 转换string为integer
分析: 巨坑的一道题, 首先要判断输入的这个数字是否合法! 开头不是+, -或者数字就非法, 而且要是有+, 或者-也只能有1个,之后才进行读取数字, 读到不是数字为止. 另外若开头为0, 注意忽略.

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:

Input: haystack = “hello”, needle = “ll”
Output: 2

要求: 返回needle再haystack中第一次出现时的下标 若没有 返回-1
分析: Sting的indexof方法, 或者自己写一个indexof方法

Count and Say

The count-and-say sequence is the sequence of integers with the first five terms as following:

  1. 1
  2. 11
  3. 21
  4. 1211
  5. 111221
    1 is read off as “one 1” or 11.
    11 is read off as “two 1s” or 21.
    21 is read off as “one 2, then one 1” or 1211.
    Given an integer n, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

要求: 下一行读前一行, 输出第N行的数字
分析: 就是如题所示, 找上一行的数, 然后计数来得到这一行的数, 注意点是用stringbuilder来减少运行时间

Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

要求: 找string数组中, 最长的前缀
分析: 长度1以内直接返回, 之后先比对0和1得到一个公共前缀, 之后拿这个前缀和之后的每一个比, 这个前缀会慢慢缩小,直到这个前缀长度为0 或者比完, 返回这个前缀, 同样要注意用stringbuilder

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值