leetcode bug free

---倒序查看,不包含jiuzhang ladders中出现过的题。

2 Longest Palindromic Substring --- NOT BUG FREE

求一个字符串中的最长回文子串。

Example:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

Example:

Input: "cbbd"

Output: "bb"

分析1:中心扩展,以每个字符为中心向外扩展,找到最长回文串。时间复杂度O(n^2),空间复杂度O(1)。

 1 public class Solution {
 2     public String longestPalindrome(String s) {
 3         int length = 0;
 4         String res = "";
 5
 6         //要考虑回文串是abba和aba两种情况
 7         for (int i = 0; i < 2 * s.length() - 1; i++) {
 8             int p1 = i % 2 == 0 ? i / 2 - 1 : i / 2;
 9             int p2 = i % 2 == 0 ? i / 2 + 1 : i / 2 + 1;
10             int len = i % 2 == 0 ? 1 : 0;
11             while (p1 >= 0 && p2 < s.length() && s.charAt(p1) == s.charAt(p2)) {
12                 len += 2;
13                 p1--;
14                 p2++;
15             }
16             if (len > length) {
17                 length = len;
18                 res = s.substring(p1 + 1, p2);
19             }
20         }
21         return res;
22     }
23 }

分析2:Manacher算法。时间复杂度O(n),空间复杂度O(1)。

1 Longest Substring Without Repeating Characters --- not bug free

给一个字符串,找出其中不包含重复字符的最长子串的长度。

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

分析:hash表记录每个字符最近出现的索引,p1指针表示以当前字符为末尾的最长不重复子串的左端点。时间复杂度O(n)。

 1 public class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         Map<Character, Integer> map = new HashMap<Character, Integer>();
 4         int res = 0;
 5         int p1 = 0;
 6         int p2 = 0;
 7
 8         for (; p2 < s.length(); p2++) {
 9             char c = s.charAt(p2);
10             if (map.containsKey(c) && p1 <= map.get(c)) {
11                 p1 = map.get(c) + 1;
12             }
13             map.put(c, p2);
14             res = Math.max(res, p2 - p1 + 1);
15         }
16         return res;
17     }
18 }

时间: 2024-08-06 16:06:11

leetcode bug free的相关文章

Leetcode的bug测试用例 ?? jump game

之所以说leetcode的测试用例有问题,是因为我刚开始理解错了题意,写下了如下的错误的代码.但是却AC了. 错误代码为: bool canJump(int A[], int n) { if(n == 0) return true; int sum = 0; //记录当前的最远距离 int i = 0; while(i < n) { if(A[i] != 0) { i+= A[i]; } else { break; } } if(i >= n-1) return true; return fa

发现LeetCode大bug一个!!!!!!

1. Two Sum 第36行本来应该是if (nodes.get(left).key < nodes.get(right).key) { 一不小心误写成了if (nodes.get(left).key < nodes.get(right).value) { 竟然A了!!!! 1 class Node{ 2 int key; 3 int value; 4 public Node(int key, int value) { 5 this.key = key; 6 this.value = val

Leetcode 还未解决的bug

27. Remove Element val = 1 nums = [1,1,2,3] for i in nums: if i == val: nums.remove(i) a = nums result : val = 1 nums = [1,1,2,3] for i in nums[:]: if i == val: nums.remove(i) a = nums result: 不用remove的解法: class Solution(object): def removeElement(se

LeetCode 28. Implement strStr()

https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 字符串简单题.一定要写的没BUG. 如果不在函数最后一行写return语句的话,LeetCode会出RUNTIME ERROR. Line 27: co

LeetCode: Gas Station 解题报告

Gas Station There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journ

leetcode刷题全纪录(持续更新)

338. Counting Bits 原题链接https://leetcode.com/problems/counting-bits/ 自己的思路:Integer.bitCount()方法,但是原题并不推荐使用内嵌方法 bug free:遇到偶数时,其1的个数和该偶数除以2得到的数字的1的个数相同,遇到奇数时,其1的个数等于该奇数除以2得到的数字的1的个数再加1 public int[] countBits(int num) { int[] result = new int[num+1]; fo

LeetCode之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 valid but "(]" and "([)]&

[LeetCode][Java][JavaScript]Counting Bits

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

LeetCode: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.