随笔分类 - 算法竞赛
摘要:原题点这里 这个题刚开始看,一点思路也没有呀。。。。然后就迫不及待的看了题解。也不知道什么时候能积累出量变 这个题虽然标记为困难,但是其实不难。最简单的办法,我们计算每个位置的储水量:Min(maxL,maxR)-height[i] 我们在计算i位置上的储水量,分别向左向右找到最大值即可。 124m
阅读全文
摘要:原题点这里 水平扫描:依次取每个str的第i个字符,若相同,则公共子串+1,否则结束。 public static String longestCommonPrefix(String[] strs) { int strNum = strs.length; if(strNum==1) return s
阅读全文
摘要:原题点这里 这个是每日一题里面的,所以顺序有点混乱 是个模拟的水题 public static int check(int[][]board,int row,int col){ int[] dx = new int[]{-1,0,1}; int[] dy = new int[]{-1,0,1}; i
阅读全文
摘要:原题点这里 这个从后面遍历罗马数字,然后比较当前字符的整数是否大于它的前一个,如果是,则当前整数取负值,否则取正值。 方法一:利用map实现 10ms public static int romanToInt(String s) { HashMap<Character,Integer> r2i =
阅读全文
摘要:原题点这里 直接暴力模拟 6ms public static String getOne(int num,int e){ String ans = null; switch (num){ case 0: ans= ""; break; case 1: ans= "I";break; case 2:
阅读全文
摘要:原题 暴力法: public static int maxArea(int[] height) { int n = height.length; int ans = 0; for(int i=0;i<n-1;i++){ for(int j=i+1;j<n;j++) { int len = j-i;
阅读全文
摘要:原题 转成字符串,然后简单的判断一下。这里我自己转换要20ms,用库函数只需要11ms。只贴库函数转换的代码。 public static boolean isPalindrome(int x) { if(x<0) return false; String ans = String.valueOf(
阅读全文
摘要:原题 这个题很简单的.最开始的想法是trim一下去掉开头的空格.然后放到字符串中,转成整数. public static int myAtoi(String str) { StringBuilder res = new StringBuilder(""); StringBuilder str2 =
阅读全文
摘要:整数反转 解法1 if(x==0) return x; StringBuilder res=new StringBuilder(); if(x<0){ res.append('-'); x = Math.abs(x); } while (x>0){ int a = x%10; res.append(
阅读全文
摘要:将一个字符串做z字形转换 很容易想到暴力求解. 这里要注意的是, 静态方法不能新建内部类实例. class zigC implements Comparable<zigC>{ public zigC(char c, int row, int col) { this.c = c; this.row =
阅读全文
摘要:求一个字符串的最长回文子串 我发现letcode好像不限制执行时间? 这个题有个技巧:在每个字符两边添加一个无关字符: aba -> *a*b*a*; abba->*a*b*b*a* .这样就会得到一个恒为奇数串的字符串. 然后有两种解法: 1) 以每个字符为中心,向两边最大扩展 O(n^2) in
阅读全文
摘要:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 暴力求解会超时。有两种滑动窗口的方法,时间都差不多。 public int lengthOfLongestSubstring(String
阅读全文
摘要:https://leetcode-cn.com/problems/add-two-numbers/ ListNode root = new ListNode(0); ListNode cur = root; int retain = 0; while (l1!=null || l2!=null ||
阅读全文
摘要:int [] two_num(int [] nums,int target){ HashMap<Integer, Integer> num_indx = new HashMap<Integer, Integer>(); int [] ans = new int [2]; for(int i=0;i<
阅读全文
摘要:蒜头君和花椰妹在玩一个游戏,他们在地上将 nn 颗石子排成一排,编号为 11 到 nn。开始时,蒜头君随机取出了 22 颗石子扔掉,假设蒜头君取出的 22 颗石子的编号为 aa, bb。游戏规则如下,蒜头君和花椰妹 22 人轮流取石子,每次取石子,假设某人取出的石子编号为 ii,那么必须要找到一对
阅读全文

浙公网安备 33010602011771号