刷刷刷

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相关推荐

  1. MySQL程序员面试笔试宝典pdf_数据库程序员面试笔试宝典

    前言 上篇 面试笔试经验技巧篇 第1章 求职经验分享2 1.1 踩别人没有踩过的坑,走别人没有走过的路2 1.2 一只小白成长为DBA的心路历程3 1.3 一个热衷于SQL优化的DBA成长经历3 第2 ...

  2. 【书】《数据库面试笔试宝典系列》简介

    [系列书]<数据库面试笔试宝典>相关内容 本文档说明 本文档主要是给数据库笔试面试系列书籍 < Oracle 数据库笔试面试宝典> . <数据库 程序员 面试笔试宝典&g ...

  3. Interview:算法岗位面试—10.23下午—上海某科技公司算法岗位(偏机器学习算法,上市)技术面试之比赛积累、项目经验、个人未来发展

    Interview:算法岗位面试-10.23下午-上海某科技公司算法岗位(偏机器学习算法,上市)技术面试之比赛积累.项目经验.个人未来发展 导读:该面试,是线上笔试通过之后,邀约的面试.整个面试过程比 ...

  4. Java面试笔试经验技巧总结

    想找到一份程序员的工作,一点技术都没有显然是不行的,但是,只有技术 也是不够的. 面试笔试经验技巧篇主要针对程序员面试笔试中遇到的 13 个常见 问题进行深度解析,并且结合实际情景,给出了一个较为合理 ...

  5. .Net工程师面试笔试宝典

    .Net工程师面试笔试宝典 传智播客.Net培训班内部资料 http://net.itcast.cn 这套面试笔试宝典是传智播客在多年的教学和学生就业指导过程中积累下来的宝贵资料,大部分来自于学员从面 ...

  6. 【.Net工程师面试笔试宝典】

    .Net工程师面试笔试宝典 培训班常见问题 1.你们会带着我们做完整个完整的项目吗? 答:小的项目会,大的项目则不可能, 1.众所周知,随便拿出一个中等大小的项目,也需要好多个熟练的开发人员开发好多个 ...

  7. 程序员求职攻略(《程序员面试笔试宝典》)之面试心得交流?

    "前车之鉴,后事之师",每一个成功的经验都能成为后来师弟师妹.学弟学妹学习的榜样,而每一次失败的经历也能给予后来者血的教训.本章以各大名牌高校.研究所的应届毕业生的亲身求职经历与体 ...

  8. 1、C语言面试笔试---变量定义和声明

    文章目录 1.背景 2.变量定义和声明 1.局部变量和全局变量 2.变量的存储类别 3.例题 4.内存泄漏 1.背景 2019秋招马上开始了,今天已经是7月30号了,赶紧刷刷C语言,争取在8月中旬刷完 ...

  9. 链表面试笔试题目总结

    链表是最基本的数据结构,凡是学计算机的必须的掌握的,在面试的时候经常被问到,关于链表的实现,百度一下就知道了.在此可以讨论一下与链表相关的练习题. 1.在单链表上插入一个元素,要求时间复杂度为O(1) ...

最新文章

  1. CVPR2020人脸防伪检测挑战赛冠亚军论文解读(下篇)
  2. [转]XPS转JPG转换器
  3. Extjs遇到的一些问题
  4. html页面懒加载灰度图片大小,小程序初级指南--图片及其优化
  5. mysql主从复制运维_Mysql主从复制配置
  6. 数据复盘《糖豆人》爆火营销过程:怎么做到以小博大?
  7. LiveVideoStack 2021招聘季
  8. linux7.0ftp,Linux(Centos7)搭建FTP服务
  9. 3月份GitHub上最热门的开源项目
  10. ASP.Net学习笔记005--ASP.Net的IsPostBack揭秘
  11. 使用IntelliJ IDEA配置Erlang开发环境
  12. ZOJ3210 A Stack or A Queue?【序列】
  13. echo输出不重复行到文件 shell_Shell脚本echo指令使用小技巧
  14. 高级软件工程第一次团队作业
  15. angular4 跨域携带cookie的设置
  16. LINUX下载编译OpenH264
  17. CentOS系统时间与网络同步
  18. js多线程编程web worker
  19. 【论坛】交通需求管理政策与实践——中国城市交通发展论坛第十一次研讨会讨论精选...
  20. win10纯净版安装教程

热门文章

  1. 关于获取当前时间出现1970年问题的解决
  2. 大学物理实验————自组惠斯通电桥测电阻数据处理代码
  3. 工信部通报下架60款APP:关联有赞、三六零、唯品会等上市公司
  4. diff 比较两个文件夹下各个文件的内容(差别)
  5. RESTful介绍(应用场合、常见的注解)
  6. 五年后中国将进入高收入国家行列?
  7. 微信小程序蓝牙ibeacon_微信开放蓝牙iBeacon接口小程序靠近原生APP功能
  8. DAS、SAN、NAS三种存储方式的概念及应用
  9. AV1编码标准整体概述
  10. 深度学习backbone是什么意思_什么是深度学习,深度学习是热门词