面试笔试杂项积累-leetcode 6-10
刷刷刷
6.3-Longest Substring Without Repeating Characters-Difficulty: Medium
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
思路
这道题是求最大连续的字符个数,博主一开始还以为是求不同字符的个数,结果怎么提交都不成功,呵呵呵,知道了题意就很简单了,两个循环,一个检查是否有重复字符,一个循环所有字符,在循环所有字符中,检测并判断是否重复。如果不重复则把当前字符加到一个temp临时string中,并用temp.Length与Max比较(max为储存当前最大长度的int),大的话则max更新。如果重复,通过第一个循环,也就是下面代码的check函数,返回重复字符位置,剪去temp中重复字符(包括重复字符之前的所有字符)
public class Solution {public int LengthOfLongestSubstring(string s){int max = 0;string temp = "";int temp_int = 0;for (int i = 0; i < s.Length; i++){temp_int = check(temp, s[i]);temp += s[i];if (temp_int == -1){if (temp.Length > max)max = temp.Length;}else{temp = temp.Substring(temp_int+1, temp.Length-temp_int-1);}}return max;}int check(string str, char temp){for (int i = 0; i < str.Length; i++){if (str[i] == temp)return i;}return -1;}
}
7.4-Median of Two Sorted Arrays-Difficulty: Hard
There are two sorted arrays nums1 andnums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
思路
Binary Search
参考:
http://blog.csdn.net/yutianzuijin/article/details/11499917/
http://blog.csdn.net/xuyze/article/details/45198757
http://blog.csdn.net/zxzxy1988/article/details/8587244
public class Solution {double findKth(int[] a, int m, int[] b, int n, int k, int a_temp, int b_temp){if (m > n)return findKth(b, n, a, m, k, b_temp, a_temp);if (m == 0)return b[b_temp + k - 1];if (k == 1)return (a[a_temp + 0] < b[b_temp + 0]) ? a[a_temp + 0] : b[b_temp + 0];//divide k into two parts int pa = (k / 2 < m) ? k / 2 : m, pb = k - pa;if (a[a_temp + pa - 1] < b[b_temp + pb - 1])return findKth(a, m - pa, b, n, k - pa, a_temp + pa, b_temp);else if (a[a_temp + pa - 1] > b[b_temp + pb - 1])return findKth(a, m, b, n - pb, k - pb, a_temp, b_temp + pb);elsereturn a[a_temp + pa - 1]; }public double FindMedianSortedArrays(int[] nums1, int[] nums2){int m = nums1.Length;int n = nums2.Length;int total = m + n;if ((total & 0x1) != 0)return findKth(nums1, m, nums2, n, total / 2 + 1, 0, 0);elsereturn (findKth(nums1, m, nums2, n, total / 2, 0, 0)+ findKth(nums1, m, nums2, n, total / 2 + 1, 0, 0)) / 2;}
}
8.5-Longest Palindromic Substring-Difficulty: Medium
Given a string S, find the longest palindromic substring inS. You may assume that the maximum length ofS is 1000, and there exists one unique longest palindromic substring.
思路
一开始自己造方法虽能实现功能但是超时,最后使用DP通过
public class Solution {public string LongestPalindrome(string s) {if (s == null || s.Length == 0)return null;int start = 0;int end = 0;int len = 0;bool[,] dp = new bool[s.Length, s.Length];for (int i = s.Length - 1; i >= 0; i--) {for (int j = i; j < s.Length; j++) {if (i == j || (s[i] == s[j] && j - i < 2)|| (s[i] == s[j] && dp[i + 1,j - 1])) {dp[i,j] = true;if (j - i + 1 > len) {len = j - i;start = i;end = j + 1;}}}}return s.Substring(start, end-start);}
}
9.7-Reverse Integer-Difficulty: Easy
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
方法一
思路
把数反过来,博主第一个想到的方法是用string,再循环反向输入,故就用此方法,要注意正负号的情况,要把负数的负号去掉再反过来,第一次完成时欠考虑,没有考虑超出int的情况,int的最大值为2147483647,最小值为-2147483648,所以判断正负时把int a = -int.MinValue也是不行的,此时a还是int.MinValue。解决这个问题就把int.MinValue单拿出来判断,其余全与int.MaxValue比较即可
public class Solution {public int Reverse(int x) {string str = "";string str_fin = "";if (x == int.MinValue)return 0;str = ((x > 0 ? +1 : -1) * x).ToString();for (int i = str.Length - 1; i > -1; i--){str_fin += str[i];}if (int.MaxValue > long.Parse(str_fin))return (x > 0 ? +1 : -1) * int.Parse(str_fin);else return 0;}
}
方法二
思路
不依赖string,求出int每一位再赋值,依然存在最大值最小值问题,,也是一种笨方法,
public int Reverse(int x)//法2{if (x == int.MinValue)return 0;int negative = 1;if (x < 0){negative = -1;x = -x;}long y = x % 10;while (x / 10 != 0){x /= 10;y *= 10;y += x % 10;}if (y > int.MaxValue){return -1;}else{return (int)(negative * y);}
}
10.8-String to Integer (atoi)-Difficulty: Easy
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
方法一
思路
刚开始看到题目时,感觉很简单,后来因为隐含条条框框太多而数次没有通过感觉心烦意乱,遂在代码上修修补补改成下面这个样子,
说起条框
1.首先要默默的把空格去掉再比对,
2.“-123123”,“+1243123”要可以容忍+-号
3.“123a345”要取a前面的数字
4.超过int范围要返回极值,int的最大值为2147483647,最小值为-2147483648
5.会出现"+-213"的情况,需要加额外判断注意
上图证明条框4
第一次出现这个博主傻傻的以为leetcode出问题了,遂加上对MaxValue+1的判断,随后又出现这个问题
才明白怎么回事
。。。总之不细心是不行啊,条框陷阱多
博主本次直接用string操作,把string转换为ascII码然后加以判断,然后转换int,,(总感觉老依赖c#的string现成的函数的这个习惯不太好)
public class Solution {public int MyAtoi(string str){str = str.Trim();if (str.Length == 0 )return 0;� System.Text.ASCIIEncoding asciiEncoding; int intAsciiCode = 0;bool sign = false;for (int i = 0; i < str.Length; i++){asciiEncoding = new System.Text.ASCIIEncoding();intAsciiCode = (int)asciiEncoding.GetBytes(str)[i];if (i == 0){if (!sign && (intAsciiCode == 43 || intAsciiCode == 45)){if (i != str.Length - 1){intAsciiCode = (int)asciiEncoding.GetBytes(str)[i + 1];if (intAsciiCode >= 48 && intAsciiCode <= 57){sign = true;continue;}elsereturn 0;}elsereturn 0;}if (intAsciiCode < 48 || intAsciiCode > 57){return 0;}}if (intAsciiCode < 48 || intAsciiCode > 57){str = str.Substring(0,i);break;}}if (str.Length == 0 )return 0;if(str.Length > 11){asciiEncoding = new System.Text.ASCIIEncoding();intAsciiCode = (int)asciiEncoding.GetBytes(str)[0];if(intAsciiCode == 43) return int.MaxValue;if(intAsciiCode == 45) return int.MinValue;return int.MaxValue;}long temp = long.Parse(str);if (temp > int.MaxValue){return int.MaxValue;}if (temp < int.MinValue){return int.MinValue;}return int.Parse(str);}
}
方法二
思路
把string转换为char数组再操作,省去转ascII,省事不少
参考:
http://blog.csdn.net/ljiabin/article/details/40508889
public int Atoi(string str){if (str == null || str.Length == 0) return 0;char[] arr = str.ToCharArray();int i = 0;bool space = false;bool negative = false;while (arr[i] == ' ') i++;if (arr[i] == '-'){negative = true;++i;}if (arr[i] == '+') ++i;long sum = 0;for (; i < arr.Length; i++){if (arr[i] == ' '){space = true;}else if (space == true){break;}else if (arr[i] >= '0' && arr[i] <= '9'){sum = sum * 10 + arr[i] - '0';}else{break;}}return (int)(negative ? -sum : sum);}
面试笔试杂项积累-leetcode 6-10相关推荐
- MySQL程序员面试笔试宝典pdf_数据库程序员面试笔试宝典
前言 上篇 面试笔试经验技巧篇 第1章 求职经验分享2 1.1 踩别人没有踩过的坑,走别人没有走过的路2 1.2 一只小白成长为DBA的心路历程3 1.3 一个热衷于SQL优化的DBA成长经历3 第2 ...
- 【书】《数据库面试笔试宝典系列》简介
[系列书]<数据库面试笔试宝典>相关内容 本文档说明 本文档主要是给数据库笔试面试系列书籍 < Oracle 数据库笔试面试宝典> . <数据库 程序员 面试笔试宝典&g ...
- Interview:算法岗位面试—10.23下午—上海某科技公司算法岗位(偏机器学习算法,上市)技术面试之比赛积累、项目经验、个人未来发展
Interview:算法岗位面试-10.23下午-上海某科技公司算法岗位(偏机器学习算法,上市)技术面试之比赛积累.项目经验.个人未来发展 导读:该面试,是线上笔试通过之后,邀约的面试.整个面试过程比 ...
- Java面试笔试经验技巧总结
想找到一份程序员的工作,一点技术都没有显然是不行的,但是,只有技术 也是不够的. 面试笔试经验技巧篇主要针对程序员面试笔试中遇到的 13 个常见 问题进行深度解析,并且结合实际情景,给出了一个较为合理 ...
- .Net工程师面试笔试宝典
.Net工程师面试笔试宝典 传智播客.Net培训班内部资料 http://net.itcast.cn 这套面试笔试宝典是传智播客在多年的教学和学生就业指导过程中积累下来的宝贵资料,大部分来自于学员从面 ...
- 【.Net工程师面试笔试宝典】
.Net工程师面试笔试宝典 培训班常见问题 1.你们会带着我们做完整个完整的项目吗? 答:小的项目会,大的项目则不可能, 1.众所周知,随便拿出一个中等大小的项目,也需要好多个熟练的开发人员开发好多个 ...
- 程序员求职攻略(《程序员面试笔试宝典》)之面试心得交流?
"前车之鉴,后事之师",每一个成功的经验都能成为后来师弟师妹.学弟学妹学习的榜样,而每一次失败的经历也能给予后来者血的教训.本章以各大名牌高校.研究所的应届毕业生的亲身求职经历与体 ...
- 1、C语言面试笔试---变量定义和声明
文章目录 1.背景 2.变量定义和声明 1.局部变量和全局变量 2.变量的存储类别 3.例题 4.内存泄漏 1.背景 2019秋招马上开始了,今天已经是7月30号了,赶紧刷刷C语言,争取在8月中旬刷完 ...
- 链表面试笔试题目总结
链表是最基本的数据结构,凡是学计算机的必须的掌握的,在面试的时候经常被问到,关于链表的实现,百度一下就知道了.在此可以讨论一下与链表相关的练习题. 1.在单链表上插入一个元素,要求时间复杂度为O(1) ...
最新文章
- CVPR2020人脸防伪检测挑战赛冠亚军论文解读(下篇)
- [转]XPS转JPG转换器
- Extjs遇到的一些问题
- html页面懒加载灰度图片大小,小程序初级指南--图片及其优化
- mysql主从复制运维_Mysql主从复制配置
- 数据复盘《糖豆人》爆火营销过程:怎么做到以小博大?
- LiveVideoStack 2021招聘季
- linux7.0ftp,Linux(Centos7)搭建FTP服务
- 3月份GitHub上最热门的开源项目
- ASP.Net学习笔记005--ASP.Net的IsPostBack揭秘
- 使用IntelliJ IDEA配置Erlang开发环境
- ZOJ3210 A Stack or A Queue?【序列】
- echo输出不重复行到文件 shell_Shell脚本echo指令使用小技巧
- 高级软件工程第一次团队作业
- angular4 跨域携带cookie的设置
- LINUX下载编译OpenH264
- CentOS系统时间与网络同步
- js多线程编程web worker
- 【论坛】交通需求管理政策与实践——中国城市交通发展论坛第十一次研讨会讨论精选...
- win10纯净版安装教程