1
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap <>();
for(int i = 0;i < nums.length;i++){
int complement = target - nums[i];
if(map.containsKey(complement)){
return new int[]{map.get(complement),i};
}
map.put(nums[i],i);
}
return new int[0];
}
}
2
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode curr = dummy;
int carry = 0;
while(l1!=null || l2!=null){
int x = (l1!=null)?l1.val:0;
int y = (l2!=null)?l2.val:0;
int sum = x+y+carry;
carry = sum/10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if(l1!= null){
l1 = l1.next;
}
if(l2 !=null){
l2 = l2.next;
}
}
if(carry > 0){
curr.next = new ListNode(carry);
}
return dummy.next;
}
}
3
public class Solution{
public int lengthOfLongestSubstring(String s){
HashSet<Character> set = new HashSet<>();
int maxLength = 0;
int left = 0;
int right = 0;
while(right < s.length()){
char c = s.charAt(right);
if(!set.contains(c)){
set.add(c);
maxLength = Math.max(maxLength,right - left + 1);
right++;
}else{
set.remove(s.charAt(left));
left++;
}
}
return maxLength;
}
}
4
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
if (nums1.length > nums2.length) {
return findMedianSortedArrays(nums2, nums1);
}
int m = nums1.length;
int n = nums2.length;
int left = 0;
int right = m;
while (left <= right) {
int i = left + (right - left) / 2; //开始二分查找,i是nums1的分割点 ,j是num2的分割点
int j = (m + n + 1) / 2 - i;
int maxLeft1 = (i == 0) ? Integer.MIN_VALUE : nums1[i - 1];
int minRight1 = (i == m) ? Integer.MAX_VALUE : nums1[i];
int maxLeft2 = (j == 0) ? Integer.MIN_VALUE : nums2[j - 1];
int minRight2 = (j == n) ? Integer.MAX_VALUE : nums2[j];
if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {
if ((m + n) % 2 == 0) {
return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2.0;
} else {
return (double)Math.max(maxLeft1, maxLeft2);
}
} else if (maxLeft1 > minRight2) {
right = i - 1;
} else {
left = i + 1;
}
}
throw new IllegalArgumentException("Input arrays are not sorted.");
}
}
5
class Solution {
public String longestPalindrome(String s) {
//中心扩展法
if (s == null || s.length() < 1)
return "0";
int start = 0,end = 0;
for (int i = 0;i < s.length();i++){
int len1 = expandAroundCenter(s,i,i);
int len2 = expandAroundCenter(s,i,i + 1);
int len = Math.max(len1,len2);
if (len > end - start){
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substring(start,end + 1);
}
private int expandAroundCenter(String s,int left,int right){
while (left >=0 && right < s.length() && s.charAt(left) == s.charAt(right)){
left--;
right++;
}
return right - left -1;
}
}
6
class Solution {
public String convert(String s, int numRows) {
if(numRows ==1){
return s;
}
List<StringBuilder> rows = new ArrayList<>();
for (int i = 0;i < Math.min(numRows,s.length());i++){
rows.add(new StringBuilder());
}
int curRow = 0;
boolean goingDown = false;
for (char c : s.toCharArray()){
rows.get(curRow).append(c);
if (curRow == 0 || curRow == numRows -1){
goingDown = !goingDown;
}
curRow += goingDown ? 1 :-1;
}
StringBuilder result = new StringBuilder();
for (StringBuilder row : rows){
result.append(row);
}
return result.toString();
}
}
7
//整数反转
class Solution {
public int reverse(int x) {
int reversed = 0;
while (x != 0){
int digit = x % 10;
x = x / 10;
if (reversed > Integer.MAX_VALUE / 10 || (reversed == Integer.MAX_VALUE / 10 && digit > 7 )){
return 0;
}else if(reversed < Integer.MIN_VALUE / 10 ||(reversed == Integer.MIN_VALUE / 10 && digit < -8)){
return 0;
}
reversed = reversed * 10 + digit;
}
return reversed;
}
}
/**
条件 (reversed == Integer.MAX_VALUE / 10 && digit > 7) 中的 digit > 7 是针对反转后的数字的个位数进行判断的部分。
具体来说,由于 Integer.MAX_VALUE 是 32 位有符号整数的最大值,即 2^31 - 1,因此个位数的最大值应为 7。在反转一个整数的过程中,如果当前需要添加的个位数 digit 大于 7,那么在将其添加到反转的结果中时,如果 reversed 的值已经是 Integer.MAX_VALUE / 10,再加上大于 7 的个位数将会导致溢出,超出了 32 位整数的范围。
因此,条件(reversed == Integer.MAX_VALUE / 10 && digit > 7) 的含义是:如果当前 reversed 的值已经是 Integer.MAX_VALUE / 10,并且当前需要添加的个位数 digit 大于 7,那么就会发生溢出,函数应该返回 0。
*/
8
class Solution {
public int myAtoi(String s) {
int index = 0;
int sign = 1;
int result = 0;
// 去除前导空格
while (index < s.length() && s.charAt(index) == ' ') {
index++;
}
// 确定正负号
if (index < s.length() && (s.charAt(index) == '+' || s.charAt(index) == '-')) {
sign = (s.charAt(index) == '-') ? -1 : 1;
index++;
}
// 转换数字
while (index < s.length() && Character.isDigit(s.charAt(index))) {
int digit = s.charAt(index) - '0';
// 检查是否会溢出
if (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && digit > 7)) {
return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
result = result * 10 + digit;
index++;
}
return result * sign;
}
}