刷题随笔Java
1.两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> hash=new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++){
if(hash.containsKey(target-nums[i])){
return new int[]{hash.get(target-nums[i]),i};
}
hash.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 head=null,tail=null;
int carry=0;
while(l1 != null || l2 != null){
int n1=l1!=null ? l1.val:0;
int n2=l2!=null ? l2.val:0;
int sum=n1+n2+carry;
if(head==null){
head=tail=new ListNode(sum%10);
}
else{
tail.next = new ListNode(sum % 10);
tail = tail.next;
}
carry=sum/10;
if(l1!=null)
l1=l1.next;
if(l2!=null)
l2=l2.next;
}
if(carry>0)
tail.next=new ListNode(carry);
return head;
}
}
3.无重复的最长子串
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character,Integer> window=new HashMap<Character,Integer>();
int left=0;
int right=0;
int res=0;
while(right<s.length()){
char c=s.charAt(right);
int count=window.getOrDefault(c, 0) + 1;
window.put(c, count);
right++;
while(window.get(c)>1){
char d=s.charAt(left);
int count2=window.getOrDefault(d, 0) - 1;
window.put(d, count2);
left++;
}
res = Math.max(res, right - left);
}
return res;
}
}
5.最长回文子串
class Solution {
public String longestPalindrome(String s) {
int n=s.length();
String res="";
for(int i=0;i<n;i++){
String s1 = pallhan(s, i, i);
String s2 = pallhan(s, i, i + 1);
res=res.length()>s1.length() ? res: s1;
res=res.length()>s2.length() ? res: s2;
}
return res;
}
//定义子函数
public String pallhan(String s, int l, int r){
while(l>=0 && r<s.length() && s.charAt(l)==s.charAt(r)){
l--;
r++;
}
//Java中:s.substring(l+1, r) 函数返回一个字符串,该字符串是从原字符串的第 l+1 个字符开始,到第 r-1 个字符为止
//C++中:s.substr(l+1,r-l-1) 函数返回一个字符串,该字符串从原字符串的第 l+1 个字符开始,长度r-l-1
return s.substring(l+1,r);
}
}
#7.整数反转
class Solution {
public int reverse(int x) {
long r = 0;
while (x != 0) {
r = r * 10 + x % 10;
x /= 10;
}
if (Math.abs(r) > Integer.MAX_VALUE) return 0;
return (int) r;
}
}
#9.回文数
class Solution {
public boolean isPalindrome(int x) {
String s=Integer.toString(x);
int n=s.length();
int left=0,right=n-1;
while(left<=right){
if(s.charAt(left)==s.charAt(right)){
left++;
right--;
}
else{
return false;
}
}
return true;
}
}
#14.最长公共前缀
class Solution {
public String longestCommonPrefix(String[] strs) {
int n=strs.length;
if(n<=1) return strs[0];
//遍历一次数组,留下最长公共前缀
String prefix=strs[0];
for(int i=1;i<n;i++){
prefix=maxpre(prefix,strs[i]);
}
return prefix;
}
//定义子函数,用于判断输入两个字符串的最大公共前缀
String maxpre(String a, String b){
String s="";
int length=Math.min(a.length(),b.length());
if(length==0) return s;
for (int i=0;i<length;i++){
if(a.charAt(i)==b.charAt(i)){
s+=a.charAt(i);
}
else{
return s;
}
}
return s;
}
}
15.三数之和
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int n=nums.length;
Arrays.sort(nums);
List<List<Integer>> ans=new ArrayList<>();
for(int first=0;first<n-2;first++){
//对首个元素去重
if(first>0 && nums[first]==nums[first-1])
continue;
//对后两个元素进行双指针环节
int second=first+1;
int third=n-1;
int target=-nums[first];
while(second<third){
//case1: 正好等于情况
if(nums[second]+nums[third]==target){
ans.add(Arrays.asList(nums[first],nums[second],nums[third]));
while(second<third && nums[second]==nums[++second]);
while(second<third && nums[third]==nums[--third]);
}
//case2: 相加比目标值小,需要增加second
if(nums[second]+nums[third]<target){
while(second<third && nums[second]==nums[++second]);
}
//case3: 相加比目标值大,需要减少third
if(nums[second]+nums[third]>target){
while(second<third && nums[third]==nums[--third]);
}
}
}
return ans;
}
}
16.最接近的三数之和
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int n=nums.length;
//作为参考值的迭代
int res=Integer.MAX_VALUE-target;
for(int first=0;first<n-2;first++){
int second=first+1;
int third=n-1;
while(second<third){
int sum=nums[first]+nums[second]+nums[third];
//Attention:需要res的绝对值接近0,就是sum负数的时候接近0,l++;以及sum正数的时接近0,r--
//case1:总数正好等于target,绝对值肯定是0;
if(sum==target) return sum;
//case2:总数比目标值大的情况,相减肯定是正数;需要r--
else if(sum>target){
if(Math.abs(sum-target)<Math.abs(res-target))
res=sum;
third--;
}
//case2:总数比目标值小的情况,相减肯定是负数;需要l++
else{
if(Math.abs(sum-target)<Math.abs(res-target))
res=sum;
second++;
}
}
}
return res;
}
}
19.删除链表倒数第n个节点
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
//链表的名字只是过客;内存还是在这里
if(head==null)
return head;
ListNode dummy=new ListNode(0);
//dummy:记录下head的头节点,作为标记
dummy.next=head;
ListNode cur=dummy;
int length=0;
//记录head的长度
while(head!=null){
length++;
head=head.next;
}
//cur节点循环操作到点
for(int i=1;i<=length-n;i++){
cur=cur.next;
}
cur.next=cur.next.next;
//返回头节点
ListNode ans=dummy.next;
return ans;
}
}