在网上看到过一个面试题,感觉挺有意思,看别人的代码写的逻辑不够谨慎,重写了一个,较真了又... package com.array7.algorithm; public class AlgorithmTest { public static void main(String[] args) { int[] arr = {2 ,4 ,5 ,8 ,10 ,12 ,13 ,16 ,17,Integer.MAX_VALUE }; int sum = 13; String result = getSumE…
#include <stdio.h> int main(void) { /* 选择排序算法 原理:从数组中 找出最小的元素然后交换位置: */ int a[10] = {9,5,10,7,2,3,1,6,8,4}; int i=0,j=0; int n = sizeof(a)/4; //外循环n-1轮 for(i=0;i<n-1;i++){ int pos = i;//始终指向最小的位置 for(j=i+1;j<n;j++){ if(a[j]<a[pos]){ pos = j…
先讨论出现次数大于n/2的数字,如果这样的数字存在,那么这个数出现的次数大于其他数出现的次数的总和. 在数组A中,我们定义两个数据集合a1,a2.a1为出现次数大于n/2的数的集合,a2为其余数组成的集合.对于数组 A中元素a.b,假设a不等于b,那么有两种情况,分别为:a属于a1,b属于a2:a属于a2,b属于a2.对于这两种情况,如 果把a.b从数组A中去掉,集合a1的size依旧是大于a2的.按照这个思路,我们有如下代码: int m; ; for (auto num : nums) {…
/*利用冒泡排序实现*/ import java.util.Scanner;public class Paixun { public static void main(String[] args) { int []a;int p=0,q=9; a=new int[10] ; Scanner in=new Scanner(System.in); for(int i=0;i<10;i++) {  a[i]=in.nextInt(); } for(int j=0;j<a.length-1;j++) …
#include <bits/stdc++.h> using namespace std; const int N = 1e6,INF = 0x3f3f3f3f; int a[N]; int n,x; void fun(int n) { int left = 0, right = n-1; while(left < right) { int ans=a[left] + a[right]; if(ans == x) { cout<<a[left]<<' '<&…
题目: 给定一个旋转数组,但是你不知道旋转位置,在旋转数组中找出给定target值出现的位置:你可以假设在数组中没有重复值出现 举例: (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 在旋转后的数组中查找6,则返回index =2. 若查找10,则返回index= -1: 解题思路: 由于数组中没有重复出现的数字,因此旋转后的数组基本可以划分为两部分:因此解题思路可以转换为我们熟悉的二分查找:但是在二分查找时我们需要注意,到底在哪边查找? 1)…
算法!用C#写一个函数,在一个数组中找出随意几个值相加等于一个值比如,数组{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}  要找出那些数相加等于100 解决方案一: #region 解决方案一 , , , , , , , , , , , , , , , , , , , }; List<List<int>> mylist = new List<List<int>>(); int length = mya…
今天下午,看了一会github,想刷个题呢,就翻出来了刷点题提高自己的实际中的解决问题的能力,在面试的过程中,我们发现,其实很多时候,面试官 给我们的题,其实也是有一定的随机性的,所以我们要多刷更多的题.去发现问题. 题目:     给定一个整数数组 nums 和一个目标值 taget,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 解析: 实际这里就是给你的一个列表的数字,给你一个预期,让你返…
题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respectively, design an algorithm to find the kth largest key. The order  of growth of the worst case running time of your algorithm should be logn, where n…
查找两个有序数组中的第K个元素 int FindKth(int a[], int b[], int k, int astart, int aend, int bstart, int bend) { ; ; ) { return b[bstart + k]; } ) { return a[astart + k]; } ) { return a[astart] > b[bstart] ? b[bstart] : a[astart] ; } int amid = aLen * k / (aLen +…