leetcode题解日练--2016.09.03

来源:互联网 发布:pss监控软件下载 编辑:程序博客网 时间:2024/06/07 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 k        for(int i=nums.size()-2;i>=0;i--)        {            if(nums[i]<nums[i+1])            {                k=i;                break;            }        }        //reverse case        if(k==-1)        {            reverse(nums.begin(),nums.end());            return ;        }        //find l        int 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

0 0
原创粉丝点击