面试笔试杂项积累-leetcode 131-135
2016-02-06 00:06
676 查看
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 of
s.
For example, given s =
"aab",
Return
1since the palindrome partitioning
["aa","b"]could be produced using 1 cut.
思路
找到最少分割的回文,返回分割次数动态规划
This can be solved by two points:
cut[i]is the minimum of
cut[j - 1] + 1 (j <= i), if
[j, i]is palindrome.
If
[j, i]is palindrome,
[j + 1, i - 1]is palindrome, and
c[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
;
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 costs
cost[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
;
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];
}
}
相关文章推荐
- 面试笔试杂项积累-leetcode 126-130
- 面试笔试杂项积累-leetcode 121-125
- Java面向对象面试案例
- 面试笔试杂项积累-leetcode 116-120
- 【转载】不再纠结:从程序员的角度解读苹果为什么坚持使用1G内存
- JAVA多线程和并发基础面试问答
- 程序员如何提高影响力:手把手教你塑造个人品牌
- Java程序员需要了解的8个开发工具
- [转] 金山CEO求伯君今退休 老一代程序员时代结束
- 在你步入职业软件开发生涯那天起就该知道的五件事
- 面试准备(一) 注册广播有哪几种方式,有什么区别
- Java系统程序员修炼之道(转载于CSDN"流浪的鱼"大大)
- 程序员必备php十大开源框架
- 【面试题1】前端全局观-待答
- (转)个人职业规划中如何使自己的职业生涯升华
- 职业素养与职业迷茫——北漂18年(42)
- 写些安卓开发的面试题
- 面试笔试杂项积累-leetcode 111-115
- 面试笔试杂项积累-leetcode 106-110
- 面试笔试杂项积累-leetcode 101-105