面试笔试杂项积累-leetcode 131-135

来源:互联网 发布:arduino摄像头编程 编辑:程序博客网 时间:2024/05/20 05:26

131.131-Palindrome Partitioning-Difficulty: Medium

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

For example, given s = "aab",
Return

  [    ["aa","b"],    ["a","a","b"]  ]

思路

给予一个string,找到所有回文的部分

动态规划

public class Solution {    public IList<IList<string>> Partition(string s) {     IList<IList<string>> res =new List<IList<string>>();        if (s=="") {            res.Add(new List<string>());            return res;        }        for (int i = 0; i < s.Length; i++) {            if (isPalindrome(s, i + 1)) {                foreach (List<string> list in Partition(s.Substring(i+1))) {                    list.Insert(0, s.Substring(0, i + 1));                    res.Add(list);                }            }        }        return res;    }    public bool isPalindrome(string s, int n) {        for (int i = 0; i < n / 2; i++) {            if (s[i] != s[n - i - 1])                return false;        }        return true;    }}

132.132-Palindrome Partitioning II-Difficulty: Hard

Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning ofs.

For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

思路

找到最少分割的回文,返回分割次数

动态规划

This can be solved by two points:

  1. cut[i] is the minimum of cut[j - 1] + 1 (j <= i), if[j, i] is palindrome.
  2. If [j, i] is palindrome, [j + 1, i - 1] is palindrome, andc[j] == c[i].

The 2nd point reminds us of using dp (caching).

a   b   a   |   c  c                j  i       j-1  |  [j, i] is palindrome   cut(j-1) +  1

参考:

https://leetcode.com/discuss/76411/easiest-java-dp-solution-97-36%25

public class Solution {    public int MinCut(string s) {        char[] c = s.ToCharArray();    int n = c.Length;    int[] cut = new int[n];    bool[,] pal = new bool[n,n];    for(int i = 0; i < n; i++) {        int min = i;        for(int j = 0; j <= i; j++) {            if(c[j] == c[i] && (j + 1 > i - 1 || pal[j + 1,i - 1])) {                pal[j,i] = true;                  min = j == 0 ? 0 : Math.Min(min, cut[j - 1] + 1);            }        }        cut[i] = min;    }    return cut[n - 1];    }}

134.134-Gas Station-Difficulty: Medium

There are N gas stations along a circular route, where the amount of gas at stationi is gas[i].

You have a car with an unlimited gas tank and it costscost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.

Note:
The solution is guaranteed to be unique.

思路

给与一个数组gas[]代表某个位置(i)的瓦斯,一个数组cost[]代表从当前位置(i)到下一个位置(i+1)需要消耗多少瓦斯,找到一条能从开始走到结尾的一条路线,返回起始位置

从每个可行起始位置开始,先要判断哪个位置可行,判断方法是gas[i]>=cost[i]这个是必须的,得有至少相等的gas到下一个位置。然后再遍历如果cost大于已有gas则丢弃此起始节点从下一个再开

public class Solution {    public int CanCompleteCircuit(int[] gas, int[] cost) {             int start = 0;        int have_gas = 0;        int j = gas.Length - 1;        for (int i = 0; i < gas.Length; i++)        {            have_gas = 0;            j = gas.Length - 1;            if (gas[i] >= cost[i])            {                start = i;                have_gas = 0;                while (j > -1)                {                    have_gas += gas[start] - cost[start];                    if (have_gas < 0)                        break;                        ++start;                    if (j == i)                        start = 0;                    --j;                }                if (j == -1)                    return i;            }        }        return -1;    }}

135.135-Candy-Difficulty: Hard

There are N children standing in a line. Each child is assigned a rating value.

You are giving candies to these children subjected to the following requirements:

  • Each child must have at least one candy.
  • Children with a higher rating get more candies than their neighbors.

What is the minimum candies you must give?

思路

分糖,

每个孩子至少要有一个糖,高优先级的孩子的糖要比左右邻居的要多

从左到右分一遍,从右到左分一遍,就是要照顾到这个左右啊。。。

动态规划


public class Solution {    public int Candy(int[] ratings) {        int n = ratings.Length;        int[] candy = new int[n];        for (int i = 0; i < n; i++)            candy[i]  = 1;        for (int i = 1; i < n; ++i)        {            candy[i] = ratings[i] <= ratings[i - 1] ? 1 : candy[i - 1] + 1;        }        for (int i = n - 2; i > -1; --i)        {            candy[i] = ratings[i] <= ratings[i + 1] ? candy[i] : Math.Max(candy[i], candy[i + 1] + 1);        }        for (int i = 1; i < n; i++)            candy[0] += candy[i];        return candy[0];    }}












1 0