1,【求数字1出现的次数】
问题描述:
输入一个整数n,求从1到n这n个整数(十进制)中1出现的次数。要求空间复杂度为O(n)。
输入描述:
1 |
|
输出描述:
1 |
|
输入样例:
12
输出样例:
5
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
int NumberOf1(const char* strN);
int PowerBase10(unsigned int n);
int NumberOf1Between1AndN(int n)
{
if(n <= 0)
return 0;
char strN[50];
sprintf(strN, "%d", n);
return NumberOf1(strN);
}
int NumberOf1(const char* strN)
{
if(!strN || *strN < '0' || *strN > '9' || *strN == '\0')
return 0;
int first = *strN - '0';
unsigned int length = static_cast<unsigned int>(strlen(strN));
if(length == 1 && first == 0)
return 0;
if(length == 1 && first > 0)
return 1;
// 假设strN是"21345"
// numFirstDigit是数字10000-19999的第一个位中1的数目
int numFirstDigit = 0;
if(first > 1)
numFirstDigit = PowerBase10(length - 1);
else if(first == 1)
numFirstDigit = atoi(strN + 1) + 1;
// numOtherDigits是01346-21345除了第一位之外的数位中1的数目
int numOtherDigits = first * (length - 1) * PowerBase10(length - 2);
// numRecursive是1-1345中1的数目
int numRecursive = NumberOf1(strN + 1);
return numFirstDigit + numOtherDigits + numRecursive;
}
int PowerBase10(unsigned int n)
{
int result = 1;
for(unsigned int i = 0; i < n; ++ i)
result *= 10;
return result;
}
// ====================测试代码====================
void Test(int n)
{
printf("%d", NumberOf1Between1AndN(n));
}
int main(int argc, char* argv[])
{
int a;
scanf("%d", &a);
Test(a);
return 0;
}
2,【查找无序数组中第二大的元素】
问题描述:
输入一个无序数组data[] , 且数组中无重复元素,查找出第二大元素。要求: 时间复杂度为O(n)。
输入描述:
1 |
|
输出描述:
1 |
|
输入样例:
1 |
|
输出样例:
1 |
|
import java.util.Scanner;
public class Main{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().toString();
String strArr[] = str.split(" ");
int data[] = new int[strArr.length];
for(int i=0; i < data.length; i++) {
data[i] = Integer.parseInt(strArr[i]);
}
System.out.println(secondMaxNum(data));
}
public static int secondMaxNum(int[] testArray)
{
int max1 = 0;
int max2 = 0;
for (int i = 1;i < testArray.length; i++)
{
if(testArray[i] > testArray[max1])
{
max2 = max1;
max1 = i;
}
else if(testArray[i] > testArray[max2] && testArray[i] < testArray[max1])
max2 = i;
}
return testArray[max2];
}
}
3,
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
/*length 为字符数组str的总容量,大于或等于字符串str的实际长度*/
void ReplaceBlank(char str[], int length)
{
if(str == nullptr && length <= 0)
return;
/*originalLength 为字符串str的实际长度*/
int originalLength = 0;
int numberOfBlank = 0;
int i = 0;
while(str[i] != '\0')
{
++ originalLength;
if(str[i] == ' ')
++ numberOfBlank;
++ i;
}
/*newLength 为把空格替换成'%20'之后的长度*/
int newLength = originalLength + numberOfBlank * 2;
if(newLength > length)
return;
int indexOfOriginal = originalLength;
int indexOfNew = newLength;
while(indexOfOriginal >= 0 && indexOfNew > indexOfOriginal)
{
if(str[indexOfOriginal] == ' ')
{
str[indexOfNew --] = '0';
str[indexOfNew --] = '2';
str[indexOfNew --] = '%';
}
else
{
str[indexOfNew --] = str[indexOfOriginal];
}
-- indexOfOriginal;
}
}
// ====================测试代码====================
void Test(char* testName, char str[], int length, char expected[])
{
if(testName != nullptr)
printf("%s begins: ", testName);
ReplaceBlank(str, length);
if(expected == nullptr && str == nullptr)
printf("passed.\n");
else if(expected == nullptr && str != nullptr)
printf("failed.\n");
else if(strcmp(str, expected) == 0)
printf("passed.\n");
else
printf("failed.\n");
}
// 空格在句子中间
void Test1(char str[])
{
const int length = 100;
// char str[length] = "hello world";
ReplaceBlank(str, length);
cout << str << endl;
}
int main(int argc, char* argv[])
{
const int length = 100;
char str[length];
cin.getline(str,length);
Test1(str);
return 0;
}
4,
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct ListNode
{
int m_nValue;
ListNode* m_pNext;
};
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if(pHead1 == nullptr)
return pHead2;
else if(pHead2 == nullptr)
return pHead1;
ListNode* pMergedHead = nullptr;
if(pHead1->m_nValue < pHead2->m_nValue)
{
pMergedHead = pHead1;
pMergedHead->m_pNext = Merge(pHead1->m_pNext, pHead2);
}
else
{
pMergedHead = pHead2;
pMergedHead->m_pNext = Merge(pHead1, pHead2->m_pNext);
}
return pMergedHead;
}
ListNode * CreateNode(int n)
{
ListNode *head = NULL, *pnew = NULL, *ptail = NULL;
int num, i=1;
while(i <= n)
{
pnew = new ListNode;
cin>>num;
pnew->m_nValue = num;
pnew->m_pNext = NULL;
if(head == NULL)
head = pnew;
else
{
ptail->m_pNext = pnew;
}
ptail = pnew;
i++;
}
pnew = NULL;
delete pnew;
return head;
};
int main(){
ListNode* nodeFirst, *nodeSecond;
nodeFirst = CreateNode(4);
nodeSecond = CreateNode(4);
ListNode* result;
result = Merge(nodeFirst, nodeSecond);
ListNode* head=result;
while(head){
cout << head->m_nValue;
head = head->m_pNext;
}
}