一站通offer你们都AC了几道题?感觉后面几题偏难啊

一站通offer你们都AC了几道题?感觉后面几题偏难啊。。。。。
全部评论
第一题 我用java 写写了3种方法都不对 是为什么, 第二题不是异或吗,为什么又不对,第三题不是深度遍历二叉树吗,为什么又不对!!!!!!到底咋了,是不是不支持JAVA. 
点赞 回复 分享
发布于 2016-04-19 21:12
第三题用的递归写的。已AC。 public class NCLongestSingleColorPath { public int max=0; public int findPath(TreeNode root) { // write code here recur(root); return max; } public int[] recur(TreeNode root){ int[] res = new int[]{0,0}; if(root==null) return res; int[] left = recur(root.left); int[] right = recur(root.right); res[root.val]=Math.max(left[root.val],right[root.val])+1; int temp=left[root.val]+right[root.val]+1; if(temp>max) max = temp; return res; } }
点赞 回复 分享
发布于 2016-04-20 14:35
class Transform{ public: string trans(string s,int n){ auto left = s.begin(); auto right = s.begin(); while (right != s.end()) { if(*right == ' '){ reverse(left,right); left = next(right); } right++; } reverse(left,right); reverse(s.begin(),s.end()); for (int i=0;i<s.length();++i) { if(isupper(s[i])){s[i] -= 'A'-'a';} else if(islower(s[i])){s[i] += 'A'-'a';} } return s; } }; class Patrition{ public: vector<int> getpartition(vector<vector<int>> & land,int n,int m){ vector<int> left(n+1,0); vector<int> right(n+1,0); vector<int> res; for (int j=0;j<n;++j) { for (int i=0;i<m;++i) { if(land[i][j] == 0){ left[j+1]++; } } left[j+1] += left[j]; } for (int j=n-1;j>=0;--j) { for (int i=m-1;i>=0;--i) { if (land[i][j] == 1) { right[j]++; } } right[j] += right[j+1]; } int resmax = -1; int pos = -1; for (int i=0;i<left.size();++i) { if (resmax < left[i] + right[i]) { resmax = left[i] + right[i]; pos = i; } } res.push_back(pos); res.push_back(pos+1); return res; } }; struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; class LongestPath { public: void longestpath(TreeNode* root,int &count,int &countincluderoot){ if(root == NULL) {count = 0;return ;} int countleft = 0,countright = 0; int countincluderootLeft = 0,countincluderootRight = 0; longestpath(root->left,countleft,countincluderootLeft); longestpath(root->right,countright,countincluderootRight); countincluderoot = 1; count = 1; if (root->left && root->right && root->left->val == root->val && root->right->val == root->val) { count += countincluderootLeft+countincluderootRight; countincluderoot = 1+ max(countincluderootLeft,countincluderootRight); } else if (root->left && root->left->val == root->val) { count += countincluderootLeft; countincluderoot = max(1,countincluderootLeft)+1; } else if (root->right && root->right->val == root->val) { count += countincluderootRight; countincluderoot = max(1,countincluderootRight)+1; } count = max(count,max(countleft,countright)); } int findPath(TreeNode* root) { // write code here int cnt = 0; int cc; longestpath(root,cnt,cc); return cnt; } };
点赞 回复 分享
发布于 2016-04-19 21:14
第四题没思路,前三题不难 = =
点赞 回复 分享
发布于 2016-04-19 21:13
做的时候做了三道题,只AC了一道题。挽!
点赞 回复 分享
发布于 2016-04-20 17:15
class LongestPath { public:     vector<int> findMaxPath(TreeNode* root, int &maxPath)         {         vector<int> tempMaxPath;//【0】储存最长的0色路径,【1】保存最长1色路径         tempMaxPath.push_back(0);         tempMaxPath.push_back(0);         if(!root)             return tempMaxPath;         vector<int> leftMaxPath = findMaxPath(root->left, maxPath);         vector<int> rightMaxPath = findMaxPath(root->right, maxPath);         //++tempMaxPath[root->val];                 tempMaxPath[1] = leftMaxPath[1] > rightMaxPath[1] ? leftMaxPath[1]  : rightMaxPath[1] ;         if(root->val == 0)             {             int temp = 0; // tempMaxPath[0] = leftMaxPath[0] + rightMaxPath[0]; temp = leftMaxPath[0] + rightMaxPath[0] + 1; tempMaxPath[0] =  leftMaxPath[0] > rightMaxPath[0] ? leftMaxPath[0] + 1 : rightMaxPath[0] + 1 ; // ++tempMaxPath[0];  if (maxPath < temp)  {  maxPath = temp;  }             tempMaxPath[1] = 0;         }         else             {              int temp = 0; // tempMaxPath[0] = leftMaxPath[0] + rightMaxPath[0]; temp = leftMaxPath[1] + rightMaxPath[1] + 1; tempMaxPath[1] =  leftMaxPath[1] > rightMaxPath[1] ? leftMaxPath[1] + 1 : rightMaxPath[1] + 1 ; // ++tempMaxPath[0]; if (maxPath < temp) { maxPath = temp; } tempMaxPath[0] = 0;         }         return tempMaxPath;     }     int findPath(TreeNode* root) {         // write code here         if(root == NULL)             return 0; int maxPath = 0;          findMaxPath(root, maxPath); return maxPath; //         if(root->val == 0) //             return tempMaxPath[0]; //         else //             return tempMaxPath[1];              } }; 不想说了,自己在电脑上多测了几个样例,想提交第三题,结果说已经结束了,什么鬼!
点赞 回复 分享
发布于 2016-04-20 16:39
有人跟我是奇葩地A了后三道吗。。。第一题掉进了某个坑里面。
点赞 回复 分享
发布于 2016-04-19 21:12
第一题,已经测试通过 var str=[]; var str1=[]; var str2=""; if(s.length==n){ for(var i=0;i<n;i++){ if(s.charAt(i)>='A'&&s.charAt(i)<='Z'){ str.push(s.charAt(i).toLowerCase()); }else if(s.charAt(i)>='a'&&s.charAt(i)<='z'){ str.push(s.charAt(i).toUpperCase()); }else{ str.push(s.charAt(i)); } } }else{ return "输入字符串与长度不匹配!请重新输入..."; } //alert(str); var str = str.reverse().join(""); var str1=str.split(" "); for(var i=0;i<str1.length;i++){ str2 += str1[i].toString().split("").reverse().join("")+" "; //str2.push(str1[i].toString().split("").reverse().join(""))); } //alert(str2.length); str2=str2.substring(0,n); return str2;
点赞 回复 分享
发布于 2016-04-19 22:33
http://www.nowcoder.com/test/1597148/summary  已经开放练习了,这次会公布错误测试用例了。
点赞 回复 分享
发布于 2016-04-19 21:49
ac了两道题,感觉这几道都不难,但是好久没碰算法了,手太生了,唉,要多花点时间在算法上了=。=求问过几道题能收到面试通知啊?
点赞 回复 分享
发布于 2016-04-19 21:31
第一题这么简单我觉得都不可能错的,竟然连一个用例都没通过。想不明白呀。 换用 Python 用同样的算法过了,真的是莫明其妙。
点赞 回复 分享
发布于 2016-04-19 21:27
最后一个感觉应该是 if(n%3==0) return (n-1)/2 ;else return (n+1)/2  
点赞 回复 分享
发布于 2016-04-19 21:14
/压哨提交的第三题,不知道提交上去没有。。。前几次有点问题,前序遍历+动态规划 class LongestPath { public:     int findPath(TreeNode* root) {         vector<int> white(301);         vector<int> black(301);         stack<TreeNode> number;         int max=0;         int count=0;         if(!root)             return 0;         if(root->val==0)            {             white[count]=1;             black[count]=0;             max=white[count];             count++;            }          else            {             black[count]=1;             white[count]=0;             max=black[count];             count++;            }         TreeNode* p=root;                  while(p||!number.empty())         {           if(p==root)           {               number.push(*p);               p=p->left;               continue;           }           if(p)           {             number.push(*p);             if(p->val==0)               {                white[count]=white[count-1]+1;                black[count]=0;                if(max<white[count])                  max=white[count];                count++;                               }             if(p->val==1)               {                black[count]=black[count-1]+1;                white[count]=0;                if(max<black[count])                  max=black[count];                count++;               }              p=p->left;           }                      else           {             TreeNode temp =number.top();             p=temp.right;             if(p)              continue;             else              {                number.pop();                count--;              }                        }                    }         return max;                  } };
点赞 回复 分享
发布于 2016-04-19 21:13
只AC了一道,应该没有希望了吧 。。。?!!
点赞 回复 分享
发布于 2016-04-20 10:06
import java.util.*; public class Transform { public String trans(String s, int n) { StringBuilder str=new StringBuilder(); String[] arr=s.split(" "); if (s.lastIndexOf(" ")==s.length()-1) { str.append(" "); } for (int i = arr.length-1; i >=0 ; i--) { for (int j = 0; j < arr[i].length(); j++) { String c=arr[i].charAt(j)+""; if (Character.isLowerCase(arr[i].charAt(j))) { str.append(c.toUpperCase()); }else { str.append(c.toLowerCase()); } } if (i!=0) { str.append(" "); } } return str.toString(); } }
点赞 回复 分享
发布于 2016-04-19 23:41
3道题把,第四道有点难,其他特简单
点赞 回复 分享
发布于 2016-04-19 23:11
2道,第一题做了90分,问题是没弄懂输入输出,一直编译错误,原来只需写出函数即可,不用写主函数
点赞 回复 分享
发布于 2016-04-19 22:38
被虐了。。
点赞 回复 分享
发布于 2016-04-19 21:48
牛客网会把这些题作为练习题再次放出来吗? 都不知道哪里错了!!!
点赞 回复 分享
发布于 2016-04-19 21:25
import java.util.*; public class Transform { public String trans(String s, int n) { // write code here s = s.substring(0,n); String[] temp = s.split(" +"); StringBuilder res = new StringBuilder(); char tempChar; for (int i = temp.length-1; i >= 0 ;i--){ for (int j = 0; j < temp[i].length() ; j++){ tempChar = temp[i].charAt(j); if (Character.isLowerCase(tempChar)) res.append(Character.toUpperCase(tempChar)); else if (Character.isUpperCase(tempChar)) res.append(Character.toLowerCase(tempChar)); } if (i!=0) res.append(" "); } return res.toString(); } } 求问第一题代码哪里错了。。一直不能AC。。第三题也是_(:зゝ∠)_
点赞 回复 分享
发布于 2016-04-19 21:23

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务