由买买提看人间百态

topics

全部话题 - 话题: newlen
(共0页)
r****k
发帖数: 21
1
来自主题: JobHunting版 - 贡献一道G家的面试题
void deleteADoubleB(char *str, int length)
{
if (str == NULL || length < 0) return;

int newLen = 0;
int bCount = 0;
for (int i = 0; i < str[i] !='\0'; i++)
{
if (str[i] != 'A')
{
str[newLen++] = str[i];
}
if (str[i] == 'B')
{
bCount++;
}
}
if (newLen + bCount > length) return;
int i = newLen - 1;
newLen += bCount;
str[newLen--] = '\0';
while (i>0)
{
str[newLen--] = ... 阅读全帖
f**l
发帖数: 44
2
来自主题: JobHunting版 - 贡献一道G家的面试题
Java version:
/*
* Process string, remove 'A' and double 'B'
*/
public static String processString(char[] array, int realLen) {
int newLen = realLen;
int pos = 0;
for (int i = 0; i < realLen; i++) {
char c = array[i];
if (c == 'A')
newLen--;
else {
if (c == 'B')
newLen++;
array[pos++] = array[i];
}
}
int j = newLen - 1;
... 阅读全帖
d****n
发帖数: 1637
3
来自主题: Programming版 - C++ Segment Fault
newLen=spaceCount*2+len;
== change to ==>
newLen=spaceCount*3+len+1;
modifiedStr=new char[newLen];
if(!modifiedStr) return NULL ; // return failed when can not allocated
memory from heap
modifiedStr[newLen-1]='\0';//for classica C string convertion with '\0'
terminated string
modifiedStr+=(newLen-1);
btw: print an address value of char * is never fail.
I think mainly you function has an issue of miscalculated space for
modifiedStr
z********i
发帖数: 568
4
来自主题: Programming版 - C++ Segment Fault
char* replace(char* s,int len){
int spaceCount=0;
char* modifiedStr;
int newLen,i;
for(i=0;i if((*s)==' ')
spaceCount++;
}
newLen=spaceCount*2+len;
modifiedStr=new char[newLen];
modifiedStr+=newLen;
/* replace ' ' with "%20" */
for(i=len;i>=0;i--,modifiedStr--,s--){
if((*s)!=' ')
(*modifiedStr)=(*s);
else{
(*modifiedStr)='0';
modifiedStr--;
(*modifiedStr)='2';
modifiedStr--;
(*modifiedStr)='%';
}
}
mo... 阅读全帖
l******i
发帖数: 103
5
来自主题: JobHunting版 - 问一道F家面试题
用dp,cost[i,j]记录将a[0...i]变成最后一个元素不大于j的sorted array所需要的最
小cost。求cost[i+1,j]时,考察包含a[i+1](a[0...i+1]都变成不大于j)和不包含a[
i+1](去掉a[i+1],将a[0...i]变成不大于j的数)两种情况下的cost,取较小值。。代
码如下,大家帮忙看看有没有错。思路和代码。
int minimum_cost(vector &v) {
if (v.size() <= 1) return 0;
int n = v.size();
map cost;
vector sorted(v);
sort(sorted.begin(), sorted.end());
vector::iterator it = unique(sorted.begin(), sorted.end());
sorted.resize(it-sorted.begin());
int newlen = sorted.size... 阅读全帖
W********e
发帖数: 45
6
remove duplicate in sorted array这道题,虽然现在leetcode已经没有这题了,只有
follow up 的judge。不过我还是写了一段代码,顺便用leetcode来judge一下。不知道
为什么我在A数组最后加了一个结束符,就出现run time error了?而我用c-free验证
却没问题?代码如下,麻烦大家了!
class Solution {
public:
int removeDuplicates(int A[], int n) {
int newArrIndex=0,index=0,newlen=0;
if(n==0)
return 0;
while(index {
if(A[index]==A[index+1])
{
A[newArrIndex]=A[index];
}
else
{
A[newArr... 阅读全帖
(共0页)