Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
okok2
using System; namespace test { int SearchInsertBest(int[] nums, int target) { for (int i = 0; i < nums.Length; i++) { if (nums[i] >= target) { return i; } } // Daca nu e nici un numar mai mare decat target, returneaza ultima pozitie return nums.Length; } class Program { static void Main(string[] args) { int[] nums = { 1, 3, 5, 6 }; int target = 4; SearchInsert(nums, target); // nu se declara functii in functii, asta tre sa fie deasupra la Main() int SearchInsert(int[] nums, int target) { int targetIndex = Array.IndexOf(nums, target); if(targetIndex > -1) { Console.WriteLine(targetIndex); } else if(targetIndex<0) { for (int i = 0; i < nums.Length; i++) { if (nums[i] > target) { // Aici poti sa faci direct Console.WriteLine(i); // .IndexOf face cautare in tot array-ul si n-are rost Console.WriteLine(Array.IndexOf(nums, nums[i])); // De preferat return; in loc de break; daca vrei sa termini executia complet break; } // Mai simplu sa faci Console.WriteLine(nums.Length) cand se termina for-ul else if (target < nums[i]) { // Iar faci IndexOf Console.WriteLine(Array.IndexOf(nums, nums[i])); break; } // else if (target > nums[nums.Length-1]) { int finalReslt = 1; Console.WriteLine(Array.IndexOf(nums, nums[nums.Length - 1])+finalReslt); break; } } } return 0; } } } }
run
|
edit
|
history
|
help
0
Operator operates. Op!
bc160401882
per1
a
Time by 15 minutes
LeftAndRightShiftStrings
Write a program to filter distinct items from the array
arrays bidimencionales y tridimencionales
yari
Sandeep Chandra Bose