![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
cucumberpieces
简简单单,随遇而安~
展开
-
leetcode-49. Group Anagrams
https://leetcode.com/problems/group-anagrams/#/description问题描述:Given an array of strings, group anagrams together.For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:[ ["ate", "ea原创 2017-06-13 22:55:22 · 242 阅读 · 0 评论 -
leetcode-48. Rotate Image
d原创 2017-05-21 09:52:36 · 377 阅读 · 0 评论 -
leetcode-6. ZigZag Conversion(Z型字符串)
https://leetcode.com/problems/zigzag-conversion/#/description思路解析: 输入:一个字符串和行数 首先将string转换为字符数组使用toCharArray() 所以每一行用一个StringBuilder对象来保存,因为可以更改其值。 主要确定好两个方向的位置,竖向和倾斜。只需要控制好sb的下标即可。注意: 在控制sb下标的同时也原创 2017-06-05 20:58:25 · 381 阅读 · 0 评论 -
leetcode-40. Combination Sum II
https://leetcode.com/problems/combination-sum-ii/#/description 问题描述: 给定一个数组(有重复元素),和一个目标值,找出这个数组的子集,是这个自己的元素和为这个目标值。 数组中的元素只能使用一次。思路解析: 参考http://blog.csdn.net/u013275928/article/details/72588102 稍加原创 2017-05-20 21:45:14 · 154 阅读 · 0 评论 -
leetcode-39. Combination Sum
https://leetcode.com/problems/combination-sum/#/description问题描述: 给定一个数组(没有重复元素),和一个目标值,找出这个数组的子集,是这个自己的元素和为这个目标值。 数组中的元素可以重复使用。思路解析: 参考http://blog.csdn.net/u013275928/article/details/72230646 完全可以利原创 2017-05-20 21:31:38 · 175 阅读 · 0 评论 -
leetcode-46. Permutations(非重复元素全排列)
https://leetcode.com/problems/permutations/#/description问题描述:全排列问题思路解析:思路同求子集问题 http://blog.csdn.net/u013275928/article/details/72230646 http://blog.csdn.net/u013275928/article/details/72290091 同样是用原创 2017-05-18 21:20:03 · 283 阅读 · 0 评论 -
leetcode-47. Permutations II(重复元素全排列)
https://leetcode.com/problems/permutations-ii/#/description问题描述: 含有重复元素的全排列思路解析: 参考http://blog.csdn.net/u013275928/article/details/72510351 这次考虑了重复元素,新增了一个用来表示该元素是否已经加入到了tempList中的boolean数组,若为真则已经存在原创 2017-05-19 21:59:32 · 256 阅读 · 0 评论 -
leetcode-14. Longest Common Prefix(寻找字符串数组最长公共前缀)
https://leetcode.com/problems/longest-common-prefix/#/description思路解析: 因为是字符串数组,对其进行排序,所以只需要比较数组当中的第一个string和最后一个string,找他们两个的最长公共前缀即可,将string转化为字符数组用到了toCharArray方法,将最后的结果放到一个可修改的string当中,所以就用到了Strin原创 2017-06-04 22:50:20 · 457 阅读 · 0 评论 -
leetcode-5. Longest Palindromic Substring(寻找最长回文子串)
https://leetcode.com/problems/longest-palindromic-substring/#/description问题描述:Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:原创 2017-06-04 22:25:57 · 250 阅读 · 0 评论 -
leetcode-121. Best Time to Buy and Sell Stock
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/#/description问题描述: Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to co原创 2017-05-30 22:53:58 · 170 阅读 · 0 评论 -
leetcode-96. Unique Binary Search Trees
https://leetcode.com/problems/unique-binary-search-trees/#/description代码如下:思路解析:未完!!!!!!!!!!!!!!!!!!!!!public class Solution { public int numTrees(int n) { int[] d=new int[n+1]; d[0]=原创 2017-06-01 22:03:38 · 156 阅读 · 0 评论 -
leetcode-54. Spiral Matrix(打印蛇形矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example, Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You sh原创 2017-05-22 17:04:09 · 718 阅读 · 0 评论 -
leetcode-59. Spiral Matrix II(打印蛇形方阵)
https://leetcode.com/problems/spiral-matrix-ii/#/description问题描述: 还是蛇形方阵,但是只输入矩阵的行数或列数,打印含有n*n个元素的蛇形矩阵。思路解析: 参考之前的一道题:http://blog.csdn.net/u013275928/article/details/72627957代码如下:public class Solutio原创 2017-05-22 19:02:24 · 291 阅读 · 0 评论 -
leetcode-67. Add Binary(二进制串求和)
https://leetcode.com/problems/add-binary/#/description问题描述:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".代码如下:public class Solution {原创 2017-06-13 22:45:40 · 308 阅读 · 0 评论 -
leetcode-17. Letter Combinations of a Phone Number
https://leetcode.com/problems/letter-combinations-of-a-phone-number/#/description问题描述: Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].思路解析: 将每个按键代表的字符串映射到一个str原创 2017-06-10 20:15:37 · 183 阅读 · 0 评论 -
leetcode-12. Integer to Roman(整数转换成罗马数字)
https://leetcode.com/problems/integer-to-roman/#/description代码如下:public class Solution { public String intToRoman(int num) { String result=""; String[][] roman = { {"",原创 2017-06-09 22:33:09 · 249 阅读 · 0 评论 -
leetcode-13. Roman to Integer(罗马数字转化成整数)
https://leetcode.com/problems/roman-to-integer/#/description思路解析: 【罗马数字】1~9: {“I”, “II”, “III”, “IV”, “V”, “VI”, “VII”, “VIII”, “IX”};10~90: {“X”, “XX”, “XXX”, “XL”, “L”, “LX”, “LXX”, “LXXX”, “XC”};10原创 2017-06-09 22:35:54 · 194 阅读 · 0 评论 -
leetcode-43. Multiply Strings(大整数相乘)
https://leetcode.com/problems/multiply-strings/#/description思路解析: 思路如图片所示。它描述的是我们计算乘法的过程。仔细分析可以发现,对于原来在上面字符串中下标为1的“2”和在下面字符串中下标为0的“4”的相乘结果08出现在了最后的乘法结果字符串的下标1和2处。这一结果对其他下标的数字同样成立:下标i和下标j相乘的高位结果位于下标i+j原创 2017-06-08 21:04:06 · 384 阅读 · 0 评论 -
leetcode-125. Valid Palindrome(验证含有标点符号String是否为回文串)
https://leetcode.com/problems/valid-palindrome/#/description问题描述:指定含有标点符号空格的字符串,判断是否为回文串。思路解析: [^A-Za-z0-9]正则表达式,过滤掉非数字和大小写字母的字符。 用到了StringBuffer的reverse()与正则表达式过滤的串比较相同则是回文串。代码如下:public class Soluti原创 2017-06-18 23:00:38 · 431 阅读 · 0 评论 -
leetcode-3. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/#/description代码如下:public class Solution { public int lengthOfLongestSubstring(String s) { Set<Character> set=new原创 2017-06-02 21:55:47 · 176 阅读 · 0 评论 -
leetcode-63. Unique Paths II
https://leetcode.com/problems/unique-paths-ii/#/description问题描述: 参考http://blog.csdn.net/u013275928/article/details/72629236 在这道题的基础上加入了阻碍元素。思路解析: 这道题,没有用到额外的二维数组用来记录方法数,而是直接利用参数数组。当数组中有1的时候,将其置为0,其他原创 2017-05-22 21:06:02 · 179 阅读 · 0 评论 -
leetcode-62. Unique Paths
https://leetcode.com/problems/unique-paths/#/solutions问题描述:有一个m*n的矩阵,从左上角走到右下角,每次只能走一步,(要么向右走,要么向下走),问有多少中走法。思路解析:DP 建立一个数组g[][]用来存储走到每个位置的步数, 假如,从中间取一个位置,该位置一定是有他的左面和上面过来的,所以他的步数 g[i][j]=g[i-1][j]+g原创 2017-05-22 19:24:11 · 164 阅读 · 0 评论 -
leetcode-70. Climbing Stairs(DP)
https://leetcode.com/problems/climbing-stairs/#/description问题描述:楼梯高为n,每次可以爬1或2个台阶问爬到最顶层有多少种方法。思路解析:简单的递归问题。代码如下:public class Solution { public int climbStairs(int n) { int[] num =new int[n];原创 2017-05-30 21:16:02 · 175 阅读 · 0 评论 -
leetcode-78. Subsets(非重复元素子集合)
https://leetcode.com/problems/subsets/#/description问题描述:给定一个没有重复元素的数组求所有子集。Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.原创 2017-05-15 22:28:42 · 290 阅读 · 0 评论 -
leetcode-86. Partition List
https://leetcode.com/problems/partition-list/#/description 问题描述:按照指定的值x,将链表分开,将小于x的排序放在x的前面,大于等于x的节点按顺序放在x的后面。思路解析:重新创建两个链表,一个用来按顺序存放小于x的结点,另一个用来按顺序存放大于等于x的结点。最后将两个链表合并。代码如下:/** * Definition for sing原创 2017-05-28 23:18:28 · 209 阅读 · 0 评论 -
leetcode-53. Maximum Subarray
https://leetcode.com/problems/maximum-subarray/#/description 问题描述:给定一个序列,求最长子串使其和最大。思路解析:两种方法。一种就是常规,另一种DP,当指针i指向该元素值时,将该元素之前的和保存在数组中,然后判断,dp[i-1]是否小于0,若小于0,则将将其逻辑上重置0,加nums[i],如下: dp[i]=nums[i]+(dp原创 2017-05-14 09:36:08 · 204 阅读 · 0 评论 -
leetcode-83. Remove Duplicates from Sorted List(删除有序链表中的重复元素)
https://leetcode.com/problems/remove-duplicates-from-sorted-list/#/description问题描述: Given a sorted linked list, delete all duplicates such that each element appear only once.For example, Given 1->1->原创 2017-05-24 16:49:04 · 174 阅读 · 0 评论 -
leetcode-33. Search in Rotated Sorted Array
https://leetcode.com/problems/search-in-rotated-sorted-array/#/description 问题描述:假设有一个升序的数组围绕着一个固定的轴旋转,给定一个值,若改值在数组中,返回索引值,不在则返回-1;Suppose an array sorted in ascending order is rotated at some pivot un原创 2017-05-13 21:56:09 · 149 阅读 · 0 评论 -
leetcode-35. Search Insert Position
https://leetcode.com/problems/search-insert-position/#/description Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it we原创 2017-05-12 19:17:43 · 140 阅读 · 0 评论 -
leetcode-19. Remove Nth Node From End of List(删除链表倒数第N个节点)
https://leetcode.com/problems/remove-nth-node-from-end-of-list/#/description假设有一个结构体代表每个链表中的节点 struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {原创 2017-05-23 19:53:30 · 239 阅读 · 0 评论 -
leetcode-26. Remove Duplicates from Sorted Array
Easy! https://leetcode.com/problems/remove-duplicates-from-sorted-array/#/description去除有序表中重复的元素问题描述: Given a sorted array, remove the duplicates in place such that each element appear only once and原创 2017-05-12 17:07:34 · 150 阅读 · 0 评论 -
leetcode-566. Reshape the Matrix
https://leetcode.com/problems/reshape-the-matrix/#/description 一道简单的题目,重塑矩阵,按照输入的行数和列数改造矩阵,但是新改造的顺序需要和原矩阵按行遍历的顺序相同。需要判断改造的矩阵是否合理。问题描述: In MATLAB, there is a very useful function called ‘reshape’, whi原创 2017-05-12 16:34:44 · 413 阅读 · 0 评论 -
leetcode-88. Merge Sorted Array
https://leetcode.com/problems/merge-sorted-array/#/description问题描述:将两个有序的数组,合并成一个有序数组,(将第二个合并到第一个里面)。代码如下:public class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) {原创 2017-05-23 17:00:52 · 166 阅读 · 0 评论 -
leetcode-18. 4Sum
https://leetcode.com/problems/4sum/#/description 问题描述: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the arra原创 2017-05-11 23:28:39 · 151 阅读 · 0 评论 -
leetcode-34. Search for a Range
https://leetcode.com/problems/search-for-a-range/#/description问题描述: 一个升序排列的数组,又重复值的那种,制定一个target,找到包含这个target的起始和终止位置,还是使用这般查找,分区间。代码如下:public class Solution { public int[] searchRange(int[] nums,原创 2017-05-14 10:47:03 · 178 阅读 · 0 评论 -
leetcode-82. Remove Duplicates from Sorted List II(删除有序列表中所有重复元素一个不留)
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/#/description问题描述:重复的元素全都删除,一个不留。思路解析:设立两个指针,一个滑动指向重复元素最后一个。当两个元素相连则同时向后滑动,否则相连。代码如下:/** * Definition for singly-linked list. * pub原创 2017-05-24 22:27:59 · 159 阅读 · 0 评论 -
leetcode-206. Reverse Linked List(单链表转置)
https://leetcode.com/problems/reverse-linked-list/#/description思路解析:头插法代码如下:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(in原创 2017-05-25 21:44:50 · 220 阅读 · 0 评论 -
leetcode-24. Swap Nodes in Pairs
https://leetcode.com/problems/swap-nodes-in-pairs/#/description问题描述:将每两个相邻的结点交换 Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return原创 2017-05-28 22:27:28 · 189 阅读 · 0 评论 -
leetcode-15. 3Sum
https://leetcode.com/problems/3sum/#/description 问题描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the s原创 2017-05-09 11:49:11 · 193 阅读 · 0 评论 -
leetcode-61. Rotate List
https://leetcode.com/problems/rotate-list/#/description问题描述: Given a list, rotate the list to the right by k places, where k is non-negative.For example: Given 1->2->3->4->5->NULL and k = 2, return原创 2017-05-26 22:59:52 · 136 阅读 · 0 评论