算法笔记
文章平均质量分 54
卓尔货币
这个作者很懒,什么都没留下…
展开
-
二.Java栈与队列的实现:
栈和队列是两种相像的经典的数据类型,都是对数据添加删除做限制的线性表。其不同之处就在于出栈(pop)和出列(dequeue)的方式。栈是先进后出,队列是先进先出。<1>Java虽说没有所谓的指针,但其实对于每个对象的引用就是指针,所以,栈可以用链表来实现:public class Stack { Node first = null; private class Node {//定义内原创 2017-12-02 23:15:55 · 151 阅读 · 0 评论 -
LeetCode 17 — Letter Combinations of a Phone Number(电话号码的字母组合)
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is giv...原创 2018-05-26 23:23:56 · 237 阅读 · 0 评论 -
LeetCode 18 — 4Sum(四数之和)
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of tar...原创 2018-05-26 23:27:34 · 459 阅读 · 0 评论 -
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-&gt;2-&gt;3-&gt;4-&gt;5, and n = 2. After removing the second node from the end, th...原创 2018-05-26 23:32:10 · 153 阅读 · 0 评论 -
LeetCode 20 — Valid Parentheses(有效的括号)
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of b...原创 2018-05-28 21:21:57 · 145 阅读 · 0 评论 -
LeetCode 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.Example: Input: 1-&gt;2-&gt;4, 1-&gt;3-&gt;4 Output: 1-...原创 2018-05-28 21:41:34 · 97 阅读 · 0 评论 -
LeetCode 22 — Generate Parentheses(括号生成)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is: [ “((()))”, “(()())”, “(())()”, “()((...原创 2018-05-28 21:47:55 · 141 阅读 · 0 评论 -
LeetCode 23 — Merge k Sorted Lists(合并K个排序链表)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.Example: Input: [ 1-&gt;4-&gt;5, 1-&gt;3-&gt;4, 2-&gt;6 ] Output: 1-&gt;1-&gt;2-&am原创 2018-05-28 22:08:47 · 316 阅读 · 0 评论 -
LeetCode 24 — Swap Nodes in Pairs(两两交换链表中的节点)
Given a linked list, swap every two adjacent nodes and return its head.Example: Given 1-&gt;2-&gt;3-&gt;4, you should return the list as 2-&gt;1-&gt;4-&gt;3. Note: Your algorithm should use only ...原创 2018-05-28 22:13:00 · 162 阅读 · 0 评论 -
LeetCode 25 — Reverse Nodes in k-Group(k个一组翻转链表)
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of ...原创 2018-06-01 14:04:29 · 305 阅读 · 0 评论 -
LeetCode 26 — Remove Duplicates from Sorted Array(删除排序数组中的重复项)
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifyi...原创 2018-06-01 14:12:26 · 789 阅读 · 0 评论 -
LeetCode 27 — Remove Element(移除元素)
Given an array nums and a value val, 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 by modifying the input arra...原创 2018-06-01 14:17:05 · 203 阅读 · 0 评论 -
LeetCode 28 — Implement strStr()(实现strStr())
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1: Input: haystack = “hello”, needle = “ll” Output: 2 Example ...原创 2018-06-01 14:21:06 · 436 阅读 · 0 评论 -
LeetCode 29 — Divide Two Integers(两数相除)
Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator. Return the quotient after dividing dividend by divisor. The integer division sho...原创 2018-06-01 14:27:52 · 801 阅读 · 0 评论 -
LeetCode 30 — Substring with Concatenation of All Words(与所有单词相关联的字串)
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and wi...原创 2018-06-01 14:40:54 · 472 阅读 · 0 评论 -
LeetCode 31 — Next Permutation(下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible o...原创 2018-06-01 14:54:07 · 440 阅读 · 0 评论 -
LeetCode 32 — Longest Valid Parentheses(最长有效括号)
Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.Example 1:Input: "(()"Output: 2Explanation: The longest valid pa...原创 2018-11-14 16:51:32 · 100 阅读 · 0 评论 -
LeetCode 16 — 3Sum Closest(最接近的三数之和)
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would ...原创 2018-05-26 23:16:55 · 301 阅读 · 0 评论 -
LeetCode 15 — 3Sum(三数之和)
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not cont...原创 2018-05-26 23:13:38 · 272 阅读 · 0 评论 -
一.快速并查集(Union_Find)
早就说要开始系统的学算法了,可总是被这个事那个事所拖累,从今天开始,认真拿着《算法》跟着coursera上的课程学算法。算法的设计总是由API的设计开始,先将需要解决的问题抽象化,对需要的功能写出相应的API,然后在考虑解决这个问题用什么数据结构最好,最后再开始实现各个API。 关于Union_Find问题,判断两个对象之间是否联通的一个抽象的想法便是将union的对象放进同一个集合中,查询时若原创 2017-12-02 23:21:38 · 348 阅读 · 0 评论 -
LeetCode 1 — Two Sum(两数之和)
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where...原创 2018-05-16 23:07:14 · 164 阅读 · 0 评论 -
LeetCode 2 — Add Two Numbers(两数相加)
分析 两数字之和加上上一位的进位,最后一位考虑是否进位,进位链表长度再加一。c++实现/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} ...原创 2018-05-16 23:12:30 · 85 阅读 · 0 评论 -
LeetCode 3 — Longest Substring Without Repeating Characters(无重复字符的最长子串)
分析 构建一个set,将字符依次放入set,重复就跳出,遍历完返回最大的set元素个数。c++实现class Solution {public: int lengthOfLongestSubstring(string s) { set&amp;amp;lt;char&amp;amp;gt; s1; int length = 0; for (int i = 0...原创 2018-05-16 23:18:28 · 118 阅读 · 0 评论 -
leetcode题解
欢迎访问我的个人网站:http://lizhiis.me第一轮刷题,始于18年5月15日。持续更新…以下为CSDN题解链接: 1.Two Sum(两数之和)2.Add Two Numbers(两数相加)3.Longest Substring Without Repeating Characters(无重复字符的最长子串)4.Median of Two Sorted A...原创 2018-05-16 23:28:24 · 193 阅读 · 0 评论 -
LeetCode 6 — ZigZag Conversion(Z字形变换)
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I...原创 2018-05-17 21:50:12 · 349 阅读 · 0 评论 -
LeetCode 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)).Example 1: nums1 = [1, 3]...原创 2018-05-17 21:57:00 · 136 阅读 · 0 评论 -
LeetCode 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 1: Input: “babad” Output: “bab” Note: “aba” is also a valid answer. Exa...原创 2018-05-17 22:02:07 · 123 阅读 · 0 评论 -
LeetCode 7 — Reverse Integer(反转整数)
Given a 32-bit signed integer, reverse digits of an integer.Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are deali...原创 2018-05-17 22:05:43 · 122 阅读 · 0 评论 -
LeetCode 8 — String to Integer (atoi)(字符串转整数 (atoi))
Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from thi...原创 2018-05-18 23:39:30 · 143 阅读 · 0 评论 -
LeetCode 9 — Palindrome Number(回文数)
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Expl...原创 2018-05-18 23:46:55 · 90 阅读 · 0 评论 -
LeetCode 10 — Regular Expression Matching(正则表达式匹配)
Given an input string (s) and a pattern (p), implement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element. ...原创 2018-05-26 22:33:29 · 183 阅读 · 0 评论 -
LeetCode 11 — Container With Most Water(盛最多水的容器)
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two li...原创 2018-05-26 22:53:36 · 128 阅读 · 0 评论 -
LeetCode 12 — Integer to Roman(整数转罗马数字)
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D ...原创 2018-05-26 22:57:27 · 318 阅读 · 0 评论 -
LeetCode 13 — Roman to Integer(罗马数字转整数)
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D ...原创 2018-05-26 23:00:25 · 497 阅读 · 0 评论 -
LeetCode 14 — Longest Common Prefix(最长公共前缀)
Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string “”.Example 1: Input: [“flower”,”flow”,”flight”] Output:...原创 2018-05-26 23:07:15 · 143 阅读 · 0 评论 -
第九届蓝桥杯国赛JavaB组——整理玩具
小明有一套玩具,一共包含NxM个部件。这些部件摆放在一个包含NxM个小格子的玩具盒中,每个小格子中恰好摆放一个部件。每一个部件上标记有一个0~9的整数,有可能有多个部件标记相同的整数。小明对玩具的摆放有特殊的要求:标记相同整数的部件必须摆在一起,组成一个矩形形状。如以下摆放是满足要求的:0002200033444441224412244122330123456789以下摆放...原创 2019-05-21 18:31:43 · 232 阅读 · 0 评论