![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
刷题
Queen_Bey
这个作者很懒,什么都没留下…
展开
-
13. Roman to Integer ★
题目内容:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999. 题目分析:罗马数字向阿拉伯数字的转换情况如下:1、M=1000 D=500 C=100 L=50 X=10 V=5 I=12、若小的罗马符号出现在大的罗原创 2017-03-25 21:13:12 · 221 阅读 · 0 评论 -
14. Longest Common Prefix ★
题目内容:Write a function to find the longest common prefix string amongst an array of strings 题目分析:本题目利用字符串数组中的一个字符串作为参考,数组中的所有字符串都和该字符串进行对比,找出所有字符串开头出现字符串的交集。其中用到的是字符串的indexOf函数。定义和用法indexOf() 方原创 2017-03-25 21:22:03 · 287 阅读 · 0 评论 -
20. Valid Parentheses ★
题目内容:Given a string containing just the characters '(', ')','{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all原创 2017-03-25 21:23:44 · 197 阅读 · 0 评论 -
21. Merge Two Sorted Lists★
题目内容:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题目分析:本题是要合并两个已经有序的单链表,思路很简单,有两种方法:非递归和递归。 题目代原创 2017-03-25 21:25:08 · 233 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array★
题目内容:Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.Do not allocate extra space for another array, you must do this in plac原创 2017-03-25 21:26:15 · 205 阅读 · 0 评论 -
1. Two Sum★
题目内容:Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the原创 2017-03-25 21:02:48 · 227 阅读 · 0 评论 -
7. Reverse Integer Add to List★
题目内容:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321 题目分析:反转的方法很简单,重点在于判断溢出的问题,下面给出了两种方法。方法一: 判断溢出方法:在执行完int newResult=result*10+tail语句后,紧接着进行原创 2017-03-25 21:10:04 · 217 阅读 · 0 评论 -
27. Remove Element★
题目内容:Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant me原创 2017-03-26 17:30:41 · 206 阅读 · 0 评论 -
28. Implement strStr() ★
题目内容:Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 题目分析:关系到两个字符串一般会用到两个指针,分别用来遍历两个字符串。判断条件从代码中显而易见。题目代原创 2017-03-26 22:53:40 · 197 阅读 · 0 评论 -
35. Search Insert Position★
题目内容: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 were inserted in order.You may assume no duplicates in the原创 2017-03-26 23:14:12 · 197 阅读 · 0 评论 -
38. Count and Say ★
题目内容:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off a原创 2017-03-26 23:24:46 · 191 阅读 · 0 评论 -
2、Add Two Numbers
题目内容:You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and原创 2017-09-07 12:03:38 · 176 阅读 · 0 评论 -
54. Spiral Matrix
public class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> res = new ArrayList<Integer>(); if (matrix.length == 0) { ...原创 2018-03-14 23:29:10 · 112 阅读 · 0 评论 -
55. Jump Game
public boolean canJump(int[] A) { int max = 0; for(int i=0;i<A.length;i++){ if(i>max) {return false;} max = Math.max(A[i]+i,max); } return true;}...原创 2018-03-15 00:05:40 · 96 阅读 · 0 评论 -
56. Merge Intervals
public List<Interval> merge(List<Interval> intervals) { if (intervals.size() <= 1) return intervals; // Sort by ascending starting point using an anonymous Comparator ...原创 2018-03-15 00:18:55 · 109 阅读 · 0 评论 -
62. Unique Paths
运用动态规划的方法。public class Solution {public int uniquePaths(int m, int n) { int[][] grid = new int[m][n]; for(int i = 0; i<m; i++){ for(int j = 0; j<n; j++){ if(i==0||j...原创 2018-03-15 00:24:43 · 116 阅读 · 0 评论 -
66. Plus One
public int[] plusOne(int[] digits) { int n = digits.length; for(int i=n-1; i>=0; i--) { if(digits[i] < 9) { digits[i]++; return digits; } ...原创 2018-03-15 00:37:35 · 125 阅读 · 0 评论 -
69. Sqrt(x)
public int sqrt(int x) { if (x == 0) return 0; int left = 1, right = Integer.MAX_VALUE; while (true) { int mid = left + (right - left)/2; if (mid > x/mid) { ...原创 2018-03-15 12:40:39 · 111 阅读 · 0 评论 -
70. Climbing Stairs
爬梯子问题使用的是菲波那切数列,也可以考虑叫做动态规划。public int climbStairs(int n) { // base cases if(n <= 0) return 0; if(n == 1) return 1; if(n == 2) return 2; int one_step_before = 2; int...原创 2018-03-15 12:44:53 · 176 阅读 · 0 评论 -
73. Set Matrix Zeroes
在结构内进行修改的时候,注意选择合适的地方进行标记修改。public class Solution {public void setZeroes(int[][] matrix) { boolean fr = false,fc = false; for(int i = 0; i < matrix.length; i++) { for(int j = 0; j ...原创 2018-03-15 13:06:50 · 117 阅读 · 0 评论 -
75. Sort Colors
public void sortColors(int[] nums) { // 1-pass int p1 = 0, p2 = nums.length - 1, index = 0; while (index <= p2) { if (nums[index] == 0) { nums[index] = nums[p1]; ...原创 2018-03-15 13:23:05 · 130 阅读 · 0 评论 -
76. Minimum Window Substring
string minWindow(string s, string t) { vector<int> map(128,0); for(auto c: t) map[c]++; int counter=t.size(), begin=0, end=0, d=INT_MAX, head=0; while(end<s.size()...原创 2018-03-15 13:43:41 · 127 阅读 · 0 评论 -
88. Merge Sorted Array
从尾部进行归并。public void merge(int A[], int m, int B[], int n) { int i=m-1, j=n-1, k=m+n-1; while (i>-1 && j>-1) A[k--]= (A[i]>B[j]) ? A[i--] : B[j--]; while (j>-1) A[k...原创 2018-03-15 14:20:04 · 101 阅读 · 0 评论 -
KMP算法
我们首先用一个图来描述kmp算法的思想。在字符串O中寻找f,当匹配到位置i时两个字符串不相等,这时我们需要将字符串f向前移动。常规方法是每次向前移动一位,但是它没有考虑前i-1位已经比较过这个事实,所以效率不高。事实上,如果我们提前计算某些信息,就有可能一次前移多位。假设我们根据已经获得的信息知道可以前移k位,我们分析移位前后的f有什么特点。我们可以得到如下的结论:A段字符串是f的一个前缀。B段字...转载 2018-03-09 21:00:11 · 104 阅读 · 0 评论 -
78. Subsets
求子集、求排列的方法是用回溯法,代码如下:This structure might apply to many other backtracking questions, but here I am just going to demonstrate Subsets, Permutations, and Combination Sum.Subsets : https://leetcode.com/...原创 2018-03-16 12:50:34 · 132 阅读 · 0 评论 -
91. Decode Ways
public class Solution { public int numDecodings(String s) { int n = s.length(); if (n == 0) return 0; int[] memo = new int[n+1]; memo[n] = 1; memo[n-1] =...原创 2018-03-16 13:07:55 · 137 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal
二叉树的前序遍历、中序遍历、后序遍历,其中包括递归的方法和非递归(利用栈结构)的方法。public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<Integer>(); Stack<TreeNode> stack = n...原创 2018-03-16 15:41:51 · 108 阅读 · 0 评论 -
98. Validate Binary Search Tree
只需检验二叉树的中序遍历是不是递增的就可以了。public boolean isValidBST(TreeNode root) { if (root == null) return true; Stack<TreeNode> stack = new Stack<>(); TreeNode pre = null; while (root != null || ...原创 2018-03-16 16:24:32 · 92 阅读 · 0 评论 -
101. Symmetric Tree
使用递归的方法。Recursive–400ms:public boolean isSymmetric(TreeNode root) { return root==null || isSymmetricHelp(root.left, root.right);}private boolean isSymmetricHelp(TreeNode left, TreeNode right){...原创 2018-03-16 16:54:19 · 120 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal
注意使用一个变量记录每一层的节点数量,使用for循环一次遍历出一层中的所有结点。public class Solution { public List<List<Integer>> levelOrder(TreeNode root) { Queue<TreeNode> queue = new LinkedList<TreeNode>...原创 2018-03-16 17:04:51 · 115 阅读 · 0 评论 -
103. Binary Tree Zigzag Level Order Traversal
深度优先DFS算法public class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> sol = new ArrayList<>(); travel(roo...原创 2018-03-16 17:18:56 · 115 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
使用用递归的方法public int maxDepth(TreeNode root) { if(root==null){ return 0; } return 1+Math.max(maxDepth(root.left),maxDepth(root.right)); }DFSpublic int maxDepth(TreeNod...原创 2018-03-16 17:36:14 · 127 阅读 · 0 评论 -
105. Construct Binary Tree from Preorder and Inorder Traversal
构建树的时候,递归是经常使用的方法。public TreeNode buildTree(int[] preorder, int[] inorder) { return helper(0, 0, inorder.length - 1, preorder, inorder);}public TreeNode helper(int preStart, int inStart, int inEnd,...原创 2018-03-16 17:51:50 · 117 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b", with the l...原创 2018-03-10 19:45:19 · 86 阅读 · 0 评论 -
4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).两个排序数组的二分法查找public ...原创 2018-03-10 20:14:22 · 97 阅读 · 0 评论 -
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:Input: "babad"Output: "bab"Note: "aba" is also a valid answer.Example:Input: ...原创 2018-03-10 20:30:52 · 92 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree
public TreeNode sortedArrayToBST(int[] num) { if (num.length == 0) { return null; } TreeNode head = helper(num, 0, num.length - 1); return head;}public TreeNode helper(int[] num, in...原创 2018-03-16 21:05:15 · 112 阅读 · 0 评论 -
6. ZigZag Conversion
public String convert(String s, int nRows) { char[] c = s.toCharArray(); int len = c.length; StringBuffer[] sb = new StringBuffer[nRows]; for (int i = 0; i < sb.length; i++) sb[i] = new...原创 2018-03-10 20:55:17 · 121 阅读 · 0 评论 -
7. Reverse Integer
这个是数字反转的方法。public int reverse(int x){ int result = 0; while (x != 0) { int tail = x % 10; int newResult = result * 10 + tail; if ((newResult - tail) / 10 != result) ...原创 2018-03-10 21:11:52 · 97 阅读 · 0 评论 -
9. Palindrome Number
public boolean isPalindrome(int x) { if (x<0 || (x!=0 && x%10==0)) return false; int rev = 0; while (x>rev){ rev = rev*10 + x%10; x = x/10; } return (x==rev || x==...原创 2018-03-10 21:29:50 · 101 阅读 · 0 评论