l****s 发帖数: 75 | 1 We only need to consider lower case
"bob" // is a palindrome
"a man, a plan, a canal, panama!" // is a palindrome
".374" // is a palindrome
" ;" // is a palindrome
bool isAlpha(char c); //provided
bool isPalindrome(const string & s)
{
if (s.empty()) return true;
int i = 0;
int j = s.size() - 1;
while (i < j)
{
while (!isAlpha(s[i]) && i < j)
{
++i;
}
// i == s.size() - 1;
while (!isAlpha(s[j]) && j > i)
{
... 阅读全帖 |
|
i**********e 发帖数: 1145 | 2 用 long long 检测 overflow,简单一些。。。
int atoi(const char *str) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int sign = 1;
while (*str == ' ') {
str++;
}
if (*str == '+') {
str++;
} else if (*str == '-') {
sign = -1;
str++;
}
long long num = 0;
bool overflow = false;
while (!overflow && isAlpha(*str)) {
int dig = *str... 阅读全帖 |
|
h****n 发帖数: 1093 | 3 bool isPalindrome(string s) {
int i = 0, j = s.size()-1;
while(i
while(i
while(i
if(i>j) break;
if(tolower(s[i])!=tolower(s[j])) return false;
i++;
j--;
}
return true;
} |
|
a***e 发帖数: 413 | 4 另外那道Length of Last Word
觉得没见过的话,现场15分钟要写出如下compact的code还是比较
int lengthOfLastWord(const char *s) {
int len = 0;
while (*s) {
if (*s++ != ' ')
{
len++;
}
else if (*s && *s != ' ') //觉得这个条件比较难懂,后来debug了才
知道怎么回事
len = 0;
}
return len;
}
我自己写的
int lengthOfLastWord(const char *s) {
int len=0;
while(*s!='\0')
{
if (*s==' '&&isalpha(*... 阅读全帖 |
|
L******k 发帖数: 395 | 5 my solution:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void jf_eval(string& in_line, unordered_map
>>& record)
{
int i_dex = in_line.find_first_of(' ');
string cur_var = in_line.substr(0, i_dex);
vector varibles;
int cur_value = 0, ele = 0;
bool new_variable = true, new_number = true; int index1 = 0;
int i = i_dex... 阅读全帖 |
|
j*****n 发帖数: 23 | 6 #include
#include
#include
#include
#include
#include
#include
using namespace std; //这个最好解释一下不会真的在production上这么写
void jf_eval(string& in_line, unordered_map
//jf 是什么意思?
>>& record)
{
int i_dex = in_line.find_first_of(' ');
string cur_var = in_line.substr(0, i_dex);
vector varibles;
int cur_value = 0, ele = 0;
bool new_variable = true, new_number = true; int ... 阅读全帖 |
|
i**********e 发帖数: 1145 | 7 Thanks for your test case.
Actually this is ok, because there is always a '\0' character that
terminates a C-style string.
isAlpha('\0') will evaluate as false and break out the loop immediately. |
|
|
|
J**9 发帖数: 835 | 10 来自主题: JobHunting版 - G家电面题 This is another question that seems easy but hard to code precisely.
Here's my version in C:
///1) Convert the string into all lower cases either in place or into
another buffer;
///2) First pass to check which ones are vowels;
///3) Second pass to sum up the products.
///4) Space O(n); Time O(n)
/** convert a string to lowercase in place */
char* hkStringTolower(char *str)
{
if(!str) return NULL;
char *s=str;
while(*s)
{
*s = tolower(*s);
s++;
}
return st... 阅读全帖 |
|
e*******i 发帖数: 56 | 11 Here is c++ version. Have to admit is over 15 lines.
Any suggestions to improve?
/*****************************************/
#include
#include
#include
using namespace std;
void maxRep(char *s)
{
int maxFreq=0;
int counter=0;
vector charList;
vector freqList;
char target=*s;
do{
if(*s!=target)
{
if( counter>=maxFreq && isalpha(target) )
{
maxFreq=counter;
charList.push_back(target);
... 阅读全帖 |
|
J**9 发帖数: 835 | 12 高手很多呀
看起来容易, 写起来难
It must be two passes unless you copy things to another array or list.
Here's my version in C:
void hkStringPrintMaxCounts(char *s)
{
if (!s || !(*s)) return;
int table[256] = {0};
char *p = s;
int max=1;
int count=1;
int index=0;
char c=*p;
printf("%s():\n", __FUNCTION__);
/// First pass: count and find max
while(*p)
{
c=*p;
count=0;
while(*p == c)
{
count++;
p++;
in... 阅读全帖 |
|
e*******i 发帖数: 56 | 13 Here is shorter version after incorporated some tricks posted by others
/************************/
#include
#include
#include
using namespace std;
void maxRep(char *s)
{
int maxFreq=0;
int counter=0;
string result;
char target=*s;
do{
if(*s!=target)
{
if( counter >= maxFreq && isalpha(target) ){
if(counter>maxFreq) result.clear();
result+=target;
maxFreq=counter;
}
target=*s;... 阅读全帖 |
|
q********c 发帖数: 1774 | 14 这是我的code, 更简单些:
int lengthOfLastWord(const char *s) {
int len = strlen(s), wordLen = 0;
const char *p = s+len-1;
while(isspace(*p) && p>=s)p--;
while(isalpha(*p) && p>=s) {
p--;
wordLen++;
}
return wordLen;
} |
|
F*****o 发帖数: 32 | 15 class TreeNode:
def __init__(self,x):
self.child=[]
self.val=x
class Solution:
def strToTree(self,s):
if len(s)==0: return None
stack=[]
for i in range(len(s)):
if s[i]=='(': stack.append(s[i])
elif s[i].isalpha():
stack.append(TreeNode(s[i])) #push to stack except ')'
else:
t,temp=stack.pop(),[]
while t!='(':
temp.append(t)
t... 阅读全帖 |
|
L******k 发帖数: 395 | 16 Problem Description
-------------------
Your task is to write a command-line program to evaluate a set of
equations, each specified on separate lines. An equation is defined
by:
=
is the left-hand side of the equation and is always a variable
name. A variable name can only be composed of letters from the
alphabet (e.g. for which isalpha(c) is 1). is the right hand
side of the equation and can be composed of variables, unsigned
integers, and the + operator.
Here is one exa... 阅读全帖 |
|
i**p 发帖数: 902 | 17 http://www.acceleratedcpp.com/details/msbugs.html
Chapter 12
Similar to the problems in Chapters 6 and 8, where VC++ 6.0 fails to include
the character classification functions (isalpha, isalnum, isspace etc.) as
part of the std namespace, it also fails to include the strlen function in
std. The workaround is analogous: Omit the qualification of std::strlen.
For example, in the Str constructor that takes a const char*:
#ifdef _MSC_VER
std::copy(cp, cp + strlen(cp), std::back_inserter(data));
#el |
|