![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode简单一刷
AKA造梦艺术家
这个作者很懒,什么都没留下…
展开
-
849. Maximize Distance to Closest Person
class Solution { public int maxDistToClosest(int[] seats) { int i, j, res = 0, n = seats.length; for (i = j = 0; j < n; ++j) if (seats[j] == 1) { ...原创 2018-09-24 13:54:54 · 138 阅读 · 0 评论 -
521. Longest Uncommon Subsequence I
给定两个字符串,你需要从这两个字符串中找出最长的特殊序列。最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列)。子序列可以通过删去字符串中的某些字符实现,但不能改变剩余字符的相对顺序。空序列为所有字符串的子序列,任何字符串为其自身的子序列。输入为两个字符串,输出最长特殊序列的长度。如果不存在,则返回 -1。示例 :输入: “aba”, “cdc” ...原创 2018-08-15 11:12:49 · 497 阅读 · 0 评论 -
434. Number of Segments in a String
统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。请注意,你可以假定字符串里不包括任何不可打印的字符。示例:输入: “Hello, my name is John” 输出: 5打完,!*这些特殊符号之后都会有个空格所以不用管,只算空格就otmk,此题题意有点trickyclass Solution { public int countSegments(St...原创 2018-08-15 10:41:30 · 121 阅读 · 0 评论 -
383. Ransom Note
题目要求找出magazine中是否有足够的字符for ransomnote,有则true 没则false;重复的字母magazine也需要提供class Solution { public boolean canConstruct(String ransomNote, String magazine) { if(ransomNote.length() &gt; mag...原创 2018-08-15 12:30:37 · 101 阅读 · 0 评论 -
345. Reverse Vowels of a String
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。示例 1:输入: “hello” 输出: “holle” 示例 2:输入: “leetcode” 输出: “leotcede” 说明: 元音字母不包含字母”y”。考察双指针class Solution { public String reverseVowels(String s) { ...原创 2018-08-15 09:46:36 · 130 阅读 · 0 评论 -
217. Contains Duplicate
原题 Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every elem...原创 2018-08-12 16:01:01 · 110 阅读 · 0 评论 -
58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defin...原创 2018-08-07 21:01:05 · 68 阅读 · 0 评论 -
53. Maximum Subarray重点掌握
Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has ...原创 2018-08-07 20:16:03 · 72 阅读 · 0 评论 -
20. Valid Parentheses
Given a string containing justthe characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. The brackets must close in the correct order, “()” and “()[]{}” are all valid but...原创 2018-08-07 17:16:06 · 67 阅读 · 0 评论 -
387. First Unique Character in a String
string leetcode 的话 第一个唯一字符是l, return index 0; blaheblahd的话 return index 4;class Solution { public int firstUniqChar(String s) { int[] a = new int[26]; //26个字母 for(int i = 0;...原创 2018-08-15 18:01:20 · 113 阅读 · 0 评论 -
66. Plus One
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits a...原创 2018-08-08 18:10:21 · 73 阅读 · 0 评论 -
747. Largest Number At Least Twice of Others
class Solution { public int dominantIndex(int[] nums) { int max = 0 ; for(int i = 0 ; i < nums.length; i++){ if(nums[i] > nums[max]...原创 2018-09-24 13:53:32 · 140 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array
class Solution { public List<Integer> findDisappearedNumbers(int[] nums) { List<Integer> list = new ArrayList(); for(int x: nums){ int val = Math.abs(x) ...原创 2018-09-14 06:17:50 · 147 阅读 · 0 评论 -
888. Fair Candy Swap
class Solution { public int[] fairCandySwap(int[] A, int[] B) { //The mathmatics way we have is: // y = x + (SumB - SumA) / 2 int sumA = 0, sumB = 0; for(int x: ...原创 2018-09-14 04:52:54 · 113 阅读 · 0 评论 -
724. Find Pivot Index
class Solution { public int pivotIndex(int[] nums) { int leftsum = 0, sum = 0; //get the sum first, then check if the leftsum is equal to the rest of the array for(int i ...原创 2018-09-13 10:41:35 · 729 阅读 · 0 评论 -
896. Monotonic Array
class Solution { public boolean isMonotonic(int[] A) { return isIncreasing(A) || isDecreasing(A); } public boolean isIncreasing(int[] A){ for(int i = 0 ; i < A.lengt...原创 2018-09-13 10:39:57 · 166 阅读 · 0 评论 -
459. Repeated Substring Pattern
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。示例 1:输入: “abab”输出: True解释: 可由子字符串 “ab” 重复两次构成。 示例 2:输入: “aba”输出: False 示例 3:输入: “abcabcabcabc”输出: True解释: 可由子字符串 “abc” ...原创 2018-08-17 10:48:33 · 231 阅读 · 0 评论 -
557. Reverse Words in a String III
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。示例 1:输入: “Let’s take LeetCode contest” 输出: “s’teL ekat edoCteeL tsetnoc” 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。class Solution { public String ...原创 2018-08-18 16:03:21 · 399 阅读 · 0 评论 -
500. Keyboard Row
此题要求在input string array中找出能被一行键盘打出来的string,很有意思的一题,有很多amazing的解法:Solution 1:一行函数解爆它public String[] findWords(String[] words) { return Stream.of(words).filter(s -> s.toLowerCase().matches("[...原创 2018-08-13 14:37:33 · 111 阅读 · 0 评论 -
485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive ...原创 2018-08-11 21:06:19 · 97 阅读 · 0 评论 -
136. Single Number
题目: Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using ...原创 2018-08-11 21:04:32 · 80 阅读 · 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.构造一个新的NodeList temp,然后使用递归的方法去调用它。当l1当前节点的值大于l2当前节点的值时,...原创 2018-08-06 16:38:26 · 53 阅读 · 0 评论 -
414. Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).Example 1:Input: [3, 2, 1]...原创 2018-08-14 07:07:10 · 81 阅读 · 0 评论 -
70. Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?uncompleted原创 2018-08-10 15:31:44 · 83 阅读 · 0 评论 -
69M. Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x, where x is guaranteed to be a non-negative integer.Since the return type is an integer, the decimal digits are truncated and only...原创 2018-08-06 11:22:34 · 141 阅读 · 0 评论 -
27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn’t matter what you leave beyond the new length.cla...原创 2018-08-05 20:46:00 · 59 阅读 · 0 评论 -
26R. Remove Duplicates from Sorted Array
Remove Duplicates from Sorted ArrayGiven a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another ar...原创 2018-08-05 20:36:06 · 90 阅读 · 0 评论 -
7M. Reverse Integer
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer).Have you met this question in a real interview?Example Given x = 123, return 321Given x = -12...原创 2018-08-05 18:17:28 · 94 阅读 · 0 评论 -
290. Word Pattern
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-emptyword in str.Example...原创 2018-08-05 17:06:36 · 172 阅读 · 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 array....原创 2018-08-06 16:50:04 · 56 阅读 · 0 评论 -
415. Add Strings
Similar to binary add.Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.Note:The length of both num1 and num2 is < 5100. Both num1 and num2 ...原创 2018-08-14 10:52:20 · 88 阅读 · 0 评论 -
412. Fizz Buzz
写一个程序,输出从 1 到 n 数字的字符串表示。如果 n 是3的倍数,输出“Fizz”;如果 n 是5的倍数,输出“Buzz”;3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。class Solution { public List<String> fizzBuzz(int n) { List<String> ...原创 2018-08-14 11:28:54 · 87 阅读 · 0 评论 -
268. Missing Number
题目:Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.For example, Given nums = [0, 1, 3] return 2.Note: Your algorithm should r...原创 2018-08-11 19:39:52 · 90 阅读 · 0 评论 -
561. Array Partition I
题目描述: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large a...原创 2018-08-11 19:05:57 · 87 阅读 · 0 评论 -
283. Move Zeroes
题目 283.MoveZeroes Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12]...原创 2018-08-11 18:29:52 · 74 阅读 · 0 评论 -
628. Maximum Product of Three Numbers
问题描述: Given an integer array, find three numbers whose product is maximum and output the maximum product.Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: ...原创 2018-08-11 16:43:14 · 75 阅读 · 0 评论 -
520. Detect Capital
检测要求的string以下三种情况 valid,其余的情况返回false:GooglegoogleGOOGLEclass Solution { public boolean detectCapitalUse(String word) { char[] words = word.toCharArray(); int allupper...原创 2018-08-14 20:57:31 · 104 阅读 · 0 评论 -
551. Student Attendance Record I
找出input string中,如果有大于一个‘A‘和大于两个连续的‘L‘,则返回false;else return trueclass Solution { public boolean checkRecord(String s) { int maxabs = 0, maxlate = 0; for(int i = 0 ; i ...原创 2018-08-14 19:19:51 · 104 阅读 · 0 评论 -
788. Rotate Number
我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数。要求每位数字都要被旋转。如果一个数的每位数字被旋转以后仍然还是一个数字, 则这个数是有效的。0, 1, 和 8 被旋转后仍然是它们自己;2 和 5 可以互相旋转成对方;6 和 9 同理,除了这些以外其他的数字旋转以后都不再是有效的数字。现在我们有一个正整数 N, 计算从 1...原创 2018-08-14 18:24:44 · 489 阅读 · 0 评论 -
852. Peak Index in a Mountain Array
Let’s call an array A a mountain if the following properties hold:A.length >= 3 There exists some 0 < i < A.length - 1 such that A[0] < A[1] < … A[i-1] < A[i] > A[i+1] > … &...原创 2018-08-06 19:01:49 · 1007 阅读 · 0 评论