![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
Make@more
程序猿入门的小王~~~
展开
-
leetcode-17-电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。给出数字到字母的映射如下(与电话按键相同)。> 注意 1 不对应任何字母。示例:输入:"23" (2: abc 3: def)输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-numb原创 2020-10-18 20:07:49 · 291 阅读 · 0 评论 -
leetcode31-下一个排列
数字从左往右代表一个数字,找到比这个数字大的最小值,若没有找到,则返回最小值以下是一些例子,输入位于左侧列,其相应输出位于右侧列。1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/next-permutation著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。解题思路:1.从后往前遍历数组,找到递增之后第一个递减的数字,其索引位置为i,原创 2020-10-17 11:05:17 · 122 阅读 · 0 评论 -
字符串模式匹配
对于特定的模式pattern以及特定的字符串str,判断str中的非空单词是否能和pattern中的字符一一对应,注意这里的一一对应是双向的。示例1:输入:“effe”,“good bad bad good”输出:true示例2:输入:“effe”,“good bad bad not” 输出:false输入:“efff”,“good bab bad bad bad” 输出:false解题步骤:先将字符串差分成多个单词,存放至list中list长度和pattern.length()长度原创 2020-10-14 10:24:53 · 1086 阅读 · 0 评论 -
leetcode-415-字符串相加
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。从末尾相加,记录进位carry public String addStrings(String num1, String num2) { StringBuilder stringBuilder = new StringBuilder(); int i = num1.length() - 1; int j = num2.length() - 1; int carry = 0原创 2020-10-11 16:03:25 · 54 阅读 · 0 评论 -
leetcode-35-搜索插入位置
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。你可以假设数组中无重复元素。示例 1:输入: [1,3,5,6], 5输出: 2来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/search-insert-position著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 public int searchInsert(int[] nu原创 2020-10-11 15:09:05 · 108 阅读 · 0 评论 -
leetcode-40-组合总和II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的每个数字在每个组合中只能使用一次。说明:所有数字(包括目标数)都是正整数。解集不能包含重复的组合。 示例 1:输入: candidates = [10,1,2,7,6,1,5], target = 8,所求解集为:[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6]]来源:力扣原创 2020-10-11 15:02:58 · 114 阅读 · 0 评论 -
leetcode-39-组合总和
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的数字可以无限制重复被选取。输入:candidates = [2,3,6,7], target = 7,所求解集为:[ [7], [2,2,3]]解题方法:回溯,深度遍历数组(排序后的数组),遍历节点时先判断当前值如果当前值大于target,就跳出本循环(而不是本次循环) public List<L原创 2020-10-11 13:09:31 · 56 阅读 · 0 评论 -
leetcode-64-路径最小和
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。说明:每次只能向下或者向右移动一步。示例:输入:[[1,3,1],[1,5,1],[4,2,1]]输出: 7解释: 因为路径 1→3→1→1→1 的总和最小。来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/minimum-path-sum著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。方法:新定义一个大小原创 2020-10-10 13:35:26 · 76 阅读 · 0 评论 -
十进制转八进制、十六进制转八进制
十进制转八进制例子:输入: 10输出: 12思路:对8进行取模运算并存入栈,直至该数字为0,将栈输出public static void Dex2Oct(int num){ if (num < 8){ System.out.println("转换后的八进制数为:"+ num); } int i; Stack<Integer> stack = new Stack<>();原创 2020-10-10 12:48:19 · 458 阅读 · 0 评论 -
leetcode-19-删除链表倒数第n个节点
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。示例:给定一个链表: 1->2->3->4->5, 和 n = 2.当删除了倒数第二个节点后,链表变为 1->2->3->5.来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。方法一:先计算链表的长度,然后原创 2020-10-08 22:33:16 · 85 阅读 · 0 评论 -
leetcode-24-链表节点两两交换
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。示例:给定 1->2->3->4, 你应该返回 2->1->4->3.来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。方法一:采用迭代法public ListNode swapPa原创 2020-10-08 21:37:34 · 90 阅读 · 0 评论 -
剑指offer-06-翻转链表
一、翻转链表,输出链表 public ListNode reverseList(ListNode head) { ListNode pre = null; ListNode cur = head; while (cur != null){ ListNode next = cur.next; cur.next = pre; pre = cur; cur = n原创 2020-10-05 11:13:02 · 74 阅读 · 0 评论 -
剑指offer03、04、05
一、offer-03找出数组中重复的数字。在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。示例 1:输入:[2, 3, 1, 0, 2, 5, 3]输出:2 或 3题解: public static int findRepeatNumber(int[] nums) { if (nums.length == 0 || nums == n原创 2020-10-05 09:45:14 · 92 阅读 · 0 评论 -
leetcode-11-盛最多水的容器
给你 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。说明:你不能倾斜容器,且 n 的值至少为 2。输入:[1,8,6,2,5,4,8,3,7]输出:49解法1:双循环遍历 public static int maxArea(int[] height) { int res = 0;原创 2020-09-24 18:14:06 · 80 阅读 · 0 评论 -
leetcode-12-整数转罗马数字
public class test_12 { int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; String[] symbols = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; public String intToRoman(int num) { StringBuilder bs = new S原创 2020-09-24 16:45:56 · 54 阅读 · 0 评论 -
leetcode-47-全排列II
给定一个可包含重复数字的序列,返回所有不重复的全排列。示例:输入: [1,1,2]输出:[ [1,1,2], [1,2,1], [2,1,1]]来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/permutations-ii著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。1、此处可能包含重复数字、需要筛去重复的组合,因此数组需要重排序2、在深度遍历的时候,需要剪枝,即去掉重复的情况if (i &原创 2020-09-18 20:12:48 · 81 阅读 · 0 评论 -
leetcode-46-数字全排列
数字全排列:输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/permutations著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。思路:回溯思想解决该问题:1、需要一个长度为数组长度的集合res,将每次满足条件的结果存放进去;同时也需要一个boolea原创 2020-09-18 19:26:28 · 85 阅读 · 0 评论 -
leetcode-13-罗马数字转整数
public static int getValue(char c){ switch (c){ case 'I': return 1; case 'V': return 5; case 'X': return 10; case 'L': return 50; case 'C': return 100; case 'D': return 500;原创 2020-09-17 19:14:49 · 60 阅读 · 0 评论 -
leetcode-28-实现strStr() 字符串匹配
public int strStr(String haystack, String needle) { //不满足的条件直接返回-1 if (haystack.length() < needle.length() || haystack == null){ return -1; } if (needle.length() == 0 || needle == null){ return 0;原创 2020-09-17 17:13:08 · 62 阅读 · 0 评论 -
leetcode-018-四数之和
解题思路:1、同三数之和思路,双指针遍历(双层for循环+双指针)原创 2020-09-16 17:28:33 · 73 阅读 · 0 评论 -
leetcode-001-两数之和
返回两个数的下标(单解)若要求返回数组的索引,则不能将数组进行排序来解决class Solution { public int[] twoSum(int[] nums, int target) { if(nums.length < 2 || nums == null){ return new int[2]; } for(int i=0;i < nums.length;i++){ for(int j=0;j < nums原创 2020-09-16 16:37:51 · 80 阅读 · 0 评论 -
leetcode-015-三数之和
import java.util.ArrayList;import java.util.Arrays;import java.util.List;//leetcode-15-三数之和public class test_15 { public static void main(String[] args){ int[] nums = {-1, 0, 1, 2, -1, -4}; List<List<Integer>> lists = th原创 2020-09-16 09:56:59 · 74 阅读 · 0 评论 -
leetcode-005-最长回文子串
class Solution { public static boolean fun(String str){ int j = str.length(); for(int i = 0;i <= str.length() / 2; i++){ if(str.charAt(i) != str.charAt(j - 1)){ return false; } j--;原创 2020-09-15 14:41:01 · 165 阅读 · 1 评论 -
leetcode-016-最接近数组之和
最接近数组之和import java.util.Arrays;public class test_16 { public static void main(String[] args){ int[] nums = {-1,2,1,-4}; int i = threeSumClosest(nums, 1); System.out.println(i); } public static int threeSumClosest(int原创 2020-09-15 14:40:44 · 176 阅读 · 0 评论