careercup-1.5

1.5 利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能。比如,字符串”aabcccccaaa“会变成”a2b1c5a3“。若”压缩“后的字符串没有变短,则返回原先的字符串。

类似 leetcode中 解法:Count and say.

C++实现代码:

#include<iostream>
#include<string>
using namespace std;

string compress(string str)
{
    if(str.empty())
        return "";
    int i,j=0;
    int len=str.length();
    string res;
    char c;
    for(i=0;i<len;i++)
    {
        if(str[i]!=str[j])
        {
            c=i-j+‘0‘;
            res+=str[j];
            res+=c;
            j=i;
        }
    }
    c=i-j+‘0‘;
    res+=str[j];
    res+=c;
    if(res.length()==2*str.length())
        return str;
    return res;
}

int main()
{
    string str="abcd";
    cout<<compress(str)<<endl;
}
时间: 2024-09-20 09:39:39

careercup-1.5的相关文章

[CareerCup] 18.9 Find and Maintain the Median Value 寻找和维护中位数

18.9 Numbers are randomly generated and passed to a method. Write a program to find and maintain the median value as new values are generated. LeetCode上的原题,请参见我之前的博客Find Median from Data Stream. 解法一: priority_queue<int> small; priority_queue<int,

[CareerCup] 18.4 Count Number of Two 统计数字2的个数

18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如果n=20,那么满足题意的是2, 12, 20,那么返回3即可.LeetCode上有一道很类似的题Factorial Trailing Zeroes,但是那道题求5的个数还包括了因子中的5,比如10里面也有5,这是两题的不同之处.那么首先这题可以用brute force来解,我们对区间内的每一个数字

CareerCup Facebook Total number of substring palindrome

Write a function for retrieving the total number of substring palindromes. For example the input is 'abba' then the possible palindromes= a, b, b, a, bb, abba So the result is 6. Updated at 11/11/2013: After the interview I got know that the O(n^3) s

Careercup | Chapter 4

二叉查换树,左孩子小于等于根,右孩子大于根. 完全二叉树,叶子都在最后一层,所有结点(除了叶子)都有两个孩子. 平衡二叉树,左右子树的高度在一定范围内. 4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two s

CareerCup之1.8 字符串移位包含问题

[题目] 原文: 1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring ( i.e., "waterbottle" is a rotat

[CareerCup] 18.10 Word Transform 单词转换

18.10 Given two words of equal length that are in a dictionary, write a method to transform one word into another word by changing only one letter at a time. The new word you get in each step must be in the dictionary. 这道题让我们将一个单词转换成另一个单词,每次只能改变一个字母,

Careercup | Chapter 6

6.3 You have a five-quart jug, a three-quart jug, and an unlimited supply of water (but no measuring cups). How would you come up with exactly four quarts of water? Note that the jugs are oddly shaped, such that filling up exactly "half" of the

[CareerCup] 18.11 Maximum Subsquare 最大子方形

18.11 Imagine you have a square matrix, where each cell (pixel) is either black or white. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels. LeetCode上的原题,请参见我之前的解法Maximal Square.书上给了两种解法,但是比较长:

[CareerCup] 18.3 Randomly Generate Integers 随机生成数字

18.3 Write a method to randomly generate a set of m integers from an array of size n. Each element must have equal probability of being chosen. 这道题让我们从一个数组中随机取出m个数字,要求每个数字被取出的概率相同,其实这道题用的是之前那道18.2 Shuffle Cards的方法,同样我们可以用递归和迭代两种方法来做,递归的思路还用的回溯法,回溯到i+

[CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径

4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes. LeetCode和CareerCup中关于图的题都不是很多,LeetCode中只有三道,分别是Clone Graph 无向图的复制,Course Schedule 课程清单 和 Course Schedule II 课程清单之二.目前看来CareerCup中有关图的题在第四章中仅此一道,这是