算法
VeronicaNa
好好学习,天天向上
展开
-
验证回文串
验证回文串leetcode 125class Solution { public boolean isPalindrome(String s) { int left = 0,right = s.length()-1; while(left<right){ while(left<right && !Character.isLetterOrDigit(s.charAt(left))) left++;原创 2021-04-17 00:24:22 · 127 阅读 · 0 评论 -
回文链表
leetcode 234在这里插入代码片原创 2021-03-24 14:29:02 · 116 阅读 · 0 评论 -
反转链表
leetcode 206 //迭代 public ListNode reverseList(ListNode head) { if(head == null || head.next == null) return head; ListNode pre = head,cur = head.next,nex; pre.next = null; while(cur!=null){ nex = cur.next原创 2021-03-20 02:27:30 · 85 阅读 · 0 评论 -
相交链表
leetcode 160//法1:哈希表//法2:双指针 public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if(headA == null || headB == null) return null; ListNode p = headA,q = headB; int count = 0; while(p!=q){ p原创 2021-03-20 01:53:37 · 48 阅读 · 0 评论 -
排序链表
leetcode 148 //此法适用于不能有重复元素的链表,重点看treemap遍历方法 public ListNode sortList(ListNode head) { TreeMap<Integer,ListNode> map = new TreeMap<>(); ListNode h = head; while(h!=null){ map.put(h.val,h);原创 2021-03-20 01:25:58 · 57 阅读 · 0 评论 -
环形链表
leetcode 141 //hashset public boolean hasCycle(ListNode head) { HashSet<ListNode> set = new HashSet<>(); while(head!=null){ if(set.contains(head)) return true; set.add(head); head.原创 2021-03-17 17:03:34 · 63 阅读 · 0 评论 -
最小栈
leetcode 155java实现栈: stack类底层是数组,而用linkedlist继承deque底层是链表 //用栈实现 // 数据栈 private Stack<Integer> data; // 辅助栈 private Stack<Integer> helper; /** * initialize your data structure here. */ public MinStack() {原创 2021-03-17 12:57:23 · 62 阅读 · 0 评论 -
复制带随机指针的链表
leetcode 138 //hashmap回溯 HashMap <Node,Node> map = new HashMap<>(); public Node copyRandomList(Node head) { if(head==null) return null; if(map.containsKey(head)) return map.get(head); Node node = new Node(he原创 2021-03-17 12:10:43 · 52 阅读 · 0 评论 -
两链表中数字相加
两数相加。给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。答案class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode pre = new ListNode(); ListNode cur = pre; int carry = 0; while(l1!=nu原创 2021-03-16 15:18:05 · 85 阅读 · 0 评论 -
回文串
214三种方法没看懂class Solution { public String shortestPalindrome(String s) { int i = 0; for(int j = s.length() - 1; j >= 0; j--){ if(s.charAt(j) == s.charAt(i)){ i++; } } if(i ==原创 2021-03-16 09:20:04 · 57 阅读 · 0 评论 -
除自身以外数组的乘积
除自身以外数组的乘积leetcode 238 //左右列表 public int[] productExceptSelf(int[] nums) { int[] res = new int[nums.length]; int[] left = new int[nums.length]; int[] right = new int[nums.length]; left[0]=1;right[nums.length-1]=1;原创 2021-03-15 14:22:44 · 45 阅读 · 0 评论 -
搜索二维矩阵
leetcode 240 public boolean searchMatrix(int[][] matrix, int target) { int m=matrix.length-1;//行 int n=matrix[0].length-1;//列 int i=0,j=n; while(i<=m&&j>=0){ if(target<matrix[i][j]) j--;原创 2021-03-14 13:39:19 · 50 阅读 · 0 评论 -
递增三元子序列
递增三元子序列leetcode 334思路牛逼 public boolean increasingTriplet(int[] nums) { int i=Integer.MAX_VALUE,j=Integer.MAX_VALUE; for(int num:nums){ if(i>=num) i=num; else if(j>=num) j=num; else return true原创 2021-03-14 12:14:24 · 65 阅读 · 0 评论 -
两个数组的交集
两个数组的交集leetcode 350 //哈希表 public int[] intersect(int[] nums1, int[] nums2) { if(nums1.length>nums2.length) return intersect(nums2,nums1); List<Integer> res = new ArrayList<>(); Map<Integer,Integer> map原创 2021-03-14 10:22:06 · 53 阅读 · 0 评论 -
打乱数组
打乱数组leetcode 384//暴力方法class Solution { private int[] array; private int[] num; private Random rand = new Random(); private List<Integer> getArrayCopy(){ List<Integer> asList = new ArrayList<>(); for(int原创 2021-03-13 22:57:45 · 82 阅读 · 0 评论 -
移动零
移动零leetcode 283 public void moveZeroes(int[] nums) { //双指针法,有一个指针a指向待更改元素,有一个指针b指向遍历到的元素 int a=0,b=0; while(b<nums.length){ if(nums[b]!=0) nums[a++]=nums[b]; b++; } for(int i=a;i<nu原创 2021-03-13 19:29:08 · 59 阅读 · 0 评论 -
存在重复元素
存在重复元素 //排序 public boolean containsDuplicate(int[] nums) { Arrays.sort(nums); for(int i=1;i<nums.length;i++){ if(nums[i]==nums[i-1]) return true; } return false; } //hashset public boolean原创 2021-03-13 18:01:12 · 72 阅读 · 0 评论 -
找众数
找众数找出现次数大于n/2的数找出现此数大于n/3的数找出现次数大于n/2的数leetcode 169 //排序 public int majorityElement(int[] nums) { Arrays.sort(nums); return nums[nums.length/2];//java 默认向下取整,即5/2=2 } //摩尔投票法:当一个数投票数相等时+1,不等时-1,要注意判断当-1后,票数是否为0,若为0则先把当前这个数存为待原创 2021-03-13 17:43:12 · 117 阅读 · 0 评论 -
乘积最大子数组
乘积最大子数组转自leetcode152运用动态规划 public int maxProduct(int[] nums) { int maxF=nums[0],minF=nums[0],ans=nums[0]; for(int i=1;i<nums.length;i++){ int maf=maxF,mif=minF; maxF=Math.max(maxF*nums[i],Math.max(mif*nums[i原创 2021-03-13 16:24:06 · 56 阅读 · 0 评论 -
旋转数组
旋转数组给定一个数组,将数组中的元素向右移动 k 个位置转自leetcode 189 //使用额外空间 public void rotate(int[] nums, int k) { int[] newInt = new int[nums.length]; for(int i=0;i<nums.length;i++){ newInt[(i+k)%nums.length] = nums[i]; }原创 2021-03-13 16:11:20 · 99 阅读 · 0 评论 -
链表反转相关问题
链表反转:转自这里 //递归法 public Node reverse(Node head){ if(head==null || head.next==null) return head; Node temp=head.next; Node newHead=reverse(head.next); temp.next=head; head.next=null; return newHead;原创 2021-03-13 01:01:50 · 47 阅读 · 0 评论