6 将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。
比如输入字符串为 “PAYPALISHIRING” 行数为 3 时,排列如下:
示例 1:
输入:s = “PAYPALISHIRING”, numRows = 3
输出:“PAHNAPLSIIGYIR”
示例 2:
输入:s = “PAYPALISHIRING”, numRows = 4
输出:“PINALSIGYAHRPI”
解释:
示例 3:
输入:s = “A”, numRows = 1
输出:“A”
package leetcode.lc006;
class Solution {
public String convert(String s, int numRows) {
int len = s.length();
if (len == 1) {
return s;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numRows; i++) {
// 针对收尾行按规律拼接
if (i == 0 || i == numRows - 1) {
int k = 0;
int index = i + 2 * k * (numRows - 1);
while (index < len) {
sb.append(s.charAt(index));
k++;
index = i + 2 * k * (numRows - 1);
}
} else {
int k = 0;
int index = i + 2 * k * (numRows - 1);
boolean flag = true;
while (index < len) {
sb.append(s.charAt(index));
if (flag) {
index = index + 2 * (numRows - 1 - i);
k++;
flag = false;
} else {
index = i + 2 * k * (numRows - 1);
flag = true;
}
}
}
}
return sb.toString();
}
}
package leetcode.lc006;
import org.junit.Test;
public class Main {
Solution s = new Solution();
@Test
public void test01(){
String str = "PAYPALISHIRING";
for(int row=3; row<=6; row++) {
System.out.println(s.convert(str, row));
}
}
@Test
public void test02(){
String str = "A";
System.out.println(s.convert(str, 1));
}
}
7 整数反转
给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。
如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0。
假设环境不允许存储 64 位整数(有符号或无符号)。
示例 1:
输入:x = 123
输出:321
示例 2:
输入:x = -123
输出:-321
示例 3:
输入:x = 120
输出:21
示例 4:
输入:x = 0
输出:0
package leetcode.lc007;
class Solution {
public int reverse(int x) {
int flag = x > 0 ? 1 : -1;
StringBuffer sb = new StringBuffer(String.valueOf(Math.abs(x)));
sb = sb.reverse();
String tmp = sb.toString();
while (sb.indexOf("tmp") == 0) {
tmp = tmp.substring(1);
}
int result = 0;
try {
result = Integer.parseInt(tmp);
} catch (NumberFormatException nfe) {
}
return flag * result;
}
}
package leetcode.lc007;
import org.junit.Test;
public class Main {
Solution s = new Solution();
@Test
public void test01(){
int x = 123;
System.out.println(s.reverse(x));
}
@Test
public void test02(){
int y = -12300;
System.out.println(s.reverse(y));
}
@Test
public void test03(){
int y = -1534236469;
System.out.println(s.reverse(y));
}
}
8 字符串转换整数 (atoi)
请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。
函数 myAtoi(string s) 的算法如下:
读入字符串并丢弃无用的前导空格
检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。
读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。
将前面步骤读入的这些数字转换为整数(即,“123” -> 123, “0032” -> 32)。如果没有读入数字,则整数为 0 。必要时更改符号(从步骤 2 开始)。
如果整数数超过 32 位有符号整数范围 [−231, 231 − 1] ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 −231 的整数应该被固定为 −231 ,大于 231 − 1 的整数应该被固定为 231 − 1 。
返回整数作为最终结果。
注意:
本题中的空白字符只包括空格字符 ’ ’ 。
除前导空格或数字后的其余字符串外,请勿忽略 任何其他字符。
示例 1:
输入:s = “42”
输出:42
解释:加粗的字符串为已经读入的字符,插入符号是当前读取的字符。
第 1 步:“42”(当前没有读入字符,因为没有前导空格)
^
第 2 步:“42”(当前没有读入字符,因为这里不存在 ‘-’ 或者 ‘+’)
^
第 3 步:“42”(读入 “42”)
^
解析得到整数 42 。
由于 “42” 在范围 [-231, 231 - 1] 内,最终结果为 42 。
示例 2:
输入:s = " -42"
输出:-42
解释:
第 1 步:" -42"(读入前导空格,但忽视掉)
^
第 2 步:" -42"(读入 ‘-’ 字符,所以结果应该是负数)
^
第 3 步:" -42"(读入 “42”)
^
解析得到整数 -42 。
由于 “-42” 在范围 [-231, 231 - 1] 内,最终结果为 -42 。
示例 3:
输入:s = “4193 with words”
输出:4193
解释:
第 1 步:“4193 with words”(当前没有读入字符,因为没有前导空格)
^
第 2 步:“4193 with words”(当前没有读入字符,因为这里不存在 ‘-’ 或者 ‘+’)
^
第 3 步:“4193 with words”(读入 “4193”;由于下一个字符不是一个数字,所以读入停止)
^
解析得到整数 4193 。
由于 “4193” 在范围 [-231, 231 - 1] 内,最终结果为 4193 。
package leetcode.lc008;
class Solution {
public int myAtoi(String s) {
char[] chars = s.trim().toCharArray();
int flag = 1;
boolean run = true;
StringBuilder sb = new StringBuilder();
boolean hasSign = false;
for (int i = 0; i < chars.length && run; i++) {
if (chars[i] == '-') {
if (hasSign || sb.length() > 0) {
break;
}
flag = -1;
hasSign = true;
continue;
}
if (chars[i] == '+') {
if (hasSign || sb.length() > 0) {
break;
}
flag = 1;
hasSign = true;
continue;
}
if (!run && (chars[i] == ' ' || chars[i] == '0')) {
continue;
}
if ('0' <= chars[i] && chars[i] <= '9') {
sb.append(chars[i]);
} else {
run = false;
}
}
if (sb.toString().length() == 0) {
return 0;
}
int result = 0;
try {
result = Integer.parseInt(sb.toString());
} catch (NumberFormatException nfe) {
return flag > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
return flag * result;
}
}
package leetcode.lc008;
import org.junit.Test;
public class Main {
Solution s = new Solution();
@Test
public void test01() {
String str = "42";
System.out.println(s.myAtoi(str));
}
@Test
public void test02() {
String str = "-42";
System.out.println(s.myAtoi(str));
}
@Test
public void test03() {
String str = "4193 with words";
System.out.println(s.myAtoi(str));
}
@Test
public void test04() {
String str = "-91283472332";
System.out.println(s.myAtoi(str));
}
@Test
public void test05() {
String str = "words and 987";
System.out.println(s.myAtoi(str));
}
@Test
public void test06() {
String str = "+-12";
System.out.println(s.myAtoi(str));
}
}
9 回文数
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
例如,121 是回文,而 123 不是。
示例 1:
输入:x = 121
输出:true
示例 2:
输入:x = -121
输出:false
解释:从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
示例 3:
输入:x = 10
输出:false
解释:从右向左读, 为 01 。因此它不是一个回文数。
class Solution {
public int reverse(int x) {
int flag = x > 0 ? 1 : -1;
StringBuffer sb = new StringBuffer(String.valueOf(Math.abs(x)));
sb = sb.reverse();
String tmp = sb.toString();
while (sb.indexOf("tmp") == 0) {
tmp = tmp.substring(1);
}
int result = 0;
try {
result = Integer.parseInt(tmp);
} catch (NumberFormatException nfe) {
}
return flag * result;
}
public boolean isPalindrome(int x) {
if(x < 0) return false;
int y = reverse(x);
return y == x;
}
}
10 正则表达式匹配
给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 ‘.’ 和 ‘*’ 的正则表达式匹配。
‘.’ 匹配任意单个字符
‘*’ 匹配零个或多个前面的那一个元素
所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。
示例 1:
输入:s = “aa”, p = “a”
输出:false
解释:“a” 无法匹配 “aa” 整个字符串。
示例 2:
输入:s = “aa”, p = “a*”
输出:true
解释:因为 ‘*’ 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 ‘a’。因此,字符串 “aa” 可被视为 ‘a’ 重复了一次。
示例 3:
输入:s = “ab”, p = “."
输出:true
解释:".” 表示可匹配零个或多个(‘*’)任意字符(‘.’)。
class Solution {
public boolean isMatch(String s, String p) {
int len_s = s.length();
int len_p = p.length();
if (len_s == 0) {
if (isPCanEliminate(p)) {
return true;
} else {
return false;
}
}
if (len_p == 0) {
return false;
}
if (p.charAt(len_p - 1) != '*') {
if (isCharMatch(s.charAt(len_s - 1), p.charAt(len_p - 1))) {
return isMatch(s.substring(0, len_s - 1), p.substring(0, len_p - 1));
} else {
return false;
}
} else {
if (isCharMatch(s.charAt(len_s - 1), p.charAt(len_p - 2))) {
return isMatch(s.substring(0, len_s - 1), p) || isMatch(s, p.substring(0, len_p - 2));
} else {
return isMatch(s, p.substring(0, len_p - 2));
}
}
}
public boolean isPCanEliminate(String p) {
if (p.length() == 0) {
return true;
}
if (p.endsWith("*")) {
return isPCanEliminate(p.substring(0, p.length() - 2));
}
return false;
}
public boolean isCharMatch(char s_char, char p_char) {
if (s_char == p_char || p_char == '.') {
return true;
}
return false;
}
}
package leetcode.lc010;
import org.junit.Test;
public class Main {
Solution s = new Solution();
@Test
public void test01() {
String str = "a";
String p = "aa";
System.out.println(s.isMatch(str, p));
}
@Test
public void test02() {
String str = "abc";
String p = "a*b*c*";
System.out.println(s.isMatch(str, p));
}
@Test
public void test0a() {
String str = "aa";
String p = "a";
System.out.println(s.isMatch(str, p));
}
@Test
public void testSubstring() {
String str = "abcde";
System.out.println(str.substring(0, 5));
}
}