Algorithm
liujunzhong
这个作者很懒,什么都没留下…
展开
-
remove duplicates from Soted ListNode
/** * 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; } * }.原创 2020-09-15 20:09:15 · 140 阅读 · 0 评论 -
合并k个有序链表
/** * 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; } * }.原创 2020-08-31 21:41:36 · 183 阅读 · 0 评论 -
求数组中最大差值
/** * 求最大差值,要求必须是下标小的减下标大的 */public class MaxDiffer { public static void main(String[] args) { int[] nums = {7,1,5,3,6,4}; maxDiffer(nums); } public static void maxDiffer(int[] nums){ int preMax = Integer.MIN_VALUE...原创 2020-08-31 19:48:09 · 1638 阅读 · 1 评论 -
combination sum 3
import java.util.ArrayList;import java.util.List;/** * 给定一个数字x,要求使用k个数字求和可以得到x,数字从1-9中选择,不能重复。 * * 实现一个函数,在输入 x,k 时,输出所有可以求和得到x的列表 * * 例子: * * 输入:k= 3, x = 9 * * 输出: [[1,2,6], [1,3,5], [2,3,4]] */public class CombinationSum3 { public st.原创 2020-08-31 16:08:55 · 151 阅读 · 0 评论 -
最长公共字符串
给定两个字符串,返回两个字符串最长公共字符串package com.ncu.dp;public class MaxCommonString { public static void main(String[] args) { String a = "12ddd"; String b = "12hhh"; helper(a,b); } public static void helper(String a,String b){原创 2020-08-31 14:58:25 · 302 阅读 · 0 评论 -
Candy
ratings 数组中的值代表每个孩子的得分,给定如下规则每个孩子最少得到一个糖果相邻孩子之间,得分高的人得到的糖果比得分低的人得到的糖果数要多class Solution { public int candy(int[] ratings) { int[] nums = new int[ratings.length]; Arrays.fill(nums,1); for(int i=1;i<ratings.length;i++..原创 2020-08-29 15:20:24 · 84 阅读 · 0 评论 -
next greater element 3
给定一个数,求下一个包含所有该数字的数例如:12--->21若没有,则返回-1,如321class Solution { public int nextGreaterElement(int n) { char[] chars = String.valueOf(n).toCharArray(); int i=chars.length-1; for(;i>0;i--){ if(chars[i]>.原创 2020-08-29 11:40:41 · 76 阅读 · 0 评论 -
按字典序打印前N个数字
class Solution { public List<Integer> lexicalOrder(int n) { List<Integer> list = new ArrayList<>(); for(int i=1;i<10;i++){ dfs(i,n,list); } return list; } void dfs(int cur, int n.原创 2020-08-08 08:30:10 · 673 阅读 · 0 评论 -
longest consecutive Sequence
// 用一个HashMap记录每个元素的最长子序列长度class Solution { public int longestConsecutive(int[] nums) { Map<Integer,Integer> map = new HashMap<>(); int maxLen = 0; for(int num:nums){ if(map.containsKey(num)) continue;.原创 2020-07-28 09:17:19 · 73 阅读 · 0 评论 -
堆排序Java实现
package com.ncu.array;import java.util.Arrays;public class HeapSort { public static void main(String[] args) { int[] nums = {1,4,2,3,5,8,6,9,7}; headSort(nums); System.out.println(Arrays.toString(nums)); } public s.原创 2020-07-22 09:37:55 · 77 阅读 · 0 评论 -
find in mountain array
思路:首先使用二分法找到最高点然后从最高点左侧进行查找再从最高点右侧查找/** * // This is MountainArray's API interface. * // You should not implement it, or speculate about its implementation * interface MountainArray { * public int get(int index) {} * public int lengt.原创 2020-07-18 11:19:51 · 90 阅读 · 0 评论 -
binary tree right view
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { .原创 2020-07-12 14:24:59 · 88 阅读 · 0 评论 -
binary tree Zigzag level order
思路:层次遍历后将偶数层的list进行反转/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode.原创 2020-07-11 17:32:58 · 88 阅读 · 0 评论 -
volid Sudoku
验证给定的一个二维数组是否为数独每一行的数字必须唯一 每一列的数字必须唯一 九宫格中的每三行,三列中数字必须唯一 遍历九宫格中每个数字 将当前数字属于哪一行表示成 行数 + 数字 将当前数字属于哪一列表示成 数字 +列数 将当前数字属于哪一个三宫格表示成 三宫格行数 + 数字 + 三宫格列数class Solution { public boolean isValidSudoku(char[][] board) { Set&...原创 2020-07-11 11:32:32 · 99 阅读 · 0 评论 -
Move even index location to tail
/** * 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; } * }.原创 2020-07-05 20:33:05 · 180 阅读 · 0 评论 -
reverse integer
class Solution { public int reverse(int x) { // boolean flag = true; // if(x<0){ // x = -x; // flag = false; // } // List<Integer> list = new ArrayList<>(); // while(x>=.原创 2020-07-05 10:18:11 · 1280 阅读 · 0 评论 -
remove duplicates from sorted array
移除排序数组中重复的数字class Solution { public int removeDuplicates(int[] nums) { if(nums.length <2) return nums.length; int id = 1; for(int i=1;i<nums.length;i++){ if(nums[i] != nums[i-1]) nums[id++] = nums[i];原创 2020-06-29 20:54:48 · 65 阅读 · 0 评论 -
Intersection of Two Arrays 2
寻找两个数组中共同重复的数字,并将他们装到一个数组中class Solution { public int[] intersect(int[] nums1, int[] nums2) { // 注释部分为最长的具有相同顺序的最长数组 // ArrayList<Integer> list = new ArrayList<>(); // for(int i=0;i<nums1.length;i++){ /原创 2020-06-27 13:38:30 · 82 阅读 · 0 评论 -
insert delete getRandom Set
class RandomizedSet { List<Integer> list = null; Map<Integer,Integer> map = null; Random random = new Random(); /** Initialize your data structure here. */ public RandomizedSet() { list = new ArrayList<>.原创 2020-06-26 20:20:03 · 89 阅读 · 0 评论 -
Pascals Triangle
class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> res = new ArrayList<>(); if(numRows == 0) return res; for(int i=0;i<numRows;i++){ .原创 2020-06-26 11:27:18 · 80 阅读 · 0 评论 -
first unique character in a string
在给定的一个字符串中,找到第一个唯一的字符,并返回它的下标class Solution { public int firstUniqChar(String s) {// if(s == null || "".equals(s)) return -1;// int res = -1;// Map<Character,Integer> map = new HashMap<>();// for(int i原创 2020-06-25 10:39:42 · 103 阅读 · 0 评论 -
Roman to Integer
罗马数字转数字Symbol ValueI 1V 5X 10L 50C 100D 500M 1000其中,一些特殊规则如下:Ican be placed beforeV(5) andX(10) to make 4 and 9. Xcan be placed beforeL(50) an...原创 2020-06-20 20:22:30 · 68 阅读 · 0 评论 -
Best time to buy and sell stock 2
低买高卖,可以多次进行,求最大的利润class Solution { public int maxProfit(int[] prices) { int profit = 0,i=0; while(i<prices.length){ while(i<prices.length-1 && prices[i+1]<=prices[i]) i++; int min = prices[.原创 2020-06-20 19:39:17 · 77 阅读 · 0 评论 -
convert sorted array to binary search tree
对给定的升序数组,将其转化成深度最低的二叉搜索树/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode原创 2020-06-18 20:09:21 · 61 阅读 · 0 评论 -
判断字符串是否可以转成数字
字符串可能是如下几类+11-11+111.22-111.22"+100","5e2","-123","3.1416"和"-1E-16"如下则不可以转成数字12e","1a3.14","1.2.3","+-5"和"12e+4.3"public class Solution { public boolean isNumeric(char[] str) { try{ Double.parseDouble(new String(str原创 2020-06-14 17:02:39 · 898 阅读 · 0 评论 -
移除链表中重复的元素
其中链表为递增的例子:1->2->2->3->3->4->5->6移除重复的元素之后为1->4->5->6/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/import java.util.*;public class Soluti原创 2020-06-12 19:30:25 · 119 阅读 · 0 评论 -
滑动窗口最大值
给定一个整数数组num,和一个滑动窗口的size,每次滑动窗口向右移动一个位置,求所有经过的这些滑动窗口的最大值import java.util.*;public class Solution { public ArrayList<Integer> maxInWindows(int [] num, int size){ ArrayList<Integer> res = new ArrayList<>(); if(size =原创 2020-06-06 19:40:31 · 168 阅读 · 0 评论 -
矩阵中的路径
给定一个字符串矩阵matrix,和这个矩阵的rows,cols即矩阵的行数和列数。再给定一个字符串,判断这个字符串是否在这个字符串矩阵中的路径中。public class Solution { public boolean hasPath(char[] matrix, int rows, int cols, char[] str){ int[] res = new int[matrix.length]; for(int i=0;i<rows;i++){原创 2020-06-06 10:45:23 · 113 阅读 · 0 评论 -
机器人运动范围
给定一个阈值threshold,机器人运动的范围是当前运动的坐标点的各个 位上的数字相加小于等于 thresholdpublic class Solution { int count = 0; public int movingCount(int threshold, int rows, int cols){ int[][] nums = new int[rows][cols]; helper(nums,threshold,0,0); r原创 2020-06-05 11:04:25 · 156 阅读 · 0 评论 -
LFU Java实现
LFU:least frequency used,当缓存空间不足时,移除使用频率最少的元素思路:使用三种数据结构,一个是缓存的元素LFUNode<k,v>, HashMap<k,LFUNode>, TreeMap<Integer,List<LFUNode>>class LFUNode<k,V>{ private k key; private v value; private int count;原创 2020-06-03 10:44:33 · 1767 阅读 · 0 评论 -
kth smallest element in bst
寻找二叉搜索树第k小的节点的值思路:首先二叉搜索树的是所有右子树大于根节点,所有的左子树小于根节点。对二叉搜索树进行中序遍历,即从小到大将数放到栈中,当放入的第k个时,就为第k小的元素/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * .原创 2020-06-02 19:50:44 · 70 阅读 · 0 评论 -
冒泡排序Java实现
冒泡排序就是将连续的两个数进行比较,交换位置,最后得出升序排序还是降序排序的结果import java.util.Arrays;public class BubbleSort { public static void main(String[] args) { int[] nums = {1,3,2,6,5,7,9,33}; sort(nums); System.out.println(Arrays.toString(nums));原创 2020-06-01 20:13:36 · 97 阅读 · 0 评论 -
longest valid parentheses
给定一个字符串,只包含‘(',')',查找给定字符串中最长的有效'(',')'长度class Solution { public int longestValidParentheses(String s) { Stack<Integer> stack = new Stack<>(); int max = 0,left = -1; for(int i=0;i<s.length();i++){ i原创 2020-06-01 19:39:35 · 61 阅读 · 0 评论 -
3Sum
给定一个数组,找出所有的3个数字之和为0的组合class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new ArrayList<>(); threeSum(res,new ArrayList<>(),nums,0); return res;.原创 2020-05-28 15:02:46 · 121 阅读 · 1 评论 -
validate binary search tree
判断一个二叉树是否是二叉搜索树/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, Tr原创 2020-05-27 20:35:43 · 88 阅读 · 0 评论 -
二叉树前序,中序,后续的递归,非递归遍历,层次遍历Java实现
先序遍历二叉树class Solution{ // 递归实现方式 void preOrder(TreeNode root){ if(root == null) return; System.out.println(root.val); preOrder(root.left); preOrder(root.right); } // 非递归实现方式 void preOrder(TreeNode root...原创 2020-05-27 12:13:46 · 118 阅读 · 0 评论 -
Longest Palindromic substring
找出最长对称回文子字符串class Solution { public String longestPalindrome(String s) { if(s == null || s.length() < 1) return ""; int start = 0,end = 0; for(int i=0;i<s.length();i++){ int len1 = helper(s,i,i);原创 2020-05-24 17:41:09 · 137 阅读 · 0 评论 -
Longest Substring without repeating characters
在一个字符串中,寻找最长的没有重复的子字符串class Solution { public int lengthOfLongestSubstring(String s) { // 注释部分为复杂度O(n2)的解法 // int res = 0; // if(s == null || "".equals(s)) return res; // char[] chars = s.toCharArray(); // Set原创 2020-05-23 20:13:15 · 81 阅读 · 0 评论 -
Maxinum Product Subarray
给定一个数组,找出最大连续的子数组的最大乘积,并返回最大乘积思路:给定的数组中包含正数,负数。遍历给定的数组,若当前的数是负数,则将最大的数和最小的数交换一下。具体代码如下class Solution { public int maxProduct(int[] nums) { int min = nums[0],max = nums[0],res = nums[0]; for(int i=1;i<nums.length;i++){原创 2020-05-22 19:57:08 · 91 阅读 · 0 评论 -
Java实现LRU
LRU:least recently used,最近最少使用有两种实现方式,一种是利用LinkedHashMap,比较简单二是利用HashMap+链表,此种实现是完全自己实现,也是面试中需要手写的第一种实现方式利用LinkedHashMappublic class LRUCache<k,v> extends LinkedHashMap<k,v>{ private static MAX_SIZE = 3; public LRUCache<k,原创 2020-05-21 21:22:21 · 100 阅读 · 0 评论