题目描述

删除给出链表中的重复元素,使链表中的所有元素都只出现一次

例如:

给出的链表为1->1->2,返回1->2.

给出的链表为1->1->2->3->3,返回1->2->3.

public class Solution {public ListNode deleteDuplicates(ListNode head) {if(head == null || head.next == null) {return head;}ListNode first = new ListNode(-1);first.next = head;ListNode pre = first;ListNode now = head;while(now != null && now.next != null){pre = now;if(now.val == now.next.val){while(now.next != null && now.val == now.next.val) {now = now.next;}pre.next = now.next;}now = now.next;}return first.next;}
}

题目描述

给出一个二维字符数组和一个单词,判断单词是否在数组中出现,

单词由相邻单元格的字母连接而成,相邻单元指的是上下左右相邻。同一单元格的字母不能多次使用。

例如:

给出的字符数组=

[↵  ["ABCE"],↵  ["SFCS"],↵  ["ADEE"]↵]
public class Solution {public boolean exist(char[][] board, String word) {int m = board.length;int n = board[0].length;boolean[][] visited = new boolean[m][n];for(int i = 0; i < m; i++) {for(int j = 0; j < n; j ++ ) {if(deep(board, word, visited, i, j, 0, m, n)) {return true;}}}return false;}private boolean deep(char[][] board, String word,  boolean[][] visited, int i, int j, int count, int m, int n) {if(count == word.length()) {return true;}if(i < 0 || i >= m || j < 0 || j >= n || board[i][j] != word.charAt(count) || visited[i][j]) {return false;}visited[i][j] = true;boolean res = deep(board, word, visited, i - 1, j, count + 1, m, n) ||deep(board, word, visited, i + 1, j, count + 1, m, n) ||deep(board, word, visited, i, j - 1, count + 1, m, n) ||deep(board, word, visited, i, j + 1, count + 1, m, n);visited[i][j] = false;return res;}
}

题目描述

给出两个整数n和k,返回从1到n中取k个数字的所有可能的组合

例如:

如果n=4,k=2,结果为

[↵  [2,4],↵  [3,4],↵  [2,3],↵  [1,2],↵  [1,3],↵  [1,4],↵]
public class Solution {ArrayList<ArrayList<Integer>> result;public ArrayList<ArrayList<Integer>> combine(int n, int k) {result = new ArrayList<ArrayList<Integer>>();ArrayList<Integer> temp = new ArrayList<Integer>();if(n < 1 || k < 1 || n < k) {return result;}combine(n, k, 1, temp);return result;}private void combine(int n, int k, int index, ArrayList<Integer> tem) {if(tem.size() == k) {result.add(new ArrayList<>(tem));return;}if(index > n) {return;} else {int left = k - tem.size() - 1;for (int i = index; i <= n - left; i++) {tem.add(i);combine(n, k, i + 1, tem);tem.remove(tem.size() - 1);}}}
}

题目描述

给出两个字符串S和T,要求在O(n)的时间复杂度内在S中找出最短的包含T中所有字符的子串。

例如:

S ="ADOBECODEBANC"
T ="ABC"

找出的最短子串为"BANC".

注意:

如果S中没有包含T中所有字符的子串,返回空字符串 “”;

满足条件的子串可能有很多,但是题目保证满足条件的最短的子串唯一。

public class Solution {public String minWindow(String S, String T) {int[] map = new int[128];for(int i = 0; i < T.length(); i++) {map[T.charAt(i)]++;}int left = 0;int right = 0;int result = Integer.MAX_VALUE;int counter = T.length();int head = 0;while(right < S.length()) {if(map[S.charAt(right++)]-- > 0) {counter--;}while (counter == 0) {if(right - left < result) {head = left;result = right - left;}if(map[S.charAt(left++)]++ == 0) {  counter++;                     }}}return result == Integer.MAX_VALUE ? "" : S.substring(head, head + result);}
}

题目描述

现在有一个包含n个物体的数组,其中物体颜色为颜色为红色、白色或蓝色,请对这个数组进行排序,让相同颜色的物体相邻,颜色的顺序为红色,白色,蓝色。

我们用0,1,2分别代表颜色红,白,蓝

注意:

本题要求你不能使用排序库函数

扩展:

一个非常直接的解法是两步的计数排序的算法

首先:遍历一遍数组,记录0,1,2的数量,然后重写这个数组,先将0写入,再将1写入,再将2写入

你能给出一个只用一步,并且能在常数级空间复杂度解决这个问题的算法吗?

public class Solution {public void sortColors(int[] A) {int zero = 0;int two = A.length - 1;for(int i = 0; i <= two; i++) {if(A[i] == 0) {A[i] = A[zero];A[zero++] = 0;} else if(A[i] == 2) {A[i] = A[two];A[two] = 2;i--;two--;}}}
}

题目描述

请写出一个高效的在m*n矩阵中判断目标值是否存在的算法,矩阵具有如下特征:

每一行的数字都从左到右排序

每一行的第一个数字都比上一行最后一个数字大

例如:

对于下面的矩阵:

[↵  [1,   3,  5,  7],↵  [10, 11, 16, 20],↵  [23, 30, 34, 50]↵]

要搜索的目标值为3,返回true;

public class Solution {public boolean searchMatrix(int[][] matrix, int target) {int m = matrix.length;int n = matrix[0].length;int i = 0;for(; i < m; i++) {if(matrix[i][0] == target) {return true;} else if(matrix[i][0] > target) {break;}}if(i == 0) {return false;}for(int j = 0; j < n; j++) {if(matrix[i - 1][j] == target) {return true;} else if(matrix[i - 1][j] > target) {return false;}}return false;}
}

题目描述

给定一个m*n的矩阵,如果有一个元素是0,就把该元素所在的行和列上的元素全置为0,要求使用原地算法。

拓展:

你的算法有使用额外的空间吗?

一种比较直接的算法是利用O(m,n)的空间,但是这不是一个好的解法

使用简单的改进可以在O(m+n)的空间解决这个问题,但是还不是最佳的解法

你能在常量级的空间复杂度内解决这个问题吗?

public class Solution {public void setZeroes(int[][] matrix) {int m = matrix.length;int n = matrix[0].length;boolean nZero = false;boolean mZero = false;for(int i = 0; i < n; i++) {if(matrix[0][i] == 0) {nZero = true;break;}}for(int i = 0; i < m; i++) {if(matrix[i][0] == 0) {mZero = true;break;}}for(int i = 1; i < m; i++) {for(int j = 1; j < n; j++) {if(matrix[i][j] == 0) {matrix[i][0] = 0;matrix[0][j] = 0;}}}for (int i = 1; i < m; i++) {for (int j = 1; j < n; j++) {if (0 == matrix[i][0] || 0 == matrix[0][j]) {matrix[i][j] = 0;}}}if (nZero)for (int i = 0; i < n; i++) {matrix[0][i] = 0;}if (mZero)for (int i = 0; i < m; i++)matrix[i][0] = 0;}
}

算法题存档20191223相关推荐

  1. 算法题存档20200505

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  2. 算法题存档20190207

    题目描述 如果一个整数只能被1和自己整除,就称这个数是素数. 如果一个数正着反着都是一样,就称为这个数是回文数.例如:6, 66, 606, 6666 如果一个数字既是素数也是回文数,就称这个数是回文 ...

  3. 算法题存档20190127

    题目描述 假设一个探险家被困在了地底的迷宫之中,要从当前位置开始找到一条通往迷宫出口的路径.迷宫可以用一个二维矩阵组成,有的部分是墙,有的部分是路.迷宫之中有的路上还有门,每扇门都在迷宫的某个地方有与 ...

  4. 算法题存档20200627(树)

    给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表. 如果在二叉树中,存在一条一直向下的路径,且每个点的数值恰好一一对应以 head 为首的链表中每个节点的值,那么请你返回 Tru ...

  5. 算法题存档2020425

    给定一个 没有重复 数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [   [1,2,3],   [1,3,2],   [2,1,3],   [2,3,1],   [3, ...

  6. 20191219算法题存档

    题目描述 给出两个有序的整数数组A和B,请将数组B合并到数组A中,变成一个有序的数组 注意: 可以假设A数组有足够的空间存放B数组的元素,A和B中初始的元素数目分别为m和n public class ...

  7. 20190730算法题存档

    题目描述 Given a singly linked list L: L 0→L 1→-→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→ ...

  8. 20190724算法题存档

    题目描述 Sort a linked list in O(n log n) time using constant space complexity. public class Solution {p ...

  9. 20190719算法题存档

    题目描述 小Q得到一个神奇的数列: 1, 12, 123,...12345678910,1234567891011.... 并且小Q对于能否被3整除这个性质很感兴趣. 小Q现在希望你能帮他计算一下从数 ...

最新文章

  1. 修改linux文件夹密码忘记,Linux中如何重新获取丢失的密码
  2. python安装要什么配置_Python的安装和详细配置
  3. React 16 加载性能优化指南
  4. 阿里开源物联网操作系统 AliOS Things 3.0 发布,集成平头哥 AI 芯片架构!
  5. Visual Studio 2010 Ultimate敏捷之道:特性解析
  6. 2021-06-25 WebStorm的设置代码tab宽度等
  7. QThread: Destroyed while thread is still running 解决方法
  8. 【hihocoder 1477】闰秒
  9. hihoCoder1378 (最大流最小割)
  10. 菲尔兹奖-历届获得者
  11. 《uniapp遇到的问题》 详情 ------ 编号:001
  12. dos2unix和unix2dos命令使用
  13. Zimbra禁止接收带有加密的文件邮件 提醒病毒(Heuristics.Encrypted.PDF)
  14. vulnhub靶场之 LordOfTheRoot_1.0.1
  15. 多家银行同城跨行取款手续费上涨到4元
  16. 用树莓派搭建全功能NAS服务器(05):玩转影音看片整理大法
  17. HTML实现表白biu爱心特效 (程序员专属情人节表白网站)
  18. 【NOIP2012模拟10.26】火炬手
  19. Linux系统之alias别名的基本使用
  20. 快速上手正则表达式RegularExpression(正则表达式常用笔记整理)

热门文章

  1. 高级JAVA - 动态代理的实现原理和源码分析
  2. java nbsp_引用了实体 nbsp
  3. python安装到桌面的路径是什么_Python 获取windows桌面路径的5种方法小结
  4. 四天学会Mybatis
  5. c语言常用算法分析 微盘,C语言常用算法归纳.pdf
  6. Avro 数据格式和命令行
  7. D-Wave的量子计算机,到底能不能进行量子计算?
  8. mysql 案例~ 主从复制转化为级联复制
  9. 查看进程中占cpu高的线程方法
  10. 最安全的浏览器?黑客大赛微软Edge被破解5次夺下“冠军”