实验平台:LeetCode
代码地址:my github
class Solution {
public int[] twoSum(int[] nums, int target) {
Map map = new HashMap<>();
int[] result = new int[2];
for (int i = 0; i < nums.length; i++) {
int subRes = target - nums[i];
if (map.containsKey(subRes)) {
result[0] = map.get(subRes);
result[1] = i;
} else {
map.put(nums[i], i);
}
}
return result;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null || l2 == null) {
return l1 == null ? l2 : l1;
}
int sum = l1.val + l2.val;
ListNode node = new ListNode(sum % 10);
node.next = addTwoNumbers(l1.next, l2.next);
if (sum >= 10) {
node.next = addTwoNumbers(new ListNode(sum / 10), node.next);
}
return node;
}
}
class Solution {
public int lengthOfLongestSubstring(String s) {
int result = 0, start = 0, end = 0;
int sLen = s.length();
HashMap map = new HashMap<>();// key存字符,value存字符所在的位置
while (start < sLen && end < sLen) {
if (map.containsKey(s.charAt(end))) {
start = Math.max(map.get(s.charAt(end)) + 1, start);// 考虑到回文等情况
}
map.put(s.charAt(end), end);
result = Math.max(result, end - start + 1);
end++;
}
return result;
}
}
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int length = nums1.length + nums2.length;
int[] array = new int[length];
int i = 0, j = 0, k = 0;
while (i < nums1.length && j < nums2.length) {
if (nums1[i] <= nums2[j]) {
array[k++] = nums1[i++];
} else {
array[k++] = nums2[j++];
}
}
while (i < nums1.length) {
array[k++] = nums1[i++];
}
while (j < nums2.length) {
array[k++] = nums2[j++];
}
int middle = length / 2;
if ((length & 1) == 0) {
return (array[middle - 1] + array[middle]) / 2.0;
} else {
return array[middle];
}
}
}
class Solution {
public String longestPalindrome(String s) {
int beginIndex = 0, endIndes = 0;
for (int i = 0; i < s.length(); i++) {
int len = Math.max(getLength(i, i, s), getLength(i, i + 1, s));
if (len >= endIndes - beginIndex + 1) {
beginIndex = i - (len - 1) / 2;
endIndes = i + len / 2 + 1;
}
}
return s.substring(beginIndex, endIndes);
}
public int getLength(int left, int right, String s) {
int l = left, r = right;
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
l--;
r++;
}
return r - l - 1;
}
}
class Solution {
public String convert(String s, int numRows) {
if (numRows <= 1) {
return s;
}
StringBuilder sb = new StringBuilder();
int margin = 2 * numRows - 2;
int strLen = s.length();
for (int i = 0; i < numRows; i++) { // 行数
for (int j = 0; i + j * margin < strLen; j++) {
sb.append(s.charAt(i + j * margin));
if (i > 0 && i < numRows - 1 && (j + 1) * margin - i < strLen) {// 当行数在0和(numRows-1)之间时
sb.append(s.charAt((j + 1) * margin - i));
}
}
}
return sb.toString();
}
}
给定一个 32 位有符号整数,将整数中的数字进行反转。
class Solution {
public int reverse(int x) {
int absNum = Math.abs(x);
long result = 0;
while (absNum > 0) {
result = result * 10 + absNum % 10;
absNum /= 10;
}
if ((x > 0 && result > Integer.MAX_VALUE) || (x < 0 && result * -1 < Integer.MIN_VALUE)) {
return 0;
} else if (x > 0) {
return (int) result;
} else {
return (int) (-1 * result);
}
}
}
class Solution {
public int myAtoi(String str) {
str = str.trim(); // 去除首尾空格
int strLen = str.length();
int flag = 1;// flag取值为1、-1,代表符号
int index = 0;
long res = 0;
if (strLen == 0) {
return 0;
}
char beginChar = str.charAt(0);
if (beginChar != '+' && beginChar != '-' && (beginChar < '0' || beginChar > '9')) {
return 0;
} else if (beginChar == '+') {
index += 1;
} else if (beginChar == '-') {
flag = -1;
index += 1;
}
for (int i = index; i < strLen; i++) {
char curChar = str.charAt(i);
if (curChar >= '0' && curChar <= '9') {
res = res * 10 + curChar - 48;
if (flag > 0 && res > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
} else if (flag < 0 && flag * res < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
} else {
return (int) (flag * res);
}
}
return (int) (flag * res);
}
}
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
class Solution {
public boolean isPalindrome(int x) {
if (x < 0 || (x != 0 && x % 10 == 0)) {
return false;
}
int reverseNum = 0;
while (reverseNum < x) {
reverseNum = reverseNum * 10 + x % 10;
x /= 10;
}
return reverseNum == x || reverseNum / 10 == x;
}
}
class Solution {
public boolean isMatch(String s, String p) {
if (s == null || p == null) {
return false;
} else if (s.length() == 0 && p.length() == 0) {
return true;
} else {
char[] sArray = s.toCharArray();
char[] pArray = p.toCharArray();
return isMatchCore(sArray, pArray, 0, 0);
}
}
public boolean isMatchCore(char[] s, char[] p, int sIndex, int pIndex) {
if (sIndex == s.length && pIndex == p.length) {
return true;
}
if (sIndex != s.length && pIndex >= p.length) {
return false;
}
if (pIndex + 1 < p.length && p[pIndex + 1] == '*') {
if (sIndex < s.length && (s[sIndex] == p[pIndex] || p[pIndex] == '.')) {
return isMatchCore(s, p, sIndex + 1, pIndex) || isMatchCore(s, p, sIndex, pIndex + 2);
} else {
return isMatchCore(s, p, sIndex, pIndex + 2);
}
} else if (sIndex < s.length && pIndex < p.length && (s[sIndex] == p[pIndex] || p[pIndex] == '.')) {
return isMatchCore(s, p, ++sIndex, ++pIndex);
}
return false;
}
}