【【Leetcode】125. 验证回文串(Valid Palindrome)】教程文章相关的互联网学习教程文章

LeetCode 234 Palindrome Linked List【代码】

要点理解 :回文串是中心对称的 /*** @Description 链表类需要自定义* @Date 2019/8/11 17:40**/class ListNode {int val;ListNode next;public ListNode(int x){val=x;}}/*** @Description* @Date 2019/8/11 17:40**/publicclass PalindromeLinkedList {publicboolean isPalindrome(ListNode head) {if (head == null || head.next == null) {returntrue;}ListNode prev = null;ListNode slow = head;ListNode fast = head;// 直到...

LeetCode之“动态规划”:Interleaving String【代码】

题目链接  题目要求:   Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.  For example,  Given:  s1 = "aabcc",  s2 = "dbbca",  When s3 = "aadbbcbcac", return true.  When s3 = "aadbbbaccc", return false.  这道题的难度还是很大。  在GeeksforGeeks上举了一个例子来说明什么是Interleaving String:Input: str1 = "AB", str2 = "CD" Output:ABCDACBDACDBCABDCADBCD...

[LeetCode] 42. Trapping Rain Water_hard tag: Two Pointers【代码】【图】

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.Example:Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6这个题目思路类似于木桶原理, 中间能收集到的水, 取决于左右...

【leetcode】 Longest Substring Without Repeating Characters【图】

题目:给定一个字符串,返回该串没有重复字符的最长子串。分析:1)子串:子串要求是连续的。2)无重复,出现重复就断了,必须从新的位置开始。而新的位置就是重复字符第一次出现位置的下一个位置。3)整个串可能没有一处重复。那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置。初始化为-1,表示都...

【leetcode】合并两个有序链表【代码】

struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){struct ListNode* head = (struct ListNode*)calloc(sizeof(struct ListNode),1);struct ListNode* p = head;while(l1 && l2){struct ListNode* temp = (struct ListNode*)calloc(sizeof(struct ListNode),1);if(l1->val <= l2->val){temp->val = l1->val;l1 = l1->next;}else{temp->val = l2->val;l2 = l2->next;}p->next = temp;p = p->next;}p->next...

LeetCode3:Longest Substring Without Repeating Characters【代码】【图】

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.解法这道题最直观的做法是从每一个元素开始,寻找以它为起始的无重复的字符串,但是时间复杂度度是O(N^3),明显不符合要求。它大量的时间都花在...

【leetcode】17. Letter Combinations of a Phone Number【代码】

题目描述:Given a digit string, return all possible letter combinations that the number could represent.解题分析:回溯法的典型应用,用一个数据结构表示出按键与其表示字母的对应关系,直接用回溯法做即可。具体代码: 1publicclass Solution {2privatestatic List<String> results=new ArrayList<String>();3privatestatic String[] array= {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};4publicstatic Lis...

[LeetCode] Implement Queue using Stacks

Implement Queue using Stacks Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty() -- Return whether the queue is empty.Notes:You must use only standard operations of a stack -- which means only pushto top, peek/pop from top, size,and is empty operations a...

[LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点【代码】

Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.For exampl...

Leetcode: Valide Binary Search Tree【代码】

Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node‘s key. The right subtree of a node contains only nodes with keys greater than the node‘s key. Both the left and right subtrees must also be binary search trees.思维难度:87,开始我以为只要每个子树都满足左子树根<根<...

[leetcode] Permutations【代码】

PermutationsGiven a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].思路:dfs搜索。visited[]数组表示对应的数字是否已经使用,visited[i]==true,表示num[i]已经使用。一直递归搜索,寻找没有使用过的数字,组成全排列。这里给visited数组赋值的时候,开始写成了sizeof(int),提交出现runtime error...

Leetcode 178. Rank Scores (Database)【代码】

select Score, @Counter := @Counter + 1 as rank from Scores, (select @Counter := 0) as C order by Score desc; 排序2. 相同分数排名相同,排名第n名表示前面有n-1个人,而不是n-1个分数。 Score Rank 100 1 99 2 99 2 98 4 90 5# Write your MySQL query statement below select S1.Score, ( select count(Score) + 1 from Scores S2 where S1.Score < S2.Score ) as Rank from Scores S1 ...

LeetCode 237. 删除链表中的节点 Delete Node in a Linked List【代码】【图】

复制后一个节点,跳过后一个节点。/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:void deleteNode(ListNode* node) {node->val = node->next->val;node->next = node->next->next;} };

LeetCode : 19. Remove Nth Node From End of List 移除链表中距离尾部N的节点【代码】

试题 Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. Follow up: Could you do this in one pass? 代码 两个指针,一个先走N步。 /*** Definition for singly-linked list.* public class ListNode {* ...

LeetCode 181. Employees Earning More Than Their Managers (sql)【代码】

181. Employees Earning More Than Their Managers Easy sql schema The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.+----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 6000...