经典的约瑟夫环,n个人排成一圈,第m个出队。递归
code1class Solution {
public:int f(int n,int m){if(n==1){//递归边界,最后一个return 0;}//先m出队,而最后留下的是在n-1个中排第f(n-1,m),在n个中就是排在m后面的第f(n-1,m)return (m+f(n-1,m))%n;}int lastRemaining(int n, int m) {return f(n,m);}
};
非递归
code2class Solution {
public:int lastRemaining(int n, int m) {int f=0;for(int i=2;i<=n;i++){f=(f+m)%i;}r...
【139-Word Break(单词拆分)】【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", dict = ["leet", "code"]. Return true because "leetcode" can be segmented as "leet code". 题目大意 给定一个字符串s和单词字典...
题目:思路:通过Python有很多方便简单的解法,但本题考察的是字符串操作,用C++更好一些(利用String可遍历的性质)代码:Pythonclass Solution(object):def replaceSpace(self, s):""":type s: str:rtype: str"""# return s.replace(‘ ‘, ‘%20‘)# return ‘‘.join((‘%20‘ if c==‘ ‘ else c for c in s))return ‘%20‘.join(s.split(‘ ‘))
C++class Solution {
public:string replaceSpace(string s) {string result;for (...
【059-Spiral Matrix II(螺旋矩阵II)】【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】原题 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix:[[ 1, 2, 3 ],[ 8, 9, 4 ],[ 7, 6, 5 ]
]题目大意 给定一个整数n。生成一个n*n的矩阵,用1-n^2的数字进行螺旋填充。解题思路 採用计算生成法...
14. 最长公共前缀编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。示例 1:输入: ["flower","flow","flight"]
输出: "fl"示例 2:输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。说明:所有输入只包含小写字母 a-z 。思路:暴力双层循环先找到长度最短的个字符串,遍历这个字符串,判断这个字符串的每个字符是否等于其他字符串相应字符串该位置的字符如果该字符在其他字符串...
【151-Evaluate Reverse Polish Notation(计算逆波兰式)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] ->((2 + 1) * 3) ->9["4", "13", "5", "/", "+"] ->(4 + (13 / 5)) ->6题目大意 ...
【137-Single Number II(只出现一次的数字II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 题目大意 给一个数组,里面只有一个数字一次,其它数字都出现3次,找出这个出现一...
1、给两个字符串s和t,判断t是否为s的重新排列后组成的单词。s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.leetcode地址:https://leetcode.com/problems/valid-anagram/description/(1)解法一:排序,O(n*logn) class Solution:def isAnagram(self, s, t):""":type s: str:type t: str:rtype: bool"""ss = list(s)tt = list(t)ss.sort()tt.sort()return ss == tt
"""
输入:"anagram"、"naga...
【082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2->3. 题目大意 给定一个排好序的单链表,删除所有重复...
面试题64. 求1+2+…+n求 1+2+...+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。示例 1:输入: n = 3
输出:?6
示例 2:输入: n = 9
输出:?45
限制:1 <= n?<= 10000来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/qiu-12n-lcof著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。func sumNums(n int) int {ans := 0var sum func(int) boolsum ...
我的LeetCode:https://leetcode-cn.com/u/ituring/我的LeetCode刷题源码[GitHub]:https://github.com/izhoujie/AlgorithmciiLeetCode 面试题13. 机器人的运动范围题目地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] 。一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左、右、上、下移动一格(不能移动到方格外),也不能进入行坐标和列坐标的数位之和大于k的格子。例如,当k为18时,机器人能够进入方格 [35, 37]...
【113-Path Sum II(路径和II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题 Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. For example: Given the below binary tree and sum = 22, 5/ 4 8/ / 11 13 4/ \ / 7 2 5 1 return[[5,4,11,2],[5,8,4,5]
]题目大意 给定一棵二叉树...
LeetCode206 反转链表思路代码#
# @lc app=leetcode.cn id=206 lang=python3
#
# [206] 反转链表
#
# https://leetcode-cn.com/problems/reverse-linked-list/description/
#
# algorithms
# Easy (61.53%)
# Likes: 624
# Dislikes: 0
# Total Accepted: 112.8K
# Total Submissions: 172.9K
# Testcase Example: '[1,2,3,4,5]'
#
# 反转一个单链表。
#
# 示例:
#
# 输入: 1->2->3->4->5->NULL
# 输出: 5->4->3->2->1->N...
我的LeetCode:https://leetcode-cn.com/u/ituring/我的LeetCode刷题源码[GitHub]:https://github.com/izhoujie/AlgorithmciiLeetCode 面试题05. 替换空格题目请实现一个函数,把字符串 s 中的每个空格替换成"%20"。示例 1:输入:s = "We are happy."
输出:"We%20are%20happy."
限制:0 <= s 的长度 <= 10000来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof
著作权归领扣网络所有。商业转...
【033-Search in Rotated Sorted Array(在旋转数组中搜索)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. 题目大...