leetcode

leetcode:Valid Sudoku and Sudoku Solver

别来无恙 提交于 2020-03-11 15:37:26
Question 1(Valid Sudoku) : Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules . The Sudoku board could be partially filled, where empty cells are filled with the character '.' . Question 2(Sudoku Solver) : Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.' . You may assume that there will be only one unique solution. What‘s the difference between above two problems? The rule of Sudoku: 1. Each row must have the number 1-9 occuring just once.          2. Each column must have the number 1-9 occuring just once

【LeetCode】 59. Spiral Matrix II 螺旋矩阵 II(Medium)(JAVA)

♀尐吖头ヾ 提交于 2020-03-11 13:23:32
【LeetCode】 59. Spiral Matrix II 螺旋矩阵 II(Medium)(JAVA) 题目地址: https://leetcode.com/problems/spiral-matrix-ii/ 题目描述: Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题目大意 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。 解题方法 解法和螺旋矩阵 I 完全相同: 【LeetCode】 54. Spiral Matrix 螺旋矩阵(Medium)(JAVA) 1、每次一循环包括四部分,右下左上 2、特殊处理边界情况 class Solution { public int[][] generateMatrix(int n) { int[][] res = new int[n][n]; int cur = 1; int all = n * n; for (int i = 0; i <= n / 2; i++) { for (int j

三数之和 四数之和

余生长醉 提交于 2020-03-10 19:21:37
leetcode 15 三数之和 leetcode16 最接近的三数之和 leetcode 18 四数之和 这三道题目都是一个类型,主要就是利用有序数组与双指针的一些技巧; 首先给出一个基本模板,如何在一个有序数组中找到两数之和为一目标值target; int l = 0 , r = nums . size ( ) - 1 ; while ( l < r ) { if ( nums [ l ] + nums [ r ] == target ) { //根据不同的要求,进行一些必要的操作 //考虑是否去重 } else if ( nums [ l ] + nums [ r ] < target ) ++ l ; else -- r ; } 1.首先看leetcode 15 三数之和 class Solution { public : vector < vector < int >> threeSum ( vector < int > & nums ) { vector < vector < int >> res ; if ( nums . size ( ) < 3 ) return { } ; sort ( nums . begin ( ) , nums . end ( ) ) ; for ( int i = 0 ; i < nums . size ( ) - 2 ; ++ i )

【LeetCode】 43. Multiply Strings 字符串相乘(Medium)(JAVA)

旧巷老猫 提交于 2020-03-09 17:07:32
【LeetCode】 43. Multiply Strings 字符串相乘(Medium)(JAVA) 题目地址: https://leetcode.com/problems/multiply-strings/ 题目描述: Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Example 1: Input: num1 = "2", num2 = "3" Output: "6" Example 2: Input: num1 = "123", num2 = "456" Output: "56088" Note: 1. The length of both num1 and num2 is < 110. 2. Both num1 and num2 contain only digits 0-9. 3. Both num1 and num2 do not contain any leading zero, except the number 0 itself. 4. You must not use any built-in BigInteger library or

Leetcode 274.H指数(H-Index)

心已入冬 提交于 2020-03-09 13:40:28
Leetcode 274.H指数 1 题目描述( Leetcode题目链接 )   给定一位研究者论文被引用次数的数组(被引用次数是非负整数)。编写一个方法,计算出研究者的 h 指数。   h 指数的定义: “h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)至少有 h 篇论文分别被引用了至少 h 次。(其余的 N - h 篇论文每篇被引用次数不多于 h 次。)” 输入 : citations = [ 3 , 0 , 6 , 1 , 5 ] 输出 : 3 解释 : 给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3 , 0 , 6 , 1 , 5 次。 由于研究者有 3 篇论文每篇至少被引用了 3 次,其余两篇论文每篇被引用不多于 3 次,所以她的 h 指数是 3 。 2 题解   原文题目描述有些问题,参考英文描述更好。首先排序,排序后遍历数组,每次令 h = l e n g t h − i h=length-i h = l e n g t h − i 表示当前有 h h h 篇论文的引用次数大于等于 c i t a t i o n s [ i ] citations[i] c i t a t i o n s [ i ] ,所以当 h ≤ c i t a t i o n s [ i ] h\le

Java实现 LeetCode 345 反转字符串中的元音字母

百般思念 提交于 2020-03-09 12:13:21
345. 反转字符串中的元音字母 编写一个函数,以字符串作为输入,反转该字符串中的元音字母。 示例 1: 输入: “hello” 输出: “holle” 示例 2: 输入: “leetcode” 输出: “leotcede” 说明: 元音字母不包含字母"y"。 class Solution { public String reverseVowels ( String s ) { if ( s . length ( ) == 0 ) { return "" ; } char [ ] ss = s . toCharArray ( ) ; int i = 0 , j = s . length ( ) - 1 ; while ( i < j ) { while ( i < j && ( ss [ j ] != 'a' && ss [ j ] != 'e' && ss [ j ] != 'i' && ss [ j ] != 'o' && ss [ j ] != 'u' && ss [ j ] != 'A' && ss [ j ] != 'E' && ss [ j ] != 'I' && ss [ j ] != 'O' && ss [ j ] != 'U' ) ) j -- ; while ( i < j && ( ss [ i ] != 'a' && ss [ i ] != 'e' &&

【LeetCode】 121. Best Time to Buy and Sell Stock 买卖股票的最佳时机(Easy)(JAVA)

亡梦爱人 提交于 2020-03-09 11:40:54
【LeetCode】 121. Best Time to Buy and Sell Stock 买卖股票的最佳时机(Easy)(JAVA) 题目地址: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 题目描述: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Note that you cannot sell a stock before you buy one. Example 1: Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Not 7-1 = 6, as selling

LeetCode | 1376. Time Needed to Inform All Employees通知所有员工所需的时间【Python】

ⅰ亾dé卋堺 提交于 2020-03-09 09:51:46
LeetCode 1376. Time Needed to Inform All Employees通知所有员工所需的时间【Medium】【Python】【自底向上遍历】 Problem LeetCode A company has n employees with a unique ID for each employee from 0 to n - 1 . The head of the company has is the one with headID . Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1 . Also it’s guaranteed that the subordination relationships have a tree structure. The head of the company wants to inform all the employees of the company of an urgent piece of news. He will inform his direct

LeetCode:字符串转换整数 (atoi)

a 夏天 提交于 2020-03-09 07:58:12
刷题神器:LeetCode官方网站 一、题目还原 请你来实现一个 atoi 函数,使其能将字符串转换成整数。 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。 该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。 注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。 在任何情况下,若函数不能进行有效的转换时,请返回 0。 说明: 假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−2 31 , 2 31 − 1]。如果数值超过这个范围,请返回 INT_MAX (2 31 − 1) 或 INT_MIN (−2 31 ) 。 示例 1: 输入: “42” 输出: 42 示例 2: 输入: " -42" 输出: -42 解释: 第一个非空白字符为 ‘-’, 它是一个负号。 我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。 示例 3: 输入: “4193 with words” 输出: 4193 解释:

动态规划通用解法总结

穿精又带淫゛_ 提交于 2020-03-09 00:37:02
背景: leetcode刷题遇到动态规划的题目,做不出来时看别人的code,也可以理解,但还是没有找到create solution的技巧,单纯的comprehend and remeber,直到遇到了下面这篇题解,终于形成了自己的动态规划通用解题方法,拿所有easy难度的题目试了下,结果横扫 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/Most-consistent-ways-of-dealing-with-the-series-of-stock-problems 在动态规划题目中,变形数量最多的差不多就是股票问题了,上述解法也围绕股票问题展开 根据上述帖子,我总结了动态规划的解题方法如下 Question1:如何识别题目适用于动态规划? 某一过程包含多种状态(情况),后一种状态的生成依赖于前面的情况 Quetion2:如何写动态规划? 1、找出问题中的 状态 (state)和 选择 (choice) 2、用选择去表达状态之间的转移 like below for state1 in state1_list: for state2 in state2_list: for state3 in state3_list: dp