leetcode题解日练--2016.09.03

2024-03-06 10:38

本文主要是介绍leetcode题解日练--2016.09.03,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

###不给自己任何借口

今日题目:

1、分数转换为循环小数

2、下一个排列

今日摘录:

人们宁愿去关心一个蹩脚电影演员的吃喝拉撒和鸡毛蒜皮,而不愿了解一个普通人波涛汹涌的内心世界……

——路遥《平凡的世界》

166. Fraction to Recurring Decimal | Difficulty: Medium

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.

If the fractional part is repeating, enclose the repeating part in parentheses.

For example,

Given numerator = 1, denominator = 2, return “0.5”.
Given numerator = 2, denominator = 1, return “2”.
Given numerator = 2, denominator = 3, return “0.(6)”.
Hint:

No scary math, just apply elementary math knowledge. Still remember how to perform a long division?
Try a long division on 4/9, the repeating part is obvious. Now try 4/333. Do you see a pattern?
Be wary of edge cases! List out as many test cases as you can think of and test your code thoroughly.

tag:数学|哈希表
题意:分数转换为循环小数

思路:
1、注意不要越界
首先得到整数部分,然后判断是否有小数部分,小数部分有两种情况,一种整除,一种不整除。对于不整除情况需要借助一个map判断分子是否之前有出现过,如果是则终止循环并且加上括号,不是就继续循环。

class Solution {
public:string fractionToDecimal(int numerator, int denominator) {assert(denominator!=0);if(numerator==0)      return "0";string res;int64_t  num =numerator,deno = denominator,cur,sign=1;//判断分数的符号if(num<0 ^ deno<0) res+="-";num =abs(num); deno=abs(deno);//首先加上整数部分res += to_string(num/deno);//num更新为小数部分,如果小数部分为0可以直接返回了,否则进行计算num %=deno; if(num==0)  return res;unordered_map<int,int> map;res+=".";//map用来记录分子为num出现的位置,默认是0,如果某个分子x发现之前在y位置出现过,说明这是一个循环小数,那么只需要保留之前的循环部分,然后给循环部分加上一个括号就可以了。while(num){if(map.count(num)>0){res.insert(map[num],1,'(');res+=')';break;}map[num] = res.size();num*=10;res+=to_string(num/deno);num%=deno;}return res;}
};

结果:0ms

31. Next Permutation | Difficulty: Medium

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 order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

tag:数组

题意:找到下一种组合方式
思路:
参考https://discuss.leetcode.com/topic/15216/a-simple-algorithm-from-wikipedia-with-c-implementation-can-be-used-in-permutations-and-permutations-ii/2

主要是4步:
1、找到最大的序号k满足nums[k] < nums[k+1]
2、找到最大的序号l满足nums[l] > nums[k]
3、交换nums[l]与nums[k]
4、逆转nums[k+1]到nums[nums.size()-1]

class Solution {
public:void nextPermutation(vector<int>& nums) {int k=-1;//find kfor(int i=nums.size()-2;i>=0;i--){if(nums[i]<nums[i+1]){k=i;break;}}//reverse caseif(k==-1){reverse(nums.begin(),nums.end());return ;}//find lint l=-1;for(int i=nums.size()-1;i>=0;i--){if(nums[i]>nums[k]){l=i;break;}}swap(nums[k],nums[l]);reverse(nums.begin() + k + 1, nums.end()); return ;}
};

结果:9ms

这篇关于leetcode题解日练--2016.09.03的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/779791

相关文章

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个?

跨平台系列 cross-plateform 跨平台应用程序-01-概览 cross-plateform 跨平台应用程序-02-有哪些主流技术栈? cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个? cross-plateform 跨平台应用程序-04-React Native 介绍 cross-plateform 跨平台应用程序-05-Flutte

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

FreeRTOS内部机制学习03(事件组内部机制)

文章目录 事件组使用的场景事件组的核心以及Set事件API做的事情事件组的特殊之处事件组为什么不关闭中断xEventGroupSetBitsFromISR内部是怎么做的? 事件组使用的场景 学校组织秋游,组长在等待: 张三:我到了 李四:我到了 王五:我到了 组长说:好,大家都到齐了,出发! 秋游回来第二天就要提交一篇心得报告,组长在焦急等待:张三、李四、王五谁先写好就交谁的