需要逻辑缜密阿,
我实现的好像有bug,大家能想到比较好的办法么
static int encode(String str) {
int num = 0;
int sum = 0;
int cur = 0;
boolean vowelBefore = false;
boolean vowel;
str = str.toLowerCase();
for ( int i=0; i< str.length(); i++) {
if (map.get(str.charAt(i)) != null) {
cur = map.get(str.charAt(i));
vowel = true;
} else {
vowel = false;
}
我来写一个java的
public ListNode swapPairs(ListNode head) {
// Start typing your Java solution below
// DO NOT write main() function
if (head == null || head.next == null)
return head;
我来写一个java的
public ListNode swapPairs(ListNode head) {
// Start typing your Java solution below
// DO NOT write main() function
if (head == null || head.next == null)
return head;
The recursive method could be very similar to the iterative approach:
node* findNtolast(node* head, int n){
for (node*tmp = head, int i=0; inext; tmp=tmp->next){}
recurs(head, tmp);
}
node* recurs(node* head, node* cur){
if (!cur->next)
return head;
else
return rev(head->next, cur->next);
}
cell zeroX, zeroY;
cell [n*2][n*2] target;
int[n][n] mixedCells;
target[n][n] = mixedCells[0][0];
put all mixedCells in a list(cellList), except [0][0];
while(!cellList.empty())
{
loop(cur) cellList
loop(curTarget) target
if 关系(cur, curTarget) then
remove from cellList
put cur to target
update zeroX, zeroY
end if
end loop;
end loop
}
output(target, zeroX, zeroY)
二爷真要较真的话,这样写并不好。因为java里array其实是一个object,每次不管是
读还是写,都要做越界检查。你这段代码虽然短,但除了第一个和最后一个element之
外,每个都读了两遍。还是不如用一个pre变量来cache前一个读数。
int ans = 0;
int pre = prices[0];
for(int i = 1; i < prices.length; i++)
{
int cur = prices[i];
ans += Math.max(0, cur - pre);
pre = cur;
}
return ans;
如果追求短的话,还可以更短:
int ret = 0;
for(int i = 1; i< prices.length; ret += Math.max(0, prices[i] - prices[i++ -
1]));
return ret;
三行就好了。
public static class Bar{
int height, startIdx;
Bar(int h, int i){ height = h; startIdx = i; }
}
public int maximalRectangle(char[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) return 0;
int n = matrix.length, m = matrix[0].length;
int[][] aux = new int[n][m];
for (int i=0; i
aux[0][i] = matrix[0][i] - '0';
}
for (int i=1; i
... 阅读全帖
int jump_gameII(const std::vector& input) {
int cur = 0;
int step = 0;
int max = 0;
for (int i = 0; i < input.size(); ++i) {
if (i > cur && i <= max) {
++step;
cur = max;
} else if (i > max) {
return -1; // cannot reach i;
}
max = std::max(max, i + input[i]);
}
return step;
}
ok, I only had a glimpse on a.append(0), and quickly skip it... :-), have
not written python for quite some time.
then I think it is correct. but personally, I feel it is tricky to append "0
" at the end of input, and also the input has to change. Another
controversial case is the empty input, you will output "out".
Well, maybe the reviewer did not see that "append", or did not think too
much. or there is still something wrong we have not checked out.
Your code is not far from not modifying inpu... 阅读全帖
我也被问过这个问题,当时没写好,现在再写一个:
public class DeepIterator {
private Stack stack;
public DeepIterator(List list) {
stack = new Stack();
stack.push(list);
advanceToNext();
}
public boolean hasNext() {
return !stack.isEmpty();
}
public int next() {
if (!hasNext())
throw new RuntimeException("no next");
int result = (Integer) stack.pop();
advanceToNext();
public class WordLadderII {
public class Ladder { //Define Ladder class it's important
public Ladder parent;
public String word;
public Ladder(Ladder prev,String w) {parent=prev;word=w;}
}
public ArrayList> findLadders(String start, String end
, HashSet dict) {
ArrayList> ladders = new ArrayList
String>>();
Ladder ladder = new Ladder(null,end); //Here we look from end to
start ... 阅读全帖
bool isSquare(int n)
{
int num = 0;
while (n)
{
if (n&0x01)
num++;
n =>>1;
}
if (num >= 2)
return false;
else
return true;
}
int getComb(int n, int tmp[])
{
int l = n/2, r = n-l;
int min = tmp[l] + tmp[r];
while (l > 0 && r < n)
{
int cur = tmp[l] + tmp[r];
if (min > cur)
min = cur;
l--;
r++;
}
return min;
}
int getSmallestK(int n)
{
if (n <= 0)
retu... 阅读全帖
u want to do
result=result*10+cur-'0';
then need to make sure that it is less than or equal to Integer.MAX_VALUE
if(result
(result==Integer.MAX_VALUE/10&&cur-'0'<=Integer.MAX_VALUE%10)) {
result=result*10+cur-'0';
} else {
//overflow
}
前段时间电面了Amex,他家的技术中心貌似在Arizona,因为recruiter和电面的人都是
从那边打来的电话。
面我的是另一人口大国的,Title好像是tech architecture lead之类,之前上
linkedin偷瞄了一下背景,将近二十年经验吧,但没看出来有前端相关的经验。
面试是structured,也就是拿着一堆问题隔着电话问,电话声音质量非常差劲,应该是
IP电话或者类似LiveMeeting之类的,中间还断线两次,面试官的口音各位自己脑补吧
,加上杂音问题,使得我每一个问题都要重复一遍以确保自己听明白了。没多久高潮来
了:
面试官:There is a div with a width of 185, padding of 10, and margin of 20.
What is the width of the div?
此题明显是个陷阱啊,没提box model,没说后一个width的定义,于是我就把content-
box和border-box说了一下,然后分别说明答案。然后他问,shouldn't the margin be
included? 我... 阅读全帖
class Solution:
# @return a string
def countAndSay(self, n):
if n==1:
return '1'
pre=self.countAndSay(n-1)
start=0
next=''
while start
count=0
cur=pre[start]
while start
count+=1
start+=1
next=next+str(count)+cur
return next
两个HASHSET 一个叫LAST 一个叫CUR
把第一个LIST放进LAST
然后遍历 2 - N 的LIST,每次把 LAST 里出现在新LIST里的 加进 CUR SET
每次循环结束时 LAST = CUR 这样LAST里面的内容会越来越少
到最后就是出现在所有LIST里的邮件了
边吃早饭边想的,如果有问题,麻烦指出
public List findEmails(List> emails) {
List ret = new ArrayList();
if (emails == null || emails.size() == 0) {
return ret;
}
Set last = new HashSet();
for (String s : emails.get(0)) {
last.add(s.toLowerCase());
}
for (int i = 1;i < emails.size(); i++) {
Set cur = new HashSet();
for (String s : ... 阅读全帖
写了一个用list的版本
public static String reverse(String s) {
List list = new ArrayList();
boolean word = Character.isLetter(s.charAt(0));
int i = 0, n = s.length(), start = 0;
while(i < n) {
char cur = s.charAt(i);
while(i < n && ((word&&Character.isLetter(cur)) || (!word&&
Character.isLetter(cur)))) {
++i;
}
list.add(s.substring(start, i));
start = i;
word = !word... 阅读全帖
读4
这道题给了我们一个Read4函数,每次可以从一个文件中最多读出4个字符,如果文件中
的字符不足4个字符时,返回准确的当前剩余的字符数。现在让我们实现一个最多能读
取n个字符的函数
int read(char *buf, int n) {
int res = 0;
for (int i = 0; i <= n / 4; ++i) {
int cur = read4(buf + res);
if (cur == 0) break;
res += cur;
}
return min(res, n);
}
读4 II Read N Characters Given Read4的拓展,那道题说read函数只能调用一次,而
这道题说read函数可以调用多次
int read(char *buf, int n) {
for (int i = 0; i < n; ++i) {
if (readPos == writ... 阅读全帖