思路是这样的,我们从第一个字符开始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到,那么就是False了。在找到第一个后,接下来找下一个断句处,当然是从第一个断句处的下一个字符开始找连续的子串,但是这时与第一个就稍有不同,比如说word=‘ab’, dict={ ‘a‘, ab‘, ...},在找到a后,接下来处理的是b,我们发现b不在dict中,但是我们发现b可以和a结合,形成ab,而ab在dict中,所以这里...
题目来源: https://leetcode.com/problems/search-a-2d-matrix/ 题意分析: 给一个m×n矩阵,矩阵是按照从小到大排列,也就是a[i][j]和a[m][n]如果i > m则a[i][j]>a[m][n],如果i == m,j > n,则a[i][j]>a[m][n]。 题目思路: 利用二分查找,首先确定在那个行,然后继续二分查找。 代码(Python): 1class Solution(object):2def searchMatrix(self, matrix, target):3""" 4 :type matrix: List[List[int]]5 ...
原题地址:https://oj.leetcode.com/problems/powx-n/题意:Implement
pow(x, n).解题思路:求幂函数的实现。使用递归,类似于二分的思路,解法来自Mark Allen Weiss的《数据结构与算法分析》。代码:class Solution:# @param x, a float# @param n, a integer# @return a floatdef pow(self, x, n):if n == 0:return 1elif n == 1:return xelif n % 2:return pow(x*x,n/2)*xelse:return pow(x*x,n/2) 原文:http://www.cnblogs.c...
【python】Leetcode每日一题-寻找旋转排序数组中的最小元素2【题目描述】已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组。例如,原数组nums = [0,1,2,4,5,6,7]在变化后可能得到:
若旋转4次,则可以得到 [4,5,6,7,0,1,2]
若旋转4次,则可以得到 [0,1,2,4,5,6,7]
注意,数组[a[0], a[1], a[2], ..., a[n-1]]旋转一次 的结果为数组[a[n-1], a[0], a[1], a[2], ..., a[n-2]]。给你一个可能存在重复...
简单的插入排序,总是超时,暂且放在这记录一下。class Solution:# @param head, a ListNode# @return a ListNodedef insertionSortList(self, head):if head == None or head.next == None:return headpsuhead = ListNode(-1)while head:tail = psuheadheadnext = head.nextwhile tail.next and tail.next.val < head.val:tail = tail.nexthead.next = tail.nexttail.next = headhead = headnextreturn psuhead.next
Last execute...
# -*- coding: utf8 -*-‘‘‘__author__ = ‘dabay.wang@gmail.com‘54: Spiral Matrixhttps://leetcode.com/problems/spiral-matrix/Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You should return [1,2,3,6,9,8,7,4,5].=== Comments by Dabay===一圈一圈的处理,一共处理n/2圈...
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.
A sudoku puzzle...
This problem is M^9 complexity m is the number of empty slots.
we can use DFS to solve this problem with backtracking.class Solution:# @param board, a 9x9 2D array# Solve the Sudoku by modifying the inp...
# -*- coding: utf8 -*-‘‘‘__author__ = ‘dabay.wang@gmail.com‘39: Combination Sumhttps://oj.leetcode.com/problems/combination-sum/Given a set of candidate numbers (C) and a target number (T),find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number of times.Note:All numbers (including target) will be positive integers....
# -*- coding: utf8 -*-‘‘‘__author__ = ‘dabay.wang@gmail.com‘37: Sudoku Solverhttps://oj.leetcode.com/problems/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.===Comments by Dabay===逐行扫描,当遇到“.”的时候,尝试每一个可能的valid_num。如果能DFS到底,就...
Problem:
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2...
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
generally we can have two methods to do this problem, the first one is based on finding pivot and then divide the array into two parts, then search.
second one is still based on Binary search.class S...
原题地址:http://www.cnblogs.com/x1957/p/3373994.html题意:Given an array of integers, every element
appears twice except for one. Find that single
one.要求:线性时间复杂度,并且不用额外空间。解题思路:这题考的是位操作。只需要使用异或(xor)操作就可以解决问题。异或操作的定义为:x ^ 0 = x; x ^ x = 0。用在这道题里面就是:y
^ x ^ x = y; x ^ x = 0; 举个例子:序列为:1122334556677。4是那个唯一的数,之...
Python 3class MinStack:def__init__(self):self.stack = []self.min_stack = [math.inf]def push(self, x: int) -> None:self.stack.append(x)self.min_stack.append(min(x, self.min_stack[-1]))def pop(self) -> None:self.stack.pop()self.min_stack.pop()def top(self) -> int:return self.stack[-1]def getMin(self) -> int:return self.min_stack[-1] 原文:https://www.cnblogs.com/vicky2021/p/14897746.html
题目:反转链表 II:反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。说明:1 ≤ m ≤ n ≤ 链表长度。示例:输入: 1->2->3->4->5->NULL, m = 2, n = 4输出: 1->4->3->2->5->NULL思路:思路较简单,找到规律就好。程序:# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution:...
题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值。思路:把给定的链表排序,并且在排序的同时把60进制的时间转化成十进制整数;遍历排序的数组,求出两个相邻值之间的差值;求出首尾两个值之间的差值。class Solution(object):def findMinDifference(self, timePoints):""" :type timePoints: List[str] :rtype: int """t =sorted(int(t[:2]) *60+int(t[-2:]) for t in timePoints)...