算法解题

算法解题01

新的方法

// 1.判断是否是字母和数字
Character.isLetterOrDigit(char ch);

// 2.拼接数组
System. arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
/*
src:源数组;
srcPos:源数组要复制的起始位置;
dest:目的数组;
destPos:目的数组放置的起始位置;
length:复制的长度.
*/

题目:https://leetcode-cn.com/problemset/all/?topicSlugs=two-pointers


无重复的最长字串

/*
	本题使用滑动窗口
	Map
*/
class Solution {
     
    public int lengthOfLongestSubstring(String s) {
     
		Map<Character,Integer> map = new HashMap<>();
        int res = 0;
        for(int l=0,r=0;r<s.length();r  ){
     
            if(map.containsKey(s.charAt(r))){
     
                char key = s.charAt(r);
                l = Math.max(l,map.get(key) 1);
            }
            res = Math.max(res,r-l 1);
            map.put(s.charAt(r),r);
        }
        return res; 
	}
}

删除排列数组中的重复项

//	双指针
//	快慢指针
class Solution {
     
    public int removeDuplicates(int[] nums) {
     
       int r;
       int l=0;
       for(r=1;r<nums.length;r  ){
     
           if(nums[r]!=nums[l]){
     
               l  ;
               nums[l]=nums[r];
               
           }
       } 
       return l 1;
    }
}

移除元素

//	同上
//	双指针
class Solution {
     
    public int removeElement(int[] nums, int val) {
     
        int l=0;
        int r=0;
        int n=nums.length;
        for(;r<n;){
     
            if(nums[r]!=val){
     
                nums[l]=nums[r];
                l  ;
                r  ;
            }else{
     
                r  ;
            }
        }
        return l;
    }
}

实现strStr()

//	暴力
//	双指针
class Solution {
     
    public int strStr(String haystack, String needle) {
     
       int n = haystack.length();
       int m = needle.length();
       for(int i=0;i<n-m 1;i  ){
     
           int j=0;
           for(;j<m;j  ){
     
               if(haystack.charAt(i j) != needle.charAt(j)){
     
                   break;
               }
           }
           if(j==m)
            return i;
       }
       return -1;
    }
}

合并两个有序数组

//	没有灵魂
class Solution {
     
    public void merge(int[] nums1, int m, int[] nums2, int n) {
     
        for(int i=0;m<nums1.length;m  ,i  ){
     
            nums1[m] = nums2[i];
        }
        Arrays.sort(nums1);

    }
}

验证回文串

class Solution {
     
    public boolean isPalindrome(String s) {
     
         if (s == null || "".equals(s)) {
     
            return true;
        }
        
        int a = 0, b = s.length() - 1;
        
        while (a < b) {
     
            if (!Character.isLetterOrDigit(s.charAt(a))) {
     
                a  ;
                continue;
            }
            
            if (!Character.isLetterOrDigit(s.charAt(b))) {
     
                b--;
                continue;
            }
            
            if (Character.toLowerCase(s.charAt(a)) != Character.toLowerCase(s.charAt(b))) {
     
                return false;
            }
            
            a  ;
            b--;
        }
        return true;
    }
}

环形链表

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
     
    public boolean hasCycle(ListNode head) {
     
        Set<ListNode> set = new HashSet<>();
        if(head==null){
     
            return false;
        }
        while(head.next!=null){
     
            if(set.contains(head)){
     
                return true;
            }else{
     
                set.add(head);
            }
            head = head.next;
        }
        return false;
    }
}

题目

  • 力扣3无重复的最长字串
  • 力扣26删除排列数组中的重复项
  • 力扣27移除元素
  • 力扣28实现strStr()
  • 力扣88合并两个有序数组
  • 力扣125验证回文串
  • 力扣141环形链表

你可能感兴趣的:(算法入门,java,指针,算法)