剑指offer题目31-40
面试题31:连续字数组的最大和
public class Solution {
public int FindGreatestSumOfSubArray(int[] array) {
int len = array.length;
if(len==0) return 0;
int[] dp = new int[len];
dp[0] = array[0];
int res = dp[0];
for(int i=1;i<len;i++){
dp[i] = Math.max(array[i],dp[i-1]+array[i]);
res = Math.max(dp[i],res);
}
return res;
}
}
面试题32:从1到n中出现的1的个数
public class Solution {
public int NumberOf1Between1AndN_Solution(int n) {
int cnt = 0;
int i = 1;
int current = 0,after = 0 ,before = 0;
while((n/i)!=0){
current = (n/i)%10;
before = n /(i*10);
after = n - (n/i)*i;
if(current > 1){
cnt += (before + 1)*i;
}else if(current == 0){
cnt += before * i;
}else if(current == 1){
cnt += before * i + after + 1;
}
i *= 10;
}
return cnt;
}
}
面试题33:把数组排列成最小的数
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Arrays;
public class Solution {
public String PrintMinNumber(int [] numbers) {
StringBuilder res = new StringBuilder();
if(numbers == null || numbers.length==0){
return "";
}
String[] nums =new String[numbers.length];
for(int i=0;i<numbers.length;i++){
nums[i] = Integer.toString(numbers[i]);
}
Comparator<String> comp = new Comparator<String>(){
@Override
public int compare(String str1,String str2){
return (str1 + str2).compareTo(str2 + str1);
}
};
Arrays.sort(nums,comp);
if(nums[nums.length-1].equals("0")){
return "0";
}
for(int i=0;i<numbers.length;i++){
res.append(nums[i]);
}
return res.toString();
}
}
面试题34:丑数
public class Solution {
public int GetUglyNumber_Solution(int index) {
if(index == 0) return 0;
int factor2 = 2,factor3 = 3,factor5 = 5;
int index2 = 0,index3 = 0,index5=0;
int[] ugly = new int[index];
ugly[0] = 1;
for(int i=1;i<index;i++){
int min = Math.min(factor2,Math.min(factor3,factor5));
ugly[i] = min;
if(min == factor2){
factor2 = 2 * ugly[++index2];
}
if(min == factor3){
factor3 = 3 * ugly[++index3];
}
if(min == factor5){
factor5 = 5 * ugly[++index5];
}
}
return ugly[index-1];
}
}
面试题35:第一个只出现一次的字符位置
import java.util.HashMap;
public class Solution {
public int FirstNotRepeatingChar(String str) {
if(str.length() == 0) return -1;
char[] arr = str.toCharArray();
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
for(int i=0;i<arr.length;i++){
if(map.get(arr[i]) == null){
map.put(arr[i],1);
}else{
map.put(arr[i],map.get(arr[i])+1);
}
}
boolean flag = false;
int index=0;
for(;index<arr.length;index++){
if(map.get(arr[index]) == 1){
flag = true;
break;
}
}
if(flag == true){return index;}
else return -1;
}
}
面试题36:数组中的逆序对
public class Solution {
public int InversePairs(int [] array) {
int cnt = 0;
cnt = inverseHelp(array,0,array.length-1);
return cnt;
}
public int inverseHelp(int[] arr,int start,int end){
int cnt = 0;
if(start < end){
int mid = start + (end - start)/2;
cnt += inverseHelp(arr,start,mid);
cnt += inverseHelp(arr,mid+1,end);
cnt += merge(arr,start,mid,end);
}
return cnt;
}
public int merge(int[] arr,int start,int mid,int end){
int cnt = 0,i=start,j=mid+1,k=start;
int[] tmp = new int[arr.length];
while(i<=mid && j<=end){
if(arr[i]>arr[j]){
cnt += end - j + 1;
tmp[k++] = arr[i++];
}else{
tmp[k++] = arr[j++];
}
}
while(i<=mid) tmp[k++] = arr[i++];
while(j<=end) tmp[k++] = arr[j++];
for(int p=start;p<=end;p++){
arr[p] = tmp[p];
}
return cnt;
}
}
平衡二叉树
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null) return true;
return height(root) != -1;
}
public int height(TreeNode root){
if(root == null){
return 0;
}
int leftHight = height(root.left);
if(leftHight == -1){return -1;}
int rightHight = height(root.right);
if(rightHight == -1){return -1;}
if(leftHight - rightHight<-1 || leftHight - rightHight > 1){
return -1;
}
return Math.max(leftHight,rightHight)+1;
}
}
面试题37:求两个链表的第一个公共结点
public class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
int len1 = getLen(pHead1);
int len2 = getLen(pHead2);
ListNode pFirst = (len1 > len2)?pHead1:pHead2;
ListNode pSecond = (len1 > len2)?pHead2:pHead1;
int i=0;
while(i<Math.abs(len1 - len2) && pFirst != null){
i++;
pFirst = pFirst.next;
}
while(pFirst != pSecond && pFirst != null && pSecond != null){
pFirst = pFirst.next;
pSecond = pSecond.next;
}
return pFirst;
}
public int getLen(ListNode p){
int len = 0;
while(p != null){
len ++;
p = p.next;
}
return len;
}
}
面试题38:数字在排序数组中出现的次数
public class Solution {
public int GetNumberOfK(int [] array , int k) {
int num = 0;
if(array != null && array.length>0){
int first = getFirstK(array,k,0,array.length - 1);
int last = getLastK(array,k,0,array.length - 1);
if(first>-1 && last>-1){
num = last - first + 1;
}
}
return num;
}
public int getFirstK(int[] array,int k,int start,int end){
if(start > end){
return -1;
}
int mid = (start + end)/2;
if(array[mid] == k){
if(mid>0 && array[mid-1]!=k || mid == 0)
return mid;
else
end = mid - 1;
}else if(array[mid] > k){
end = mid - 1;
}else{
start = mid + 1;
}
return getFirstK(array,k,start,end);
}
public int getLastK(int[] array,int k,int start,int end){
if(start > end){
return -1;
}
int mid = (start + end)/2;
if(array[mid] == k){
if(mid<array.length-1 && array[mid+1]!=k || mid == array.length-1)
return mid;
else
start = mid + 1;
}else if(array[mid] > k){
end = mid - 1;
}else{
start = mid + 1;
}
return getLastK(array,k,start,end);
}
}
面试题39:二叉树的深度
public class Solution {
public int TreeDepth(TreeNode root) {
if(root == null) return 0;
return Math.max(TreeDepth(root.left) , TreeDepth(root.right))+1;
}
}
面试题40:数组中只出现1次的数字
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
if(array == null || array.length<2) return;
int diff = 0;
for(int num : array){
diff ^= num;
}
diff = Integer.highestOneBit(diff);
for(int num : array){
if((diff & num) == 0){
num1[0] ^= num;
}else{
num2[0] ^= num;
}
}
return;
}
}
剑指offer题目31-40的更多相关文章
- 【剑指Offer】剑指offer题目汇总
本文为<剑指Offer>刷题笔记的总结篇,花了两个多月的时间,将牛客网上<剑指Offer>的66道题刷了一遍,以博客的形式整理了一遍,这66道题属于相对基础的算法题目,对于 ...
- 代码题 — 剑指offer题目、总结
剑指offer题目总结: https://www.cnblogs.com/dingxiaoqiang/category/1117681.html 版权归作者所有,任何形式转载请联系作者.作者:马孔多 ...
- 剑指offer题目系列三(链表相关题目)
本篇延续上一篇剑指offer题目系列二,介绍<剑指offer>第二版中的四个题目:O(1)时间内删除链表结点.链表中倒数第k个结点.反转链表.合并两个排序的链表.同样,这些题目并非严格按照 ...
- 再来五道剑指offer题目
再来五道剑指offer题目 6.旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...
- 剑指 Offer 题目汇总索引
剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格 ...
- 剑指offer题目java实现
Problem2:实现Singleton模式 题目描述:设计一个类,我们只能生成该类的一个实例 package Problem2; public class SingletonClass { /* * ...
- 牛客网上的剑指offer题目
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...
- 剑指offer题目系列二
本篇延续上一篇,介绍<剑指offer>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...
- 剑指offer题目系列一
本篇介绍<剑指offer>第二版中的四个题目:找出数组中重复的数字.二维数组中的查找.替换字符串中的空格.计算斐波那契数列第n项. 这些题目并非严格按照书中的顺序展示的,而是按自己学习的顺 ...
- 剑指offer题目解答合集(C++版)
数组中重复的数字 二维数组中查找 字符串 替换空格 二叉树的编码和解码 从尾到头打印链表 重建二叉树 二叉树的下一个节点 2个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...
随机推荐
- CentOS 7 时间同步
在做这个之前需要先搭建yum http://www.cnblogs.com/jw31/p/5955852.html 在做之前我们需要先安装ntp服务 yum install ntp -y vi /et ...
- WCF:没有终结点在侦听可以接受消息的*这通常是由于不正确的地址或者 SOAP操作导致的。
没有终结点在侦听可以接受消息的 http://xx.com/WebService.svc. 这通常是由于不正确的地址或者 SOAP 操作导致的.如果存在此情况,请参见 InnerException 以 ...
- python标准库xml.etree.ElementTree的bug
使用python生成或者解析xml的方法用的最多的可能就数python标准库xml.etree.ElementTree和lxml了,在某些环境下使用xml.etree.ElementTree更方便一些 ...
- SQLLite 学习笔记
1.SQLLite 简介 2.命令行使用 3.常用GUI管理工具
- R中,去掉dataframe中的NA行
R中使用complete.cases 和 na.omit来去掉包含NA的行 现在有个一data.frame datafile如下所示 Date sulfate nitrate ID 1 ...
- Object类概述
Object:类 Object 是类层次结构的根类.每个类都使用 Object 作为超类.每个类都直接或者间接的继承自Object类. Object类的方法:public int hashCode() ...
- 扫地雷II
感谢格致杭业晟同学改进完善 uses crt;var i,j,k,ls,x,y:byte; b:array[0..11,0..11] of shortint; f:array[0..11,0.. ...
- 剑指offer习题集2
1.把数组排成最小的数 class Solution { public: static bool compare(const string& s1, const string& s2) ...
- link
public IEnumerable InsuranceSearch(InsuranceSC sc, out int TotalCount) { var data = from q in Insura ...
- RichEdit
RichEdit 设置字符颜色 ; ; this->RichEdit1->SelAttributes->Color=clRed; 行间距字符间距 void __fastcall TF ...